C++ OOP Concepts MCQ Quiz
Practice 15 basic and 10 tricky MCQs with instant feedback. Continue with the tutorial and interview Q&A links below.
Basic Level (15 Questions)
Which of the following is not a pillar of Object-Oriented Programming?
The four main pillars of OOP are: Inheritance, Polymorphism, Encapsulation, and Abstraction. Compilation is the process of converting source code to machine code and is not an OOP concept.
Which access specifier allows class members to be accessed only by the class itself and its friends?
Private members can only be accessed by the class itself and its friend functions/classes. Protected members can be accessed by derived classes, and public members are accessible from anywhere.
What is the relationship between a class and an object?
A class is a blueprint or template that defines the properties and behaviors, while an object is a specific instance created from that class with its own state.
Which type of inheritance allows a class to inherit from multiple base classes?
Multiple inheritance allows a class to inherit from more than one base class. C++ supports multiple inheritance, though it can lead to complexities like the diamond problem.
What is function overriding in C++?
Function overriding occurs when a derived class provides a specific implementation of a function that is already defined in its base class. This enables runtime polymorphism.
Which keyword is used to achieve runtime polymorphism in C++?
The virtual keyword is used to declare a function as virtual in the base class, which allows derived classes to override it. This enables runtime polymorphism through function overriding.
What is the purpose of a constructor in a class?
A constructor is a special member function that is automatically called when an object is created. Its primary purpose is to initialize the object's data members and allocate resources if needed.
Which of the following is true about abstract classes in C++?
Abstract classes are designed to be base classes and contain at least one pure virtual function (declared with = 0). They cannot be instantiated directly and must be inherited by derived classes that implement all pure virtual functions.
What is encapsulation in OOP?
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit called a class. It also often involves data hiding through access specifiers, but its core definition is the bundling aspect.
What is the 'diamond problem' in multiple inheritance?
The diamond problem occurs in multiple inheritance when a class inherits from two classes that both inherit from the same base class. This creates ambiguity about which path to take to access members of the base class. It's solved using virtual inheritance.
What is the output of this code?
class Base {
public: virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public: void show() { cout << "Derived"; }
};
int main() {
Base* b = new Derived();
b->show();
return 0;
}
Since the show() function is declared as virtual in the base class, the function call is resolved at runtime based on the actual object type (Derived), not the pointer type (Base). This is an example of runtime polymorphism.
Which of the following is not a type of constructor in C++?
C++ does not have a concept of "static constructors." The valid constructor types in C++ are: default constructor, parameterized constructor, copy constructor, move constructor (since C++11), and delegated constructor (since C++11).
What is the purpose of the 'explicit' keyword for constructors?
The explicit keyword prevents the compiler from using that constructor for implicit conversions. It ensures that the constructor can only be used for explicit object creation, avoiding unexpected type conversions.
What is the output of this code?
class Test {
static int count;
int id;
public:
Test() { id = ++count; }
static int getCount() { return count; }
int getId() { return id; }
};
int Test::count = 0;
int main() {
Test t1, t2, t3;
cout << Test::getCount() << " " << t3.getId();
return 0;
}
The static member 'count' is shared by all objects and is incremented each time a new object is created. After creating three objects, count becomes 3. The id of t3 is also 3 because it was the third object created.
What is the difference between early binding and late binding?
Early binding (static binding) occurs at compile time where the function call is resolved based on the static type. Late binding (dynamic binding) occurs at runtime where the function call is resolved based on the actual object type, enabled by virtual functions.
Tricky Level (10 Questions)
Encapsulation means?
Private/protected members hide implementation details.
Constructor name must?
Constructor has the same name as the class.
Destructor is called when?
Destructor runs at end of lifetime.
public inheritance keeps public members?
public stays public in derived class.
override keyword prevents?
Compiler checks virtual override signature.
Abstract class can be instantiated?
Cannot create objects of classes with pure virtual functions.
Quick review: pick the best C++ statement (1)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (2)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (3)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (4)
Review the tutorial and Q&A for this topic.