AMCAT Object-Oriented Programming Concepts 2 — Questions and Answers
Question 1: Method overriding in OOP occurs when:
- A subclass defines a method with the same name and signature as a method in its superclass (Correct answer)
- A class defines two methods with the same name
- A method calls itself recursively
- A method is declared but not implemented
Correct answer: A subclass defines a method with the same name and signature as a method in its superclass
Method overriding (runtime polymorphism) allows a subclass to provide its own specific implementation of a method already defined in its superclass.
Question 2: What is the primary purpose of a constructor in a class?
- To destroy an object when it goes out of scope
- To initialize an object's state when it is created (Correct answer)
- To define static class-level variables
- To prevent a class from being subclassed
Correct answer: To initialize an object's state when it is created
A constructor is a special method invoked automatically when an object is instantiated, used to set the initial state (field values) of the object.
Question 3: What does the keyword 'this' refer to inside an instance method?
- The parent class of the current object
- The static context of the class
- The current instance of the class (Correct answer)
- The return type of the method
Correct answer: The current instance of the class
'this' is a reference to the current object — the instance on which the method is being called — and is commonly used to resolve naming conflicts between instance variables and parameters.
Question 4: Which access modifier makes a class member accessible only within the class in which it is declared?
- public
- protected
- private (Correct answer)
- default
Correct answer: private
The private access modifier restricts visibility to the enclosing class only, supporting encapsulation by hiding implementation details.
Question 5: What is a static method in OOP?
- A method that can only be called on an object, not the class
- A method that belongs to the class itself rather than any instance (Correct answer)
- A method that cannot have parameters
- A method that is always overridden by subclasses
Correct answer: A method that belongs to the class itself rather than any instance
Static methods belong to the class rather than to any particular object instance, and are called on the class directly (e.g., ClassName.method()).
Question 6: In Java, which keyword is used to call the parent class's constructor from a subclass?
- this()
- parent()
- super() (Correct answer)
- base()
Correct answer: super()
super() calls the parent class's constructor, and it must be the first statement in the subclass constructor when used.
Question 7: Which of the following OOP concepts allows hiding complex implementation details and showing only the essential features?
- Encapsulation
- Polymorphism
- Abstraction (Correct answer)
- Inheritance
Correct answer: Abstraction
Abstraction focuses on exposing only the necessary interface to the user while hiding the underlying complexity, achieved through abstract classes and interfaces.
Method overriding in OOP occurs when: