CodeSignal Technical Assessment Object-Oriented Programming 2 — Questions and Answers
Question 1: What is method overriding in OOP?
- Defining multiple methods with the same name but different parameters in the same class
- A subclass providing its own implementation of a method already defined in its superclass (Correct answer)
- Hiding a parent class method using access modifiers in a child class
- Calling a superclass method from within a subclass constructor
Correct answer: A subclass providing its own implementation of a method already defined in its superclass
Method overriding lets a subclass supply a specific implementation for a method defined in its parent class, changing the behavior for that subtype.
Question 2: What is method overloading?
- A subclass replacing a parent class method at runtime
- Defining multiple methods with the same name but different parameter lists within the same class (Correct answer)
- Calling a parent class method explicitly from a subclass
- A method that calls itself to solve a problem recursively
Correct answer: Defining multiple methods with the same name but different parameter lists within the same class
Method overloading allows a class to have multiple methods sharing a name, differentiated by their parameter count, types, or order.
Question 3: Which access modifier restricts a class member to be accessible only within its own class?
- public
- protected
- private (Correct answer)
- internal
Correct answer: private
`private` is the most restrictive access level, preventing any access to the member from outside the class where it is declared.
Question 4: What is an interface in object-oriented programming?
- A concrete class that provides default behavior for all its methods
- A contract specifying methods a class must implement, without providing implementation details (Correct answer)
- A way to achieve multiple concrete inheritance through class extension
- A special constructor used exclusively by abstract classes
Correct answer: A contract specifying methods a class must implement, without providing implementation details
An interface defines a contract of method signatures that any implementing class must fulfill, enabling loose coupling and polymorphism.
Question 5: Which statement best describes the difference between composition and inheritance?
- Composition uses `extends` while inheritance uses `implements`
- Inheritance models an 'is-a' relationship while composition models a 'has-a' relationship (Correct answer)
- Composition is only available in functional languages; inheritance is exclusive to OOP
- Inheritance enables code reuse while composition does not
Correct answer: Inheritance models an 'is-a' relationship while composition models a 'has-a' relationship
Inheritance expresses that one type 'is a' subtype of another (Dog is an Animal), while composition expresses that one type 'has a' component of another (Car has an Engine).
Question 6: What defines a static method in OOP?
- A method that cannot be overridden by any subclass
- A method that belongs to the class itself rather than to any specific instance (Correct answer)
- A method that takes no parameters and returns no value
- A method whose return value never changes regardless of input
Correct answer: A method that belongs to the class itself rather than to any specific instance
Static methods are associated with the class, not with individual objects; they can be called using the class name without creating an instance.
Question 7: Which SOLID principle states that software entities should be open for extension but closed for modification?
- Single Responsibility Principle
- Liskov Substitution Principle
- Open/Closed Principle (Correct answer)
- Dependency Inversion Principle
Correct answer: Open/Closed Principle
The Open/Closed Principle says code should allow new functionality to be added (open for extension) without altering existing tested code (closed for modification).
What is method overriding in OOP?