Which of the following correctly demonstrates constructor overloading?
Answer: A
Constructor overloading allows multiple constructors with different parameter lists. Option A is correct as it has constructors with no parameters and with an int parameter.
Q.2Medium
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 *b = new Derived();
b->display();
}
Answer: B
Due to virtual function and dynamic polymorphism, the Derived class's display() method is called because the actual object is of type Derived, even though the pointer is of type Base.
Q.3Medium
What is the difference between method overloading and method overriding?
Answer: A
Method overloading occurs in the same class with different parameter lists (compile-time polymorphism). Method overriding occurs in derived classes with the same signature (runtime polymorphism).
Q.4Medium
Which of the following is true about abstract classes in C++?
Answer: B
C++ doesn't have a dedicated 'abstract' keyword. Abstract classes are created by defining at least one pure virtual function (virtual func() = 0;). You cannot instantiate abstract classes.
Q.5Medium
Which type of inheritance can lead to the Diamond Problem in C++?
Answer: B
Multiple inheritance can lead to the Diamond Problem where a class inherits from two classes that share a common base. This is solved using virtual inheritance.
Advertisement
Q.6Medium
What is a copy constructor in C++?
Answer: A
A copy constructor takes a reference to another object of the same class and creates a copy of it. Syntax: ClassName(const ClassName &obj);
Q.7Medium
Which of the following demonstrates proper encapsulation?
Answer: B
Proper encapsulation hides data members (private) and provides controlled access through public getter and setter methods.
Q.8Medium
Which of the following is a characteristic of a pure virtual function?
Answer: B
A pure virtual function (defined with = 0) must be overridden in derived classes. The base class containing it becomes abstract and cannot be instantiated.
Q.9Medium
What is the main advantage of using interfaces/abstract classes in C++?
Answer: B
Abstract classes define a contract (interface) that derived classes must implement. This ensures consistency and enables polymorphic behavior across different derived classes.
Q.10Medium
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.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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.