C++ OOP Concepts MCQ Quiz

Test your knowledge of C++ Object-Oriented Programming concepts with this interactive quiz. Select the correct answer for each question and see immediate feedback.

Medium Level Questions Medium

1

Which of the following is not a pillar of Object-Oriented Programming?

Correct Answer: D) Compilation

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.

2

Which access specifier allows class members to be accessed only by the class itself and its friends?

Correct Answer: C) private

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.

3

What is the relationship between a class and an object?

Correct Answer: B) An object is an instance of a class

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.

4

Which type of inheritance allows a class to inherit from multiple base classes?

Correct Answer: D) Multiple inheritance

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.

5

What is function overriding in C++?

Correct Answer: B) Redefining a base class function in a derived class

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.

6

Which keyword is used to achieve runtime polymorphism in C++?

Correct Answer: B) virtual

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.

7

What is the purpose of a constructor in a class?

Correct Answer: B) To initialize the object's data members

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.

8

Which of the following is true about abstract classes in C++?

Correct Answer: C) They cannot be instantiated directly

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.

9

What is encapsulation in OOP?

Correct Answer: B) Binding data and functions together into a single unit

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.

10

What is the 'diamond problem' in multiple inheritance?

Correct Answer: B) When a derived class has multiple paths to a base class

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.

Advanced Level Questions Advanced

11

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;
}

Correct Answer: B) Derived

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.

12

Which of the following is not a type of constructor in C++?

Correct Answer: D) Static constructor

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).

13

What is the purpose of the 'explicit' keyword for constructors?

Correct Answer: B) To prevent implicit conversions

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.

14

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;
}

Correct Answer: A) 3 3

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.

15

What is the difference between early binding and late binding?

Correct Answer: A) Early binding happens at compile time, late binding at runtime

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.

Your score: 0/15

Keep practicing to improve your C++ OOP knowledge!