Computer Knowledge
Object-Oriented Programming
2,239 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
-
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.
-
When the class has no other constructors
-
When you define at least one constructor with at least one parameter
-
When you define at least one constructor
-
none of the above
A
Correct answer
Explanation
The Java compiler automatically provides a default, no-argument constructor only if no other constructors are explicitly defined in the class. If you write any constructor yourself, whether it has parameters or not, the compiler will not generate the default constructor.
B
Correct answer
Explanation
Instance variables belong to a specific instance (object) of a class. Each object has its own separate copy of instance variables in memory, so modifying the instance variables of one object has no effect on those of another object.
A
Correct answer
Explanation
Inheritance represents an 'is-a' relationship between classes. If class B is a specific example of class A (for example, a Dog is an Animal), then class B can inherit from class A to reuse and extend its functionality.
-
new
-
create
-
construct
-
abracadabra
A
Correct answer
Explanation
In Java, the 'new' keyword is used to instantiate a class and allocate memory for a new object on the heap.
-
myClass
-
The_Classiest_Class
-
MyClassyClass
-
aniketsclasstoendallclasses
C
Correct answer
Explanation
Java class naming conventions dictate using UpperCamelCase (PascalCase), where the first letter of each word is capitalized, and no underscores or special characters are used. 'MyClassyClass' perfectly adheres to this standard.
-
Has a specific function
-
Has a general function
A
Correct answer
Explanation
In biology, specialized cells or tissues are those that have developed unique structures to perform a specific function. Unlike general cells, they are highly adapted to carry out one particular job efficiently.