C++ Online Quiz


Advertisements


Following quiz provides Multiple Choice Questions (MCQs) related to C++ Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Answer : A

Explaination

As inline function gets expanded at the line of call like a macro it executes faster.

Q 2 - The operator used to access member function of a structure using its object.

A - .

B - ->

C - *

D - None of the above.

Answer : A

Explaination

Just the way we use dot (.) operator to access members of the class, in similar it is used to access the members of the structure too.

Q 3 - In the following program f() is overloaded.

void f(int x) {

}

int f(signed x) { 
   return 1;
}

main() {

}

A - True

B - False

Answer : B

Explaination

No, as both the functions arguments is same and compiler ignores return type to consider overloading though different in return type.

Q 4 - What is the output of the following program?

#include<iostream>

using namespace std;
class Base {
public: 
   virtual void f() { 
      cout<<"Base\n";
   }
};

class Derived:public Base {
public: 
   void f() { 
      cout<<"Derived\n";
   }
};

main() { 
   Base *p = new Derived();
   p->f();

}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : B

Explaination

The overridden method f() of the created object for derived class gets called.

#include<iostream>

using namespace std;
class Base {
public: 
   virtual void f() { 
      cout<<"Base\n";
   }
};

class Derived:public Base {
public: 
   void f() { 
      cout<<"Derived\n";
   }
};

main() { 
   Base *p = new Derived();
   p->f();

}

Q 5 - What is the output of the following program?

#include<iostream>

using namespace std;
main() { 
   int *p = new int; 
   delete p; 
   delete p; 
   cout<<"Done";
}

A - Done

B - Compile error

C - Runtime error

D - None of the above

Answer : C

Explaination

It is invalid to release memory more than once.

#include<iostream>

using namespace std;
main() { 
   int *p = new int; 
   delete p; 
   delete p; 
   cout<<"Done";
}

Answer : B

Explaination

Defining a templated class is defining a generic class. Hence functionality of the class is generalized for several types, if applicable.

Q 7 - What is the output of the following program?

#include<iostream>

using namespace std;
main() {
   char s[] = "hello", t[] = "hello";
   if(s==t)
      cout<<"eqaul strings";
}

A - Equal strings

B - Unequal strings

C - No output

D - Compilation error

Answer : C

Explaination

No output, as we are comparing both base addresses and are not same.

#include<iostream>

using namespace std;
main() {
   char s[] = "hello", t[] = "hello";
   if(s==t)
      cout<<"eqaul strings";
}

Q 8 - What is the size of the following union definition?

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

A - 1

B - 2

C - 4

D - 8

Answer : C

Explaination

union size is biggest element size of it. All the elements of the union share common memory.

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

Q 9 - What is the output of the following program?

#include<iostream>

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

A - 3 2 1 0

B - 3 2 1

C - 3 3 3

D - Compile error

Answer : B

Explaination

As the static variable retains its value from the function calls, the recursion happens thrice.

#include<iostream>

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

Q 10 - i) Exceptions can be traced and controlled using conditional statements.

ii) For critical exceptions compiler provides the handler

A - Only (i) is true

B - Only (ii) is true

C - Both (i) & (ii) are true

D - Both (i) && (ii) are false

Answer : B

Explaination

Conditional statements are used to take alternate actions depending upon certain condition but not multi branching. C++ too provides some critical exception handlers.


cpp_questions_answers.htm

Advertisements