Challenge your understanding of core object-oriented programming (OOP) principles and terms. This quiz is designed to reinforce key OOP concepts such as encapsulation, polymorphism, inheritance, and abstraction with practical scenarios and clear examples.
Which OOP principle is demonstrated when a bank account class restricts direct access to its balance field, allowing updates only through deposit and withdraw methods?
Explanation: Encapsulation involves hiding the internal state of an object and allowing access only through well-defined interfaces, such as specific methods. Polymorphism refers to objects taking on many forms, which is unrelated to access control. Inheritance is the mechanism of acquiring properties from a parent class, which doesn't directly relate to restricting access. Initialization simply refers to setting values during object creation and does not enforce access restrictions.
If a class Bird extends Animal and another class Parrot extends Bird, which concept best describes this relationship?
Explanation: Inheritance allows classes to derive properties and behaviors from other classes, as shown in Animal, Bird, and Parrot. Aggregation refers to a 'has-a' relationship and not to a chain of derivation. Immutability means an object's state cannot change after creation and is unrelated to class hierarchies. Overloading refers to having multiple methods with the same name but different parameters, which isn't about class relationships.
Which term describes the ability of a function draw() to operate on objects of class Circle or Rectangle and produce different results based on the object type?
Explanation: Polymorphism allows the same function to behave differently for different object types, as with the draw() method for Circle or Rectangle. Encapsulation is about hiding internal state, not varying behavior. Iteration is the repeated execution of code, which is not shown here. Serialization involves converting an object into a format for storage or transfer and does not relate to function behavior differences.
In OOP, what is an advantage of using an abstract class instead of an interface when building a shape hierarchy?
Explanation: Abstract classes can provide default method implementations, which helps code reuse within a hierarchy such as different shapes. Abstract classes do not prevent instantiation by themselves; specific methods must be abstract to do so. They do not support multiple direct inheritance in most languages, so they cannot force it. Guaranteeing fewer coding errors is not an inherent property of abstract classes compared to interfaces.
When a class defines multiple constructors with different parameters, which OOP concept is being applied?
Explanation: Overloading allows a class to have multiple methods (or constructors) with the same name but different parameter lists. Overriding relates to a subclass providing a new implementation for a method defined in its superclass. Inheritance describes the derivation of classes, not how methods are declared. Aggregation represents a 'has-a' relationship and does not involve how methods or constructors are distinguished within a class.