9
Department of Electrical Engineering N-W.F.P University of Engineering and Technology, Peshawar Computer Programming, Mid Term, Spring Semester XXXX Time Allowed: 02hours MAIN CAMPUS Q.No.01: Encircle the correct answer. [15 points] 1. In C/C++ the #include is called, a) Header file b) Preprocessor Directive c) Statement d) Function 2. Loops are ------------------------ Structure. a) Decision b) Repetition c)Sequential d) Hierarchical 3. What will be the result of arithmetic expression 6+27/3*3 ? a) 33 b) 45 c) 9 d) 30 4. Default case in switch statement is, a) Must b) Optional c) syntax error d) Necessary 5. The C++ data type that can take on either one of only two possible values is a) Binary

Midterm Paper

Embed Size (px)

DESCRIPTION

mid term paper of C++ OF u.e.t. PESHAWAR

Citation preview

Page 1: Midterm Paper

Department of Electrical Engineering N-W.F.P University of Engineering and Technology, Peshawar Computer Programming, Mid Term, Spring Semester XXXX

Time Allowed: 02hours

MAIN CAMPUS

Q.No.01: Encircle the correct answer. [15 points]

1. In C/C++ the #include is called,a)  Header file b)  Preprocessor Directive c)  Statement d)  Function

2. Loops are ------------------------ Structure.a) Decision b) Repetition c) Sequential d)  Hierarchical

3. What will be the result of arithmetic expression 6+27/3*3 ? a)  33 b) 45 c) 9 d) 30

4.  Default case in switch statement is, 

a)   Must 

b) Optional 

c)       syntax error 

d)     Necessary

5. The C++ data type that can take on either one of only two possible values isa) Binaryb) shortc) Booleand) Character

6. The number of arguments or parameters with which a function can be defined is

a) Not limitedb) Depends on data typec) Oned) Eight

Page 2: Midterm Paper

7. Variables that can be accessed and altered only within the function in which they are defined are called

a) Local variablesb) Global variablesc) Static variablesd) User defined variables

8. If the variable a = 7, what will the following code fragment print out? cout<<a--;cout<<++a;

a) 76b) 67c) 78d) 77

9. Variables inside parenthesis of functions declarations have _____ level access.

a)  Local

b) Global

c) Module

d)  Universal

10. If the variable a = 7, what will the following code fragment print out? cout<<a--;cout<<a++;

a) 76b) 67c) 78d) 77

11. The library function exit() causes an exit from

a) the loop in which it occursb) the block in which it occursc) the function in which it occursd) the program in which it occurs

12. Assume there are three bool variables; a=false, b=true, c=false; what would the following instruction evaluate to.

cout<<a || b && c||a;a) 0b) 1c) true d) false

Page 3: Midterm Paper

13. Output of following code will beint b , a=5;b= ++a + --a;cout<<b<<"\t"<<++a<<"\t"<<a<<"\t"<<--a;

a) 10 5 6 5b) 10 6 6 5c) 9 5 5 4d) 9 5 6 5

14. Output of following code will be

int i=0;for(;i<=5;)i++;cout<<i;

a) 0 1 2 3 4 5b) 0 1 2 3 4c) 4d) Error

15. A C++ program contains a function with the header int function(double d, char c). Which of the following function headers could be used within the same program?

a) char function(double d, char c)b) int function(int d, char c)c) both (a) and (b)d) neither (a) nor (b)

Q.No.02: Write the output of following and if there is any error, identify and describe it. In case of logical errors, you have to show the outputs and also you have to identify the errors. In case of syntax errors, just identify and describe the errors.

[4x13=52 points]

1. main(){

for(int x=1;x<=5;++x) cout<<x<<endl;getch();}

2. main(){ for(int x=1;x<=5;)

{ ++x; cout<<x; }getch(); }

Page 4: Midterm Paper

3. main() {

  int a=9;  cout<<(a++*++a);

getch(); }

4. main() {

  int a=9;  cout<<(a++*a++); getch(); }

5: main() {

int i=5.5;do{

i=i*2;cout<<i<<"\t";

}while ((i>5)&&(i<25)); getch();

}

6: main() { int a=2; if (a) cout<<"false\t"; if(!a) cout<<"true\t"; else cout<<"invalid"<<endl; getch(); }

7:main(){ int A=3, B=5;

Page 5: Midterm Paper

if( A= =3 && B= =5 || B= =0)cout<<”this exam was difficult\n”;elsecout<<”exam was easy\n”;getch();}

8: main(){ int i=2;

switch(i){ case 1: {

i=i-1; break;

}case 2:

{i=i+2;

}

case 3:{i=i+i;

break;}

default:{i=i-1;

}

}cout<<”i =”<<i;getch();}

9. main(){ for(int n=0;n<n+1;n++);

cout<<n<<endl;getch();

}

Page 6: Midterm Paper

10.main(){int n = 3;while (n >= 0)cout << (n /= 2) << endl;getch();}

11. main(){int n;cout << (n = 4) << endl;cout << (n == 4) << endl;cout << (n > 3) << endl;cout << (n < 4) << endl;cout << (n = 0) << endl;cout << (n == 0) << endl;cout << (n > 0) << endl;cout << (n && 4) << endl;cout << (n || 4) << endl;cout << (!n) << endl;getch();}

12.main(){int n, k = 5;n = (100 % k ? k + 1 : k - 1);cout << "n = " << n << " k = " << k << endl;getch();}

13.main(){while(1){ int x=4;

x++;};cout<<x;

getch();}

Page 7: Midterm Paper

Q.No. 03: Write short answers to following questions. [10]

1. What is the purpose of the getch() function near the end of each program?

2.

Define “variable.”

3.

What is the scope of a function’s parameters?

4. How is a compiler different from an assembler?

5. Is C++ a low-level or a high –level language? What’s the difference between the two?

Q.No. 04: Write down a program that calculates the factorial of a number. If a negative number is entered, user should be prompted to enter a positive value.

[10]

Q.No. 05: Write a function named "g_c_d" that takes two positive integer arguments and returns as its value the greatest common divisor of those two integers. If the function is passed an argument that is not positive (i.e., greater than zero), then the function should return the value 0 as a sentinel value to indicate that an error

Page 8: Midterm Paper

occurred. Thus, for example,cout << g_c_d(40,50) << endl; // will print 10cout << g_c_d(10,-6) << endl; // will print 0 (even though 2 is the g.c.d.) [15]