What is the purpose of using static members in a class?
Answer: B
Static members belong to the class, not to individual objects. All objects of the class share the same static member, making it useful for counters, constants, etc.
Q.62Medium
Which of the following is an example of compile-time polymorphism?
Answer: B
Function overloading is resolved at compile time. Virtual functions and dynamic casting are examples of runtime polymorphism.
Q.63Hard
What happens when you try to delete a derived class object through a base class pointer without a virtual destructor?
Answer: C
Without a virtual destructor, only the base class destructor is called, leading to incomplete cleanup of derived class resources and potential memory leaks.
Q.64Hard
Consider the code: class A { int x; }; class B : private A { }; class C : public B { };
Can class C access x?
Answer: D
When B inherits privately from A, the protected and public members of A become private in B. C cannot access them because they are not accessible through the inheritance chain.
Q.65Medium
What is the main difference between composition and inheritance?
Answer: A
Composition represents a 'has-a' relationship where a class contains objects of other classes. Inheritance represents an 'is-a' relationship where a class extends another class.
Advertisement
Q.66Hard
In C++, what is the result of dynamic_cast when it fails on a pointer?
Answer: B
When dynamic_cast fails on a pointer, it returns nullptr. For references, it throws a std::bad_cast exception.
Q.67Medium
Which of the following correctly uses pure virtual functions?
Answer: B
Abstract classes with pure virtual functions cannot be instantiated. Derived classes must override pure virtual functions. Option B correctly shows this pattern.
Q.68Hard
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(); }
Answer: A
With virtual functions and dynamic binding, arr[0]->print() calls A's print (outputs 'A'), and arr[1]->print() calls B's overridden print (outputs 'B'). Result: 'AB'.
Q.69Easy
Which C++ feature allows a function to work with objects of different types through a common interface?
Answer: B
Polymorphism enables a function to work with objects of different types through a common base class interface, providing flexibility and extensibility.
Q.70Hard
In a multiple inheritance scenario with diamond problem, how does C++ resolve ambiguity?
Answer: B
The diamond problem occurs when a derived class inherits from two classes that have a common base. Virtual inheritance ensures only one copy of the base class is created.
Q.71Medium
What will be the output of the following code?
class Base { public: virtual void display() { cout << "Base"; } };
class Derived : public Base { public: void display() { cout << "Derived"; } };
int main() { Base *ptr = new Derived(); ptr->display(); }
Answer: B
This demonstrates runtime polymorphism. The virtual function is called based on the actual object type (Derived), not the pointer type (Base), so 'Derived' is printed.
Q.72Medium
Which of the following statements about abstract classes in C++ is true?
Answer: B
An abstract class in C++ must have at least one pure virtual function. It cannot be instantiated directly, but it can have constructors and other member functions.
Q.73Medium
What is the purpose of the 'const' keyword when used with member functions?
Answer: B
A const member function cannot modify any member variables of the object. It guarantees that the function will not alter the state of the object.
Q.74Medium
In the context of operator overloading, which operator cannot be overloaded in C++?
Answer: B
The scope resolution operator (::), member access operator (.), pointer-to-member operator (.*), and ternary operator (?:) cannot be overloaded in C++.
Q.75Medium
What is the output of the following code?
class A { public: int x = 5; };
class B : public A { public: int x = 10; };
int main() { B obj; cout << obj.x; }
Answer: B
The derived class B has its own member variable 'x' with value 10, which shadows the base class member. obj.x refers to B's x, which is 10.
Q.76Easy
Which of the following best describes the concept of function overloading?
Answer: B
Function overloading allows multiple functions to have the same name as long as they differ in number, type, or order of parameters. Return type alone is not sufficient.
Q.77Easy
What is the significance of the 'this' pointer in C++?
Answer: B
The 'this' pointer is an implicit pointer that points to the current object instance. It is used to access members of the current object and to return a reference to the current object.
Q.78Hard
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?
Answer: B
In multiple inheritance, if both base classes have the same method, the derived class must either override it or use the scope resolution operator (A::foo() or B::foo()) to specify which one to call.
Q.79Hard
What is the relationship between references and pointers in C++ OOP context?
Answer: D
Both references and pointers can be used for polymorphism in C++. References are generally safer (no null references, cannot be reassigned) while pointers are more flexible (can be null, reassigned).
Q.80Medium
Which of the following correctly describes function binding in C++?
Answer: C
Virtual functions use dynamic (runtime) binding where the function to be called is determined based on the actual object type at runtime. Non-virtual functions use static (compile-time) binding.