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

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

  1. To write programs without functions
  2. To organize code using objects and classes
  3. To replace variables with constants
  4. 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?

  1. Encapsulation
  2. Inheritance
  3. Compilation
  4. Polymorphism

Answer: Compilation

The four pillars of OOP are Encapsulation, Inheritance, Polymorphism, and Abstraction.

3. A class is best described as:

  1. An instance of an object
  2. A blueprint for creating objects
  3. A function
  4. A variable

Answer: A blueprint for creating objects

A class defines the properties and methods that its objects will have.

4. An object is:

  1. A blueprint
  2. An instance of a class
  3. A programming language
  4. 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?

  1. Inheritance
  2. Encapsulation
  3. Compilation
  4. 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?

  1. Polymorphism
  2. Abstraction
  3. Inheritance
  4. 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?

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Answer: Polymorphism

Polymorphism allows one interface to have multiple implementations.

8. What is a constructor?

  1. A loop
  2. A special method used to initialize objects
  3. A variable
  4. 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#?

  1. class
  2. object
  3. new
  4. 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?

  1. Inheritance
  2. Abstraction
  3. Polymorphism
  4. 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?

  1. public
  2. protected
  3. private
  4. default

Answer: private

The private access modifier restricts access to members within the same class only.

12. What is method overriding?

  1. Creating multiple methods with the same name in the same class
  2. Redefining a parent class method in the child class
  3. Calling a constructor twice
  4. 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?

  1. Using multiple classes
  2. Defining multiple methods with the same name but different parameters
  3. Replacing a constructor
  4. 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?

  1. super
  2. self
  3. this
  4. 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?

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. 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?

  1. parent
  2. base
  3. super
  4. 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?

  1. A special type of variable
  2. A collection of method declarations without implementation
  3. A database table
  4. 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?

  1. Inheritance
  2. Compilation
  3. Recursion
  4. Iteration

Answer: Inheritance

Inheritance allows new classes to reuse the properties and methods of existing classes.

19. Which of the following best describes polymorphism?

  1. One class having many constructors
  2. One interface, many implementations
  3. One object having many variables
  4. 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?

  1. Abstraction
  2. Inheritance
  3. Encapsulation
  4. Polymorphism

Answer: Encapsulation

Encapsulation binds data and the methods that operate on it into a single unit (class) while protecting the data.

Take the timed quiz