Object-Oriented Programming (OOP) in C++ is a fundamental paradigm that every developer should be familiar with. OOP concepts, such as classes, objects, inheritance, polymorphism, etc., help in writing clean, modular, and reusable code. In this blog post, let’s challenge your understanding of OOP concepts in C++ with a quiz tailored for beginners.
After each question, you’ll find the correct answer and a brief explanation. Ready to test your knowledge?
1. Which of the following is not an OOP principle in C++?
Answer:
Explanation:
Compilation is the process of converting source code into machine code. It is not a principle of OOP.
2. What is the primary purpose of a constructor in a class?
Answer:
Explanation:
A constructor is a special member function that is invoked when an object of the class is created. Its primary purpose is to initialize the object.
3. How many types of inheritance does C++ support?
Answer:
Explanation:
C++ supports five types of inheritance – Single, Multiple, Multilevel, Hierarchical, and Hybrid inheritance.
4. Which keyword is used to prevent overriding in derived classes?
Answer:
Explanation:
The final keyword can be used to prevent derived classes from overriding a method.
5. Which of the following is not a type of constructor?
Answer:
Explanation:
There is no “Multi constructor” in C++.
6. Which of the following concepts hides the internal data from outside access?
Answer:
Explanation:
Encapsulation is the concept of bundling data (attributes) and methods (functions) into a single unit and restricting access to the data.
7. What is an abstract class?
Answer:
Explanation:
An abstract class cannot be instantiated and is meant to be subclassed. It contains at least one pure virtual function.
8. Which keyword is used to declare a pure virtual function?
Answer:
Explanation:
A pure virtual function is declared using virtual followed by = 0.
9. Which concept allows C++ to implement the “is-a” relationship?
Answer:
Explanation:
The “is-a” relationship is represented through inheritance in OOP.
10. What is the base class in C++ from which all other classes implicitly inherit?
Answer:
Explanation:
Unlike some other object-oriented languages, C++ doesn’t have a universal base class from which all other classes implicitly inherit.
11. Which function cannot be overloaded in C++?
Answer:
Explanation:
Destructors cannot be overloaded as a class can have only one destructor.
12. What does the :: operator represent in C++?
Answer:
Explanation:
The :: operator in C++ is known as the scope resolution operator. It’s used to define a member function outside a class or to access a global variable within the scope where a local variable with the same name exists.
13. What is the default access specifier for members of a class?
Answer:
Explanation:
By default, members of a class are private in C++.
14. Which keyword is used for dynamic polymorphism?
Answer:
Explanation:
Dynamic polymorphism is achieved using virtual functions in C++.
15. Which of the following is correct about the destructor?
Answer:
Explanation:
A destructor is used to release resources before an object is destroyed. It doesn’t have a return type and cannot accept parameters. It’s automatically called when an object goes out of scope.
16. Which of the following cannot be static in a class?
Answer:
Explanation:
Constructors in C++ cannot be declared as static. However, methods, variables, and destructors can be.
17. Which of the following is a mechanism of reusing code in C++?
Answer:
Explanation:
Inheritance allows a class (derived class) to inherit properties and behaviors (methods) from another class (base class), thereby promoting code reuse.
18. What is the process by which one class can acquire the properties and behaviors of another class?
Answer:
Explanation:
Inheritance is the process by which a new class (called a derived class) can acquire the properties and behaviors (methods) of an existing class (base class).
19. Which keyword is used to prevent a class from being inherited?
Answer:
Explanation:
In C++, when a class is declared as final, it cannot be inherited by other classes.
20. When is the memory for a class’s member variables allocated?
Answer:
Explanation:
Memory for a class’s member variables is allocated when an object of the class is declared.