Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”. These objects can contain both data (in the form of fields, often known as attributes) and code (in the form of methods). Here’s a set of 20 MCQs to test your understanding of OOP concepts.
1. Which of the following is NOT one of the core concepts of OOP?
Answer:
Explanation:
While compilation is a step in many programming languages, it is not one of the core concepts of OOP. The main concepts are Inheritance, Polymorphism, Abstraction, and Encapsulation.
2. What is the process of wrapping data and code into a single unit?
Answer:
Explanation:
Encapsulation refers to bundling the data (attributes) and methods (functions) that operate on the data into a single unit or class.
3. Which of these is achieved by using the 'private' access specifier?
Answer:
Explanation:
Using the 'private' access specifier restricts the visibility and accessibility of class members, ensuring that data is hidden from the outside world. This is a form of encapsulation.
4. What do you call the process by which one class can inherit properties of another class?
Answer:
Explanation:
Inheritance allows a class (derived class) to inherit properties and behaviors (methods) from another class (base class).
5. What is the main advantage of abstraction?
Answer:
Explanation:
Abstraction focuses on hiding the internal complexities and showing only the necessary features of an object.
6. In OOP, what is an instance of a class called?
Answer:
Explanation:
An object is an instance of a class, and it represents a real-world entity.
7. Which of these can be overloaded?
Answer:
Explanation:
In OOP, especially in languages like Java and C++, methods can be overloaded, which means defining multiple methods with the same name but with different parameters.
8. What specifies a contract that classes must adhere to?
Answer:
Explanation:
An interface defines a contract. Classes that implement an interface must provide implementations for the interface's declared methods.
9. In which relationship is an object of one class a member of another class?
Answer:
Explanation:
Composition is a type of relationship where one class contains an object of another class as a member.
10. Which principle of OOP suggests using the most restricted access level for members?
Answer:
Explanation:
Encapsulation recommends restricting the access to the internals of an object as much as possible. This is achieved by using private or protected access levels.
11. Which concept allows for implementing two or more interfaces by a single class?
Answer:
Explanation:
Multiple Inheritance (though not supported directly in all languages) allows a class to implement more than one interface.
12. What kind of polymorphism is achieved when a class overrides a base class method?
Answer:
Explanation:
Run-time (or dynamic) polymorphism is achieved through method overriding.
13. A blueprint that defines properties and behaviors is called:
Answer:
Explanation:
A class is a blueprint for creating objects and defines attributes and methods.
14. Which of the following terms refers to the hiding of data or methods from outside a class?
Answer:
Explanation:
Data hiding is a fundamental principle of OOP and refers to the concept of preventing access to certain parts of an object from outside the class.
15. Which feature in OOP allows a class to have methods with the same name as those in its parent class?
Answer:
Explanation:
Method overriding allows a subclass to provide a different implementation of a method that is already defined in its superclass.
16. Which concept represents the "IS-A" relationship in OOP?
Answer:
Explanation:
Inheritance establishes an "IS-A" relationship between the base class and the derived class.
17. Which of the following terms refers to a real-world representation of an abstraction?
Answer:
Explanation:
An object is a tangible representation of a class, which is essentially an abstraction.
18. If an attribute or method is defined as protected, where can it be accessed?
Answer:
Explanation:
Protected members can be accessed within their own class and by subclasses.
19. What OOP principle ensures that internal details of how an object works are hidden, presenting a clear and consistent external interface?
Answer:
Explanation:
Abstraction hides the complex reality while exposing only the necessary parts.
20. Which concept can be used to model real-world relationships between a whole and its parts?
Answer:
Explanation:
Composition is a strong “whole-part” relationship, where parts do not have a lifecycle outside of the whole.
21. Which of the following is a mechanism by which base class can access overridden functions of its derived classes?
Answer:
Explanation:
Through polymorphism, a base class reference can be used to call an overridden method in a derived class.
22. Constructors in OOP:
Answer:
Explanation:
Constructors are special methods in a class that get called automatically when an object of the class is created. They are mainly used for initialization purposes.