What is 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
Due to virtual function and runtime polymorphism, the Derived class's display() method is called through the Base class pointer, printing 'Derived'.
Q.22Medium
Which of the following statements about abstract classes in C++ is correct?
Answer: B
An abstract class in C++ is defined by having at least one pure virtual function (declared with = 0). Objects cannot be instantiated from abstract classes.
Q.23Medium
What will be the output of the following code?
class A {
public:
A() { cout << "A "; }
};
class B : public A {
public:
B() { cout << "B "; }
};
int main() {
B obj;
}
Answer: A
When a derived class object is created, the base class constructor is called first, then the derived class constructor. Output is 'A B'.
Q.24Easy
In C++, what is the correct syntax for a pure virtual function?
Answer: A
A pure virtual function in C++ is declared with the 'virtual' keyword followed by '= 0'. This makes the function purely virtual, requiring derived classes to implement it.
Q.25Easy
Which access specifier allows a derived class to access members of the base class?
Answer: B
The 'protected' access specifier allows derived classes to access members of the base class while keeping them hidden from the outside world.
Advertisement
Q.26Medium
What is the main difference between method overloading and method overriding?
Answer: A
Method overloading is resolved at compile-time (static polymorphism) with different function signatures, while method overriding is resolved at runtime (dynamic polymorphism) using virtual functions.
Q.27Hard
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?
Answer: A
A virtual destructor ensures that when a derived class object is deleted through a base class pointer, the derived class destructor is called first, preventing memory leaks and ensuring proper cleanup.
Q.28Medium
What does encapsulation in C++ primarily achieve?
Answer: B
Encapsulation bundles data and functions together, hiding implementation details using access specifiers (private, protected, public) and controlling how external code interacts with the object.
Q.29Hard
How is the Diamond Problem solved in C++ using virtual inheritance?
Answer: C
Virtual inheritance ensures that when a class appears multiple times in an inheritance hierarchy, only one copy of it is included, resolving ambiguity and the Diamond Problem.
Q.30Medium
What is the purpose of the 'const' keyword when applied to member functions?
Answer: B
A const member function cannot modify the state of the object (its member variables). It's used to indicate that a function only reads data without changing it.
Q.31Hard
Which of the following is NOT a characteristic of a good object-oriented design?
Answer: C
Good OOP design aims to minimize code duplication through inheritance and composition, not maximize it. High cohesion, low coupling, and proper encapsulation are fundamental principles.
Q.32Hard
Which of the following best describes the Liskov Substitution Principle (LSP)?
Answer: A
LSP states that objects of a derived class should be able to replace objects of the base class without breaking the program. This ensures predictable polymorphic behavior.
Q.33Medium
What happens when you try to call a non-virtual function through a pointer to a base class pointing to a derived object?
Answer: B
For non-virtual functions, the function call is resolved based on the pointer type (base class), not the actual object type. Therefore, the base class version is called.
Q.34Easy
In C++, what is the correct way to implement an interface-like behavior?
Answer: B
In C++, an interface-like behavior is achieved using an abstract class containing only pure virtual functions. Derived classes must implement these functions, enforcing a contract.
Q.35Easy
Which of the following is a key principle of Object-Oriented Programming that bundles data and methods together?
Answer: A
Encapsulation is the bundling of data (attributes) and methods (functions) into a single unit called a class, hiding internal details from the outside world.
Q.36Easy
In C++, what is the default access specifier for members of a class?
Answer: B
In C++, the default access specifier for class members is private, meaning they are not accessible outside the class by default.
Q.37Medium
Which keyword is used to prevent a derived class from further overriding a virtual function in C++?
Answer: B
The 'final' keyword (introduced in C++11) prevents further overriding of a virtual function in derived classes.
Q.38Medium
What is the output of the following code?
class Base { public: void display() { cout << "Base"; } };
class Derived : public Base { public: void display() { cout << "Derived"; } };
Base obj = Derived();
obj.display();
Answer: B
Since 'display()' is not virtual and obj is of type Base, it calls Base::display(). Object slicing occurs when a Derived object is assigned to Base reference.
Q.39Easy
Which of the following correctly describes a pure virtual function in C++?
Answer: B
A pure virtual function is declared with '= 0' and has no implementation in the base class. Derived classes must override it to create a concrete class.
Q.40Easy
In C++, an abstract class is a class that contains at least one _______.
Answer: C
An abstract class must contain at least one pure virtual function, making it impossible to instantiate objects of that class directly.