Object-Oriented Programming Quiz
Object-oriented programming organises code around objects. The quiz covers classes and objects, encapsulation, inheritance, polymorphism, abstraction and constructors.
What this quiz covers
- Classes and objects
- Encapsulation and abstraction
- Inheritance
- Polymorphism
Study tip: Learn the four pillars with one everyday example each, such as vehicles or bank accounts.
Questions, answers and explanations
1. What is the main purpose of Object-Oriented Programming (OOP)?
- To write programs without functions
- To organize code using objects and classes
- To replace variables with constants
- To avoid using loops
Answer: To organize code using objects and classes
OOP organizes software into objects created from classes, making code reusable and easier to maintain.
2. Which of the following is NOT a pillar of Object-Oriented Programming?
- Encapsulation
- Inheritance
- Compilation
- Polymorphism
Answer: Compilation
The four pillars of OOP are Encapsulation, Inheritance, Polymorphism, and Abstraction.
3. A class is best described as:
- An instance of an object
- A blueprint for creating objects
- A function
- A variable
Answer: A blueprint for creating objects
A class defines the properties and methods that its objects will have.
4. An object is:
- A blueprint
- An instance of a class
- A programming language
- A compiler
Answer: An instance of a class
An object is a real instance created from a class.
5. Which OOP principle hides internal implementation details from the user?
- Inheritance
- Encapsulation
- Compilation
- Recursion
Answer: Encapsulation
Encapsulation hides data and implementation details while exposing only the required functionality.
6. Which OOP principle allows one class to acquire the properties and methods of another class?
- Polymorphism
- Abstraction
- Inheritance
- Encapsulation
Answer: Inheritance
Inheritance allows a child class to reuse the properties and methods of a parent class.
7. Which OOP concept allows the same method to behave differently for different objects?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Answer: Polymorphism
Polymorphism allows one interface to have multiple implementations.
8. What is a constructor?
- A loop
- A special method used to initialize objects
- A variable
- A compiler
Answer: A special method used to initialize objects
A constructor is automatically called when an object is created to initialize its data.
9. Which keyword is commonly used to create an object in Java, C++, and C#?
- class
- object
- new
- create
Answer: new
The 'new' keyword is commonly used to instantiate an object.
10. Which OOP principle focuses on showing only essential information while hiding unnecessary details?
- Inheritance
- Abstraction
- Polymorphism
- Encapsulation
Answer: Abstraction
Abstraction hides implementation details and exposes only the necessary features.
11. Which access modifier allows a class member to be accessed only within the same class?
- public
- protected
- private
- default
Answer: private
The private access modifier restricts access to members within the same class only.
12. What is method overriding?
- Creating multiple methods with the same name in the same class
- Redefining a parent class method in the child class
- Calling a constructor twice
- Hiding variables
Answer: Redefining a parent class method in the child class
Method overriding allows a child class to provide its own implementation of an inherited method.
13. What is method overloading?
- Using multiple classes
- Defining multiple methods with the same name but different parameters
- Replacing a constructor
- Creating multiple objects
Answer: Defining multiple methods with the same name but different parameters
Method overloading allows methods with the same name but different parameter lists.
14. Which keyword refers to the current object in many OOP languages?
- super
- self
- this
- current
Answer: this
The 'this' keyword refers to the current object instance.
15. Which type of inheritance is NOT supported directly in Java using classes?
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
Answer: Multiple Inheritance
Java does not support multiple inheritance with classes to avoid ambiguity, though it supports it through interfaces.
16. Which keyword is used to call the constructor of the parent class in Java?
- parent
- base
- super
- this
Answer: super
The 'super' keyword is used to access the parent class constructor and members.
17. What is an interface in Object-Oriented Programming?
- A special type of variable
- A collection of method declarations without implementation
- A database table
- A loop structure
Answer: A collection of method declarations without implementation
An interface defines a contract that implementing classes must follow.
18. Which feature of OOP improves code reusability the most?
- Inheritance
- Compilation
- Recursion
- Iteration
Answer: Inheritance
Inheritance allows new classes to reuse the properties and methods of existing classes.
19. Which of the following best describes polymorphism?
- One class having many constructors
- One interface, many implementations
- One object having many variables
- One function calling itself
Answer: One interface, many implementations
Polymorphism allows the same interface or method to perform different actions depending on the object.
20. Which OOP principle combines data and methods into a single unit?
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism
Answer: Encapsulation
Encapsulation binds data and the methods that operate on it into a single unit (class) while protecting the data.