OOP & C++ MCQs
Practice MCQs on classes, inheritance, polymorphism, encapsulation, and STL for C-CAT preparation. Essential revision material for aspirants.
Which OOP concept allows a derived class to provide a specific implementation of a method already defined in its base class?
Method overriding lets a derived class redefine a base class method with the same signature, enabling runtime polymorphism.
What is the output of the following code? class Base { public: virtual void show() { cout << "Base"; } }; class Derived : public Base { public: void show() override { cout << "Derived"; } }; int main() { Base *b = new Derived(); b->show(); }
Since show() is virtual, C++ resolves the call at runtime based on the actual object type (Derived), not the pointer type (Base) — this is dynamic dispatch.
Which access specifier allows a class member to be accessed only within the same class and by derived classes, but not from outside?
protected members are accessible within the declaring class and any derived class, but not from outside the class hierarchy.
What is a pure virtual function in C++?
A pure virtual function is declared as 'virtual void func() = 0;' It has no implementation in the base class and forces derived classes to implement it, making the base class abstract.
Which of these best describes constructor overloading?
Constructor overloading means a class can have multiple constructors, differentiated by their parameter lists, similar to function overloading.
What happens if a base class destructor is NOT declared virtual, and a derived object is deleted through a base class pointer?
Without a virtual destructor, deleting a derived object via a base pointer only calls the base destructor, skipping the derived class's cleanup — a common source of memory/resource leaks.
Which C++ feature allows the same operator to behave differently based on the operand types?
Operator overloading lets you redefine how operators like +, -, or == behave for user-defined types (classes/structs).
In C++, what does the 'friend' keyword allow?
A friend function or friend class is granted access to the private and protected members of the class that declares it as a friend, even though it isn't a member itself.
Which type of inheritance can lead to the 'diamond problem' in C++?
The diamond problem occurs in multiple inheritance when a class inherits from two classes that both inherit from a common base, creating ambiguity about which base copy to use — resolved in C++ using virtual inheritance.
What is the primary purpose of C++ templates?
Templates let you write functions and classes that work with any data type, with the actual type substituted at compile time — the foundation of generic programming in C++ (e.g. the STL).
Which STL container stores elements in contiguous memory and provides fast random access via index?
std::vector stores elements contiguously like a dynamic array, giving O(1) random access, unlike std::list (doubly linked list) which only allows sequential access.
What does encapsulation in OOP primarily achieve?
Encapsulation bundles data (attributes) and behavior (methods) together within a class, and uses access specifiers to hide internal implementation details from outside code.
Quiz Complete!
📖 Study first: View Cheat Sheet