1.
What is the output of the following code
class base{
public : virtual void func1()=0;
void func2() {
cout<<"\n In base, function2"; } }; //end of class
class derived : public base{
public : void fun3() {
cout<<"\n In derived , function3"; } };//end of class
int main(){
derived d;
d.fun3();
return 0; }
Correct Answer
C. Compilation error
Explanation
The code will result in a compilation error because the derived class is not implementing the pure virtual function func1() from the base class. Since func1() is declared as a pure virtual function in the base class, it must be implemented in any derived class that inherits from the base class. Since the derived class does not implement func1(), the code will not compile.
2.
A special library function which is called automatically when no handler at any level catches the
exception
Correct Answer
B. Terminate()
Explanation
The terminate() function is a special library function that is called automatically when no exception handler at any level catches the exception. It is responsible for terminating the program and cleaning up any resources that were allocated.
3.
What is the output of the following code
int main() {
char s1[20]="Hello";
char s2[20]="Hai";
s2=s1;
cout<<"String1 : "<<s1;
cout<<" String2 : "<<s2;
return 0;
}
Correct Answer
C. Error : forbidden assignment of arrays
Explanation
The given code tries to assign one array to another, which is not allowed in C++. This results in a compilation error stating "forbidden assignment of arrays". Arrays cannot be assigned directly to each other in C++, instead, you need to use functions like strcpy or strncpy to copy the contents of one array to another.
4.
class Test{
public:
~Test() {
cout<<" Destructor"; } }; //end of class
int main() {
Test t;
t.~Test();
return 0; }
Correct Answer
B. Destructor Destructor
Explanation
The given code snippet creates an object of the Test class named "t" in the main function. Then, the destructor of the Test class is explicitly called using the syntax "t.~Test();". This results in the destructor being called twice, leading to the output "Destructor Destructor". Therefore, the correct answer is "Destructor Destructor".
5.
class VirtualClass {
int x,y;
public:
VirtualClass() { x=1,y=2; }
virtual void operator<<(ostream&); }; // end of class
void VirtualClass::operator<<(ostream &os)
{ os<<this->x<<"\t"<<this->y; }
int main() {
VirtualClass vcObj;
vcObj<<cout;
return 0; }
Correct Answer
A. 1 2
Explanation
The code defines a class called VirtualClass with two integer variables x and y. The constructor initializes x to 1 and y to 2. The class also overloads the
6.
int main()
{
int a=2,b=3;
if ((a==2) || (b++==4))
cout<<"success";
return 0;
}
What will be the values of a and b ?
Correct Answer
A. 2,3
Explanation
The values of a and b will be 2 and 3 respectively. In the if statement, the first condition (a==2) is true, so the second condition (b++==4) is not evaluated. Therefore, the value of b remains unchanged. Since the if statement is true, the statement "success" is printed.
7.
What is the output of the below code
int main(){
int i=0;
for(i=0;i<20;i++)
switch(i) {
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default: i+=4; }
cout<<i;
return 0; }
Correct Answer
C. 22
Explanation
The code starts with initializing the variable i to 0. Then, it enters a for loop with the condition i
8.
What is the output of the following code
class base{
public : virtual void display()
{ cout<<" \n I am in base-display ";}
void virtual show()
{ cout<<" \n I am in base-show ";
}
virtual void show1(){cout<<"In base show1 ";}
};
class derived : public base
{};
int main(){
derived d;
cout<<sizeof(d)<<"\n";}
Correct Answer
B. 8
Explanation
The output of the code is 8. This is because the sizeof operator is used to determine the size of an object or data type in bytes. In this case, the object "d" is of type "derived", which is derived from the base class. Since the derived class does not have any additional member variables or functions, its size is the same as the base class, which is 8 bytes.
9.
What is the output of the following code
int main(){
int i=4,j;
j=i<<2;
cout<<j;
return 0;}
Correct Answer
D. 16
Explanation
The code assigns the value 4 to the variable i. Then, it performs a left shift operation on i by 2 bits, resulting in 16. Finally, it prints the value of j, which is 16.
10.
#define SQR(x) (x*x)
main()
{
int a,b=3;
a=SQR(b+2);
cout<<a;
return 0;
}
Correct Answer
A. 11
Explanation
The correct answer is 11.
In the given code, the macro SQR(x) is defined as (x*x).
In the main function, the value of b is 3.
The expression a=SQR(b+2) is evaluated as a=(b+2)*(b+2), which becomes a=3+2*3+2, which simplifies to a=11.
Therefore, the value of a is 11, which is outputted by the cout statement.