iGET

C++ Programming - MCQ Practice Questions

C++ MCQs — OOP, STL, pointers, templates for coding rounds & IT exams.

100 questions | 100% Free

Q.1Hard

What is the output of the following code? class A { public: A() { cout << "A "; } ~A() { cout << "~A "; } }; class B : public A { public: B() { cout << "B "; } ~B() { cout << "~B "; } }; int main() { B obj; return 0; }

Q.2Hard

Consider the following code: class Base { protected: int x = 10; }; class Derived : private Base { public: void display() { cout << x; } }; int main() { Derived d; d.display(); }

Q.3Hard

What is the output of operator overloading in the following context? class Complex { public: int real, imag; Complex operator+(const Complex &c) { Complex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } }; Complex c1, c2, c3; c1 = {1, 2}; c2 = {3, 4}; c3 = c1 + c2;

Q.4Hard

Consider the following code: class A { public: virtual ~A() {} }; class B : public A { public: ~B() {} }; int main() { A* ptr = new B(); delete ptr; } What is the significance of virtual destructor here?

Q.5Hard

How is the Diamond Problem solved in C++ using virtual inheritance?

Q.6Hard

Which of the following is NOT a characteristic of a good object-oriented design?

Q.7Hard

Which of the following best describes the Liskov Substitution Principle (LSP)?

Q.8Hard

What is the difference between shallow copy and deep copy in C++?

Q.9Hard

What is a virtual destructor and why is it important?

Q.10Hard

In C++, what does the 'mutable' keyword do when applied to a class member?

Q.11Hard

What is the primary advantage of using interfaces (abstract classes with pure virtual functions) in C++?

Q.12Hard

What happens when you try to delete a derived class object through a base class pointer without a virtual destructor?

Q.13Hard

Consider the code: class A { int x; }; class B : private A { }; class C : public B { }; Can class C access x?

Q.14Hard

In C++, what is the result of dynamic_cast when it fails on a pointer?

Q.15Hard

What will be printed by this code? class A { public: virtual void print() { cout << "A"; } }; class B : public A { public: void print() { cout << "B"; } }; int main() { A *arr[2]; arr[0] = new A(); arr[1] = new B(); arr[0]->print(); arr[1]->print(); }

Q.16Hard

In a multiple inheritance scenario with diamond problem, how does C++ resolve ambiguity?

Q.17Hard

Consider a scenario where class C inherits from both class A and class B, and both A and B have a method foo(). Which statement correctly resolves the ambiguity?

Q.18Hard

What is the relationship between references and pointers in C++ OOP context?

Q.19Hard

Consider the following code: class A { public: virtual ~A() {} }; class B : public A {}; int main() { A *ptr = new B(); delete ptr; }. What does the virtual destructor ensure?

Q.20Hard

What is the key difference between shallow copy and deep copy in the context of copy constructors?