CPP Object-Oriented Programming Concepts 1 — Questions and Answers
Question 1: Which OOP principle allows a subclass to provide a specific implementation of a method already defined in its superclass?
- Encapsulation
- Abstraction
- Polymorphism (Correct answer)
- Inheritance
Correct answer: Polymorphism
Polymorphism allows subclasses to override superclass methods, enabling different behaviors through the same interface.
Question 2: What does the 'encapsulation' principle primarily aim to achieve in OOP?
- Code reuse via inheritance
- Hiding internal state and requiring all interaction through methods (Correct answer)
- Allowing multiple classes to share one interface
- Enabling runtime method resolution
Correct answer: Hiding internal state and requiring all interaction through methods
Encapsulation bundles data and methods while restricting direct access to an object's internal state.
Question 3: In object-oriented design, what is an abstract class?
- A class with no methods
- A class that cannot be instantiated and may contain abstract methods (Correct answer)
- A class that inherits from multiple parents
- A class whose attributes are all private
Correct answer: A class that cannot be instantiated and may contain abstract methods
An abstract class cannot be instantiated directly and is meant to be subclassed, often containing abstract method declarations.
Question 4: Which concept describes a class implementing more than one interface in Java or C#?
- Multiple inheritance
- Interface segregation
- Multiple interface implementation (Correct answer)
- Composition
Correct answer: Multiple interface implementation
Java and C# allow a single class to implement multiple interfaces, providing multiple type contracts without multiple class inheritance.
Question 5: What is the difference between method overloading and method overriding?
- Overloading changes return type; overriding changes parameter types
- Overloading defines multiple methods with the same name but different signatures; overriding redefines a superclass method in a subclass (Correct answer)
- Overloading only works with constructors; overriding works with all methods
- Overloading is compile-time; overriding is always static
Correct answer: Overloading defines multiple methods with the same name but different signatures; overriding redefines a superclass method in a subclass
Overloading uses the same method name with different parameter lists in the same class, while overriding redefines an inherited method in a subclass.
Question 6: What is the 'Liskov Substitution Principle' (LSP)?
- Subclasses should be replaceable for their base classes without altering program correctness (Correct answer)
- Every class should have only one reason to change
- High-level modules should not depend on low-level modules
- Clients should not be forced to depend on interfaces they do not use
Correct answer: Subclasses should be replaceable for their base classes without altering program correctness
LSP states that objects of a subclass must be usable wherever their parent class is expected without breaking the application.
Which OOP principle allows a subclass to provide a specific implementation of a method already defined in its superclass?