IKM - International Knowledge Measurement Object-Oriented Programming Principles Questions and Answers 1 — Questions and Answers
Question 1: A developer creates a `Vehicle` class and then defines `Car`, `Truck`, and `Motorcycle` classes that each derive from the `Vehicle` class. Each of these new classes can now reuse the common methods and properties of the `Vehicle` class. Which object-oriented principle does this scenario best exemplify?
- Encapsulation
- Inheritance (Correct answer)
- Polymorphism
- Abstraction
Correct answer: Inheritance
Inheritance is the mechanism where a new class (subclass or derived class) acquires the properties and behaviors of an existing class (superclass or base class). In this case, `Car`, `Truck`, and `Motorcycle` inherit from `Vehicle`, promoting code reuse.
Question 2: Which of the following best describes the principle of Encapsulation in object-oriented programming?
- Exposing only the essential features of an object while hiding the complex implementation details.
- Allowing an object to take on many forms or have multiple behaviors through a single interface.
- Bundling the data (attributes) and the methods (functions) that operate on the data into a single unit, or class, and restricting access to the internal state. (Correct answer)
- Creating new classes from existing classes to establish a parent-child relationship and promote code reuse.
Correct answer: Bundling the data (attributes) and the methods (functions) that operate on the data into a single unit, or class, and restricting access to the internal state.
Encapsulation is the practice of bundling data and the methods that operate on that data within a single unit, like a class. It also involves restricting direct access to an object's internal state, which is a concept known as data hiding.
Question 3: A program uses a `Shape` class with a method called `draw()`. Several other classes, such as `Circle`, `Square`, and `Triangle`, inherit from `Shape` and provide their own specific implementation of the `draw()` method. When the program calls the `draw()` method on a list of `Shape` objects, the correct `draw()` implementation is executed for each specific shape. This is an example of:
- Polymorphism (Correct answer)
- Composition
- Data Hiding
- Instantiation
Correct answer: Polymorphism
Polymorphism, which means 'many forms', allows objects of different classes to be treated as objects of a common superclass. It enables a single interface (like the `draw()` method) to be used for a general class of actions, with each specific subclass providing its own implementation.
Question 4: Consider a `DatabaseConnection` class. The user of this class only needs to know about methods like `connect()`, `query()`, and `disconnect()`. The complex internal details, such as managing network sockets, handling authentication protocols, and managing data buffers, are hidden from the user. Which OOP principle is most evident here?
- Inheritance
- Polymorphism
- Abstraction (Correct answer)
- Encapsulation
Correct answer: Abstraction
Abstraction is the concept of hiding complex implementation details and showing only the essential features of the object. It focuses on what an object does rather than how it does it. The user interacts with a simplified interface without needing to understand the underlying complexity.
Question 5: In object-oriented design, what is the primary purpose of defining a class with `private` attributes and `public` methods (getters and setters)?
- To ensure that all child classes can directly modify the attributes.
- To allow any part of the program to access the attributes directly for efficiency.
- To implement polymorphism through method overriding.
- To enforce encapsulation by controlling access to the object's internal state. (Correct answer)
Correct answer: To enforce encapsulation by controlling access to the object's internal state.
Making attributes `private` hides them from outside access. Providing `public` methods (getters and setters) creates a controlled interface for interacting with that data. This is the fundamental mechanism for enforcing encapsulation and data hiding, preventing direct, uncontrolled modification of an object's state.
Question 6: Which statement accurately distinguishes Abstraction from Encapsulation?
- Abstraction is about bundling data and methods, while Encapsulation is about hiding implementation details.
- Abstraction is a mechanism for code reuse through inheritance, while Encapsulation is for creating objects.
- Abstraction focuses on showing essential features (the 'what'), while Encapsulation is the mechanism of bundling data and hiding the implementation details (the 'how'). (Correct answer)
- Encapsulation allows an object to behave differently in different contexts, while Abstraction provides a blueprint for objects.
Correct answer: Abstraction focuses on showing essential features (the 'what'), while Encapsulation is the mechanism of bundling data and hiding the implementation details (the 'how').
Abstraction is a design-level concept that focuses on showing only necessary details to the outside world (what an object does). Encapsulation is an implementation-level mechanism that bundles data with the methods that operate on it and hides the internal state to protect it from outside interference (how it does it).
A developer creates a `Vehicle` class and then defines `Car`, `Truck`, and `Motorcycle` classes that each derive from the `Vehicle` class.
Each of these new classes can now reuse the common methods and properties of the `Vehicle` class.
Which object-oriented principle does this scenario best exemplify?