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
-
Single Inheritence
-
Double Inheritence
-
Multiple Inheritence
-
Class Inheritence
A
Correct answer
Explanation
Java supports single class inheritance, meaning a class can only extend one superclass directly. Java does not support multiple inheritance of classes (only multiple inheritance of interfaces through implementation). Double inheritance is not a standard term, and class inheritance is a general concept rather than a type.
-
the new method should return int
-
the new method can return any type of values
-
the argument list of new method should exactly match that of overriden method
-
the return type of new method should exactly match that of overriden method
C,D
Correct answer
Explanation
When overriding a method in Java, the signature (parameter list) must match exactly, and the return type must be the same or a covariant subtype (not changed arbitrarily). Options A and B are incorrect.
-
abstract double area() { }
-
abstract double area()
-
abstract double area();
-
abstract double area(); { }
C
Correct answer
Explanation
An abstract method in Java is declared with the abstract keyword and has no method body (just a semicolon). Option C shows correct syntax. Option A has a body (not abstract), B is incomplete, D has invalid syntax.
-
Thread
-
Runnable
-
Implements
-
None of the above
B
Correct answer
Explanation
In Java, to make a class runnable as a thread, you implement the Runnable interface which contains the run() method. This is the preferred way over extending the Thread class because it allows your class to extend another class if needed. Option A is incorrect because Thread is a class, not an interface you implement.
-
Interface and Class
-
Class and Interface
-
interface and interface
-
class and class
B
Correct answer
Explanation
Map is an interface in Java that defines the contract for key-value mappings, while HashMap is a concrete class implementing that interface. The question asks for HashMap first, then Map.
-
Inner Class
-
Sub class
-
Private Class
-
Anonymous class
D
Correct answer
Explanation
Anonymous classes are declared and instantiated simultaneously, making it impossible to declare explicit constructors. They can only use instance initializers or pass arguments to the superclass constructor.
-
Polymorphism
-
Inheritance
-
Encapsulation
-
Both a &b
A
Correct answer
Explanation
Polymorphism allows one interface to represent a general class of actions, where specific implementations can vary while the interface remains consistent. This is a core OOP principle enabling flexible, reusable code.
-
Method overriding
-
Method overloading
-
Method overwriting
-
None of the above
B
Correct answer
Explanation
Method overloading occurs when a class has multiple methods with the same name but different parameter lists (different number, type, or order of parameters). This allows the same method name to perform different actions based on input.
-
Method overriding
-
Method overloading
-
Method overwriting
-
None of the above
-
constructor
-
Destructor
-
Class
-
method
A
Correct answer
Explanation
The this() keyword is used to call another constructor in the same class, enabling constructor chaining. It must be the first statement in a constructor and helps avoid code duplication when multiple constructors share common initialization logic.
-
Inner Class
-
Sub class
-
Private Class
-
Both a &b
A
Correct answer
Explanation
Classes defined within other classes, including nested classes or local classes inside methods, are collectively referred to as inner classes. Subclasses inherit from parent classes, and private classes refer to access visibility.
-
One
-
Two
-
Three
-
None of the above
D
Correct answer
Explanation
The Cloneable interface is a marker interface that contains NO methods. It indicates that a class permits cloning - the actual clone() method is inherited from Object class. Marker interfaces exist for type-safety and compiler checks only.
-
Wrapper Classes
-
Super Classes
-
Sub Classes
-
None of the above
A
Correct answer
Explanation
Wrapper classes in Java (Integer, Double, Character, Boolean, etc.) encapsulate primitive data types within objects. This allows primitives to be used in contexts requiring objects, such as collections. Super classes and sub classes refer to inheritance relationships, not primitive wrapping.
-
The Void class extends the Class class.
-
The Float class extends the Double class.
-
The System class extends the Runtime class.
-
The Integer class extends the Number class.
D
Correct answer
Explanation
The Integer wrapper class extends the abstract Number class, which is part of Java's type hierarchy for numeric types. Void does not extend Class (Void is final). Float extends Number, not Double. System does not extend Runtime. Only the Integer-Number relationship is correct.
-
initialize instance variables
-
destroy variables
-
both (a) and (b)
-
None of the above
A
Correct answer
Explanation
Constructors are special methods in Java used to initialize the instance variables of an object when it is created. Destructors or garbage collection handle destroying variables, so option b is incorrect.