Computer Knowledge
Object-Oriented Programming
2,686 Questions
Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.
Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects
Object-Oriented Programming Questions
-
A getter function
-
A setter function
-
A constructor
-
An overridden method
B
Correct answer
Explanation
A mutator method, commonly known as a setter, is used to modify the state of an object by changing the value of its private fields.
-
Always
-
Never
-
Not necessarily
-
All of the above
C
Correct answer
Explanation
Having multiple constructors is called constructor overloading. It is perfectly valid as long as the constructors have different parameter lists (different signatures).
-
Polymorphism
-
Abstraction
-
Aggregation
-
Inheritance
A
Correct answer
Explanation
Polymorphism allows objects to be treated as instances of their parent class, enabling different classes to provide their own specific implementation of a method defined in a common interface or superclass.
-
Only its own class can access it
-
Any class can access it
-
Only its package can access it
-
Only its subclasses can access it
A
Correct answer
Explanation
The 'private' access modifier restricts visibility so that the member can only be accessed within the class where it is declared.
-
Encapsulation
-
Abstraction
-
Inheritance
-
Overloading
A
Correct answer
Explanation
Encapsulation is the bundling of data and methods that operate on that data into a single unit, and restricting direct access to some of the object's components to protect the internal state.
-
Aggregation
-
Dependency
-
Inheritance
-
Overloading
B
Correct answer
Explanation
A dependency represents a temporary relationship where one class uses another class, often as a parameter in a method or a local variable.
-
Aggregation
-
Dependency
-
Inheritance
-
Overloading
A
Correct answer
Explanation
Aggregation represents a 'has-a' relationship where one class contains another, but the contained object can exist independently of the container.
-
The process of negating methods from a super class.
-
The process of restoring orphaned objects.
-
The process of de-allocating memory automatically.
-
The process of creating a new object.
C
Correct answer
Explanation
Garbage collection in Java is an automatic memory management process that identifies and discards objects that are no longer needed by the application, thereby freeing up memory resources. This prevents memory leaks without requiring manual de-allocation by the developer.
B
Correct answer
Explanation
In the Java Collections Framework, java.util.Map is a self-contained interface that models key-value mappings and does not extend the java.util.Collection interface. In contrast, List, Set, and Queue all extend Collection.
-
The interface that all java collections implement
-
A class filled with static methods used to manipulate collections
B
Correct answer
Explanation
java.util.Collections is a utility class in Java that consists entirely of static methods that operate on or return collections (such as sorting, searching, or making them thread-safe). The interface that collection classes implement is java.util.Collection (singular).
A
Correct answer
Explanation
In Java, you can declare a class as abstract even if it contains only concrete methods (methods with implementations) and no abstract methods. This is typically done to prevent the class from being instantiated directly, forcing users to subclass it.
B
Correct answer
Explanation
Private members of a class are only accessible within the class in which they are declared. Even though a subclass inherits all members of its superclass, it cannot directly access private fields or methods. Instead, it must use public or protected getter and setter methods to interact with them.
-
Method with different number or type of parameters
-
When the JVM overrides your calls for Garbage Collection.
-
None of the Above
-
Same method signature but different implementation
D
Correct answer
Explanation
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The overriding method must have the exact same name, return type, and parameter list (signature) as the method in the parent class. This allows for runtime polymorphism.
B
Correct answer
Explanation
The Serializable interface is a marker interface, which means it does not contain any methods or fields. Since there are no methods defined in the interface, a class that implements Serializable does not need to implement any methods.