Computer Programming-2
Computer Programming-2
int a = 7, b;
b = a++;
cout<<++b;
A. 9
B. 7
C. 8
D. None of the above
2. Which of the following is not true?
a. (7.0 * 3) / 5 is evaluated to 4.2
b. 8 * 3 / 3 evaluated to 8
c. (5 > 3) || (5 != 5) && ( 2 <= 2) results in true
d. (8.0 * 3) % 5 is evaluated to 4
e. None of the above
3. What will be the output produced by the following code if the value of vehicleType is 2?
int vehicleType;
cout << "Enter vehicle type: ";
cin >> vehicleType;
switch (vehicleType) {
case 1:
cout << "Passenger car.";
case 2:
cout << "Bus.";
case 3:
cout << "Truck.";
default:
cout << "Unknown vehicle class!";
}
A. Bus.
B. Passenger Car.
C. Bus.Truck.
D. None of the above
4. Given the following nested loop, how many times will "hello" be displayed on the screen?
int i = 1;
while(i < 5){
int j = 10;
while (j > i){
j --;
cout<<“hello”<<endl;
}
i = i + 5;
}
A) 5
B) 10
C) 25
D) 9
E) None of the above
5. Considering the below given string declaration, which one of the following statements is
correct?
char msg[] = "This is the final exam for C++";
A. There is an error with the initialization statement
B. strlen(msg) would return 24
C. strcpy(msg, "Hurray") would assign the content of msg to Hurray
D. cout<<msg ; would display: This is the final exam for C++
E. None of the above
Consider the following code fragment and assume the address of the first element in the
array is 7000. Based on this information answer two questions
float num [4] = {2.0, 1.0, 3.0, 5.5};
float *ptr;
ptr = num;
6. Which one of the following is false?
A) num is the same as &num[0].
B) The statement cout<<ptr++; will print 1.0.
C) The statement cout<<num[3]; will print 5.5.
D) ptr + 3 is equal to 7012.
7. Which one of the following is wrong way of C++ Function declaration?
A. void Sum( );
B. int Sum(int x);
C. void Sum(int x, int y);
D. None of the above
8. What is the output of the below given program?
int figure_me_out () {
static int x = 0;
if (x % 2 == 0)
x = x + 1;
else
x = x + 2;
return x;
}
A. 1
B. 19
C. 20
D. 2
E. None of the above
9. When overloading a function, what must be true?
A. The names should be different with the same number and/or types of parameters.
B. The names should be the same with different number and/or types of
parameters.
C. The names should be different with different number and/or types of parameters.
D. The names should be the same with the same number and/or types of parameters.
E. None of the above
10. What is the output of the following code fragment?
int f1(int n, int m){
if(n < m)
return 0;
else if(n == m)
return m + f1(n-1, m);
else
return n + f1(n-2, m-1);
}
int main(){
cout << f1(1,4);
return 0;
}
A. 0
B. 4
C. 8
D. None of the above
11. One of the following statements is true
A. If the function is declared inline the compiler must copy the code in the calling
function
B. It is recommended to use inline functions to simple and frequently used
functions
C. The typedef command is used to create a new type.
D. All of the above
E. None of the above
12. Which one of the following is not true about a structure?
A. Members of a structure may not have the same type.
B. A structure definition doesn't reserve any memory space.
C. Assignment operation between two structure variables of the same type is legal.
D. Like any other variable type structure variables can be passed by value and
reference.
E. None of the above
Consider the following structure definition and answer Question #9 and Question #10
struct house {
double price;
int rooms;
};
house *ptr1, *ptr2;
13. Which one of the following is valid?
A. cout<<ptr1<<ptr2;
B. ptr2->rooms = ptr1->rooms;
C. ptr1 < ptr2;
D. ptr1 = ptr1 + ptr2;
14. Which one of the following is wrong way of initializing members of a structure?
A. struct Time {
int h = 06, m = 20;
float s = 25.00;
};
B. struct Time {
int h, m;
float s;
}t = {06, 20, 25.00};
C. struct Time {
int h, m;
float s;
};
Time t = {06, 20, 25.00};
D. All of them are correct
E. None of the above
15. Which of the following statement will return the current position of the file pointer if the file
is opened for write mode? Assume fileout is a file variable or object.
A. fileout.tellg()
B. fileout.tellp()
C. fileout.seekg()
D. fileout.seekp()
E. None of the above
16. Which one of the following is different from others?
A. if(in.fail( )){
Statement;
}
B. if(in.bad( )){
Statement;
}
C. if(!in){
Statement;
}
D. if(in.is_open()){
Statement;
}