'this' is a constant pointer that points to the current object instance, allowing members to reference themselves.
Q.42Medium
Consider a scenario where Class B inherits from Class A, and Class C also inherits from Class A. Class D inherits from both B and C. Which problem does this create?
Answer: C
The Diamond Problem occurs when a class inherits from two classes that both inherit from a common base class, creating ambiguity in the inheritance hierarchy.
Q.43Medium
What is the purpose of a destructor in C++?
Answer: B
A destructor is called when an object is destroyed, allowing cleanup of dynamically allocated memory and other resources.
Q.44Medium
Which of the following is NOT a type of polymorphism in C++?
Answer: C
Memory Polymorphism is not a recognized type of polymorphism in C++. The main types are compile-time (static), runtime (dynamic), and parametric polymorphism.
Q.45Medium
What will be the output of the following code?
class A { public: virtual void func() { cout << "A"; } };
class B : public A { public: void func() { cout << "B"; } };
A *ptr = new B();
ptr->func();
delete ptr;
Answer: B
Since func() is virtual and ptr points to a B object, B::func() is called due to dynamic dispatch, printing 'B'.
Advertisement
Q.46Medium
In C++, what is a friend function?
Answer: B
A friend function is declared using the 'friend' keyword and can access private and protected members of the class, breaking encapsulation when necessary.
Q.47Hard
What is the difference between shallow copy and deep copy in C++?
Answer: B
Shallow copy only copies the pointer references, while deep copy allocates new memory and copies the actual data, preventing dangling pointers.
Q.48Easy
Which of the following best defines abstraction in OOP?
Answer: A
Abstraction is the process of hiding complex implementation details and exposing only the necessary interface to users.
Q.49Medium
In C++11 and later, what does the 'override' keyword ensure when used with virtual functions?
Answer: B
The 'override' keyword (C++11) provides compile-time checking to ensure that a function actually overrides a virtual function from the base class.
Q.50Easy
What is the correct syntax for inheriting from multiple base classes in C++?
Answer: C
Multiple inheritance in C++ uses the syntax 'class Derived : public Base1, public Base2 { };' with appropriate access specifiers.
Q.51Hard
What is a virtual destructor and why is it important?
Answer: A
A virtual destructor ensures that when deleting a base class pointer pointing to a derived object, the derived class destructor is called first, preventing resource leaks.
Q.52Medium
Which of the following statements about constructors in C++ is FALSE?
Answer: B
Constructors do not have a return type, not even void. They are special member functions that automatically initialize objects when created.
Q.53Hard
In C++, what does the 'mutable' keyword do when applied to a class member?
Answer: A
The 'mutable' keyword allows a member variable to be modified even within a const member function or when the object itself is const.
Q.54Hard
What is the primary advantage of using interfaces (abstract classes with pure virtual functions) in C++?
Answer: B
Interfaces (abstract classes) define a contract specifying what methods derived classes must implement, promoting loose coupling and design flexibility.
Q.55Easy
What is the access specifier for class members that are accessible only within the same class?
Answer: C
Private members are accessible only within the same class. Public members are accessible from anywhere, and protected members are accessible in derived classes.
Q.56Easy
In C++, when a derived class object is created, which constructor is called first?
Answer: B
The base class constructor is always called before the derived class constructor to ensure proper initialization of inherited members.
Q.57Easy
What does polymorphism literally mean?
Answer: B
'Poly' means many and 'morph' means form. Polymorphism allows objects to take many forms through method overloading and overriding.
Q.58Medium
In C++, what is the default access specifier for class members?
Answer: C
In C++, the default access specifier for class members is private. For structs, it is public.
Q.59Medium
What is the output of the following code?
class Base { public: void show() { cout << "Base"; } };
class Derived : public Base { public: void show() { cout << "Derived"; } };
int main() { Base *b = new Derived(); b->show(); }
Answer: A
Without the virtual keyword, static binding occurs. The function called is determined by the pointer type (Base*), not the actual object type. Output is 'Base'.
Q.60Medium
Which statement about 'this' pointer is correct?
Answer: B
The 'this' pointer is an implicit pointer that points to the current object. It is automatically available in non-static member functions.