Test your understanding of object-oriented design essentials, including encapsulation, composition over inheritance, and interfaces/polymorphism. This quiz covers foundational concepts with clear examples, perfect for beginners and those refreshing their OOP knowledge.
Which of the following best describes encapsulation in object-oriented design?
Explanation: Encapsulation refers to hiding the internal state and requiring all interaction to be performed through an object's methods, thus only exposing what is necessary. Inheriting properties is inheritance, not encapsulation. Combining classes is not a feature called encapsulation and might cause confusion. Making objects abstract refers to abstraction, not encapsulation.
If a Student class keeps its 'grade' variable private and provides public methods to update or view the grade, what concept is being applied?
Explanation: Encapsulation is used here by making 'grade' private and only accessible through controlled methods. Inheritance is about creating a subclass, which isn't shown. Polymorphism allows different object behaviors via a common interface, but that's not the key idea here. Instantiation is the process of creating an object from a class.
Why is encapsulation considered beneficial in object-oriented design?
Explanation: Encapsulation restricts direct access to object data, improving security and preventing unintended modifications. Dynamic method resolution is related to polymorphism. Inheriting methods describes inheritance, not encapsulation. Uniqueness of objects is unrelated to encapsulation.
Which statement best describes composition in object-oriented design?
Explanation: Composition means that a class includes instances of other classes as member variables, thus reusing code by combining object capabilities. A blueprint for another class refers to inheritance, not composition. Implementing an interface is interface-related. Exposing private data is the opposite of encapsulation and not composition.
Why is composition often preferred over inheritance in software design?
Explanation: Composition allows objects to be composed of different, interchangeable parts, leading to flexible code that can change behavior without modifying the class hierarchy. Forcing method overrides is not a benefit of composition. Eliminating public methods is incorrect, as public methods can exist in both patterns. Faster execution speed is not guaranteed by either approach.
A Car class includes an Engine object as one of its fields. This is an example of which design principle?
Explanation: Including an Engine within a Car as a member variable is the essence of composition, representing a 'has-a' relationship. Inheritance would involve Car extending Engine, but that is not correct here. Aggregation is similar to composition, but composition implies strong ownership. Polymorphism deals with different forms, not composition.
What is the main purpose of inheritance in object-oriented design?
Explanation: Inheritance lets a subclass use the methods and attributes of its parent class, promoting code reuse. Data prevention is about encapsulation. Communication between unrelated classes is typically handled by interfaces or methods, not inheritance. Storing multiple values is not related to inheritance.
What is polymorphism in the context of object-oriented design?
Explanation: Polymorphism allows objects from different classes to be treated through the same interface, with each class providing its own implementation. Hiding properties is encapsulation. Multiple variables with the same name is not related and is poor practice. Public variables in a class are unrelated to polymorphism.
Why are interfaces used in object-oriented design?
Explanation: Interfaces describe a set of method signatures that classes must implement, ensuring consistency. Direct private access breaks encapsulation. Storing data by value or reference is unrelated to interfaces. Limiting object creation is achieved by other patterns, not interfaces.
Suppose you have two classes, Bird and Airplane, both implementing a method called fly() from an interface. What feature does this illustrate?
Explanation: Both Bird and Airplane can be treated as objects with the ability to fly, despite different implementations, demonstrating polymorphism. Aggregation is about relationships between objects, not shared behavior. Inheritance implies a parent class, which is not mentioned. Instantiation is the process of creating an object and is not illustrated here.
Which statement correctly compares composition and inheritance?
Explanation: Composition lets you build complex systems from simpler, interchangeable components, which often results in greater modularity and flexibility. Inheritance is not always preferred, as it can create rigid relationships. Composition supports code reuse by combining functionality. Inheritance can sometimes increase code duplication if not used carefully.
Which practice is considered a violation of encapsulation?
Explanation: Publicly exposing member variables allows external code to modify internal state directly, breaking encapsulation. Using private methods, providing accessors, and defining interfaces maintain or even enhance encapsulation, not violate it.
What must a class do after declaring that it implements an interface?
Explanation: When a class implements an interface, it must define all the methods declared in that interface. Inheriting properties from another interface is not mandatory, and exposing internal fields goes against encapsulation. Declaring variables with method names does not fulfill interface requirements.
How can changing a component object in a composed class alter the behavior of the main object?
Explanation: With composition, you can swap out component objects to change the main object's behavior dynamically. Changing the parent class requires redefining class relationships and can't happen at runtime. Making variables static does not involve object behavior. Accessing private variables directly breaks encapsulation and is undesirable.
What enables polymorphic method calls in object-oriented design?
Explanation: When multiple classes follow the same contract through interfaces or base classes, objects can be treated uniformly and have their specific implementations called at runtime. Methods with unique names cannot be called polymorphically. The final keyword prevents overriding, thus restricting polymorphism. Static methods belong to the class, not instances, and cannot be polymorphic.
What is one advantage of using interfaces for abstraction in object-oriented design?
Explanation: Interfaces define a set of methods that must be shared, letting various classes be used interchangeably. Interfaces don't force inheritance of implementation, only of method signatures. Giving direct access to data breaks encapsulation. Constructors are still needed for object creation.