Object-Oriented Design Fundamentals Quiz Quiz

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.

  1. Encapsulation Definition

    Which of the following best describes encapsulation in object-oriented design?

    1. Inheriting properties from a parent class.
    2. Allowing objects to become abstract.
    3. Hiding internal object details and only exposing necessary functionalities.
    4. Combining multiple classes into one.

    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.

  2. Encapsulation Example

    If a Student class keeps its 'grade' variable private and provides public methods to update or view the grade, what concept is being applied?

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

    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.

  3. Primary Benefit of Encapsulation

    Why is encapsulation considered beneficial in object-oriented design?

    1. It allows classes to inherit methods from others.
    2. It enables dynamic method resolution.
    3. It ensures every object is unique at runtime.
    4. It improves data security by restricting direct access.

    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.

  4. Understanding Composition

    Which statement best describes composition in object-oriented design?

    1. A class becomes a blueprint for another class.
    2. A class implements all methods from an interface.
    3. A class contains objects of other classes as fields to reuse their functionality.
    4. A class exposes its private data directly.

    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.

  5. Composition Over Inheritance

    Why is composition often preferred over inheritance in software design?

    1. It provides more flexibility by allowing behavior changes at runtime.
    2. It forces all subclasses to override methods.
    3. It guarantees faster execution speed.
    4. It eliminates the need for public methods.

    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.

  6. Composition Example

    A Car class includes an Engine object as one of its fields. This is an example of which design principle?

    1. Aggregation
    2. Composition
    3. Polymorphism
    4. Inheritance

    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.

  7. Inheritance Purpose

    What is the main purpose of inheritance in object-oriented design?

    1. To allow new classes to acquire the properties and behavior of existing classes.
    2. To enable multiple unrelated classes to communicate.
    3. To prevent data from being accessed by other classes.
    4. To store multiple values in a variable.

    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.

  8. Polymorphism Meaning

    What is polymorphism in the context of object-oriented design?

    1. The use of public variables inside a class.
    2. The ability of different classes to provide a unique implementation of the same method signature.
    3. The act of defining multiple variables with the same name.
    4. The process of hiding class properties from outside access.

    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.

  9. Interface Purpose

    Why are interfaces used in object-oriented design?

    1. To limit the number of objects a class can create.
    2. To define a contract for methods that must be implemented by implementing classes.
    3. To enable direct access to private variables.
    4. To store data by value rather than by reference.

    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.

  10. Interface Example Scenario

    Suppose you have two classes, Bird and Airplane, both implementing a method called fly() from an interface. What feature does this illustrate?

    1. Instantiation
    2. Inheritance
    3. Polymorphism
    4. Aggregation

    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.

  11. Composition vs. Inheritance

    Which statement correctly compares composition and inheritance?

    1. Inheritance prevents code duplication better than composition.
    2. Composition does not support reusing existing code.
    3. Inheritance is always preferred for combining behaviors.
    4. Composition allows for more modular and flexible designs than 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.

  12. Encapsulation Violation

    Which practice is considered a violation of encapsulation?

    1. Using private methods inside a class.
    2. Providing getters and setters for private variables.
    3. Making class member variables public and directly accessible.
    4. Defining interfaces for public behavior.

    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.

  13. Interface Implementation

    What must a class do after declaring that it implements an interface?

    1. Declare new variables matching the interface method names.
    2. Expose all internal fields to public.
    3. Provide concrete implementations for all methods defined in the interface.
    4. Inherit all properties and methods from another 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.

  14. Changing Behavior Using Composition

    How can changing a component object in a composed class alter the behavior of the main object?

    1. By making all variables static in the class.
    2. By replacing the component to provide new functionality at runtime.
    3. By accessing private variables directly.
    4. By changing the class's parent class.

    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.

  15. Polymorphic Method Calls

    What enables polymorphic method calls in object-oriented design?

    1. All classes defining methods with unique names.
    2. Assigning final to method definitions.
    3. Objects of different classes implementing the same interface or inheriting from the same base class.
    4. Using only static methods in classes.

    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.

  16. Abstracting with Interfaces

    What is one advantage of using interfaces for abstraction in object-oriented design?

    1. They eliminate the need for constructors.
    2. They provide direct access to all class data.
    3. They force classes to inherit implementation details.
    4. They allow different classes to be accessed through a common set of methods.

    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.