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
-
this
-
super
-
this()
-
super()
C
Correct answer
Explanation
In Java, this() is used to call one constructor from another constructor in the same class. It must be the first statement in the constructor body. 'this' alone refers to the current object instance, while 'super' and 'super()' are used for parent class references and constructors respectively.
A
Correct answer
Explanation
Let C be classes and M be methods per class. Total = CM. Also Total = (C-2)(M+1). CM = CM + C - 2M - 2 => C - 2M = 2. For C=8, M=3 (Total 24). If C=6, M=4 (Total 24). This fits the condition.
-
Adapter
-
Bridge
-
Composite
-
Strategy
B
Correct answer
Explanation
The Bridge pattern is specifically designed to decouple an abstraction from its implementation, allowing both to vary independently. This is useful when you want to avoid a permanent binding between an abstraction and its implementation. Adapter converts interfaces, Composite composes objects into tree structures, and Strategy defines interchangeable algorithms.
-
A class
-
A design pattern
-
Both A and B
-
None of These
-
Memento
-
Command
-
Builder
-
Proxy
D
Correct answer
Explanation
The Image2 class acts as a proxy, creating RealImage lazily (only on first use) and then delegating calls. This avoids loading expensive resources until needed and reuses the loaded object for subsequent calls, classic Proxy pattern.
-
boolean
-
Boollean
-
Bolean
-
Boolean
D
Correct answer
Explanation
Boolean is the correct wrapper class name for the primitive boolean in Java. Option A is the primitive type (not wrapper), while options B and C are misspelled. Java wrapper classes follow specific capitalization rules.
-
Metaclass
-
Superclass
-
Object
-
None
C
Correct answer
Explanation
In Java, the 'java.lang.Object' class is the root of the class hierarchy. Every class in Java, whether user-defined or built-in, inherits from Object either directly or indirectly.
-
Import new package
-
use concrete class
-
Use of Interfaces
-
Use Inheritance
C
Correct answer
Explanation
Java does not support multiple inheritance of classes to avoid complexity and the 'diamond problem'. However, it allows a class to implement multiple interfaces, effectively providing a way to achieve the benefits of multiple inheritance.
-
static variable
-
Abstract method
-
Initializer Block
-
Instance variables
A,C
Correct answer
Explanation
In Java, static variables are initialized when the class is loaded, and initializer blocks (both static and instance) run before the constructor body. Static blocks run once at load time; instance blocks run every time an object is created, just before the constructor.
-
constructor
-
public instance variable
-
protected members
-
Primitive elements
A
Correct answer
Explanation
Constructors are not inherited in Java - each class must have its own constructors. When a subclass is instantiated, it calls the superclass constructor (implicitly or explicitly via super()), but it does not inherit the constructor itself. Public instance variables, protected members, and primitive elements are all inherited by subclasses.
B
Correct answer
Explanation
In Java, a source file can contain multiple classes, but only one can be public. The public class name must match the filename. Non-public classes can have any access modifier, making the original statement false.
B
Correct answer
Explanation
Class variables (static variables) can be accessed using the class name (e.g., ClassName.variableName) or via an instance of the class (e.g., instanceName.variableName). While using the class name is preferred for clarity, it is not the 'only' way.
A
Correct answer
Explanation
When overriding a method, the overriding method cannot throw broader (checked) exceptions than the overridden method. It can throw the same exceptions, a subset, or subclasses of those exceptions, maintaining the contract established by the parent method.
A
Correct answer
Explanation
A final class cannot be subclassed, which means none of its methods can ever be overridden. Since overriding is the primary reason for declaring individual methods as final, all methods in a final class are implicitly final, making this statement true.
-
only 1
-
2
-
No need of void main
-
infinite
A
Correct answer
Explanation
A Java class can have only one main method with the signature public static void main(String[] args). This is the entry point that the JVM looks for when executing the class. Overloading main with different parameters is technically possible but only the standard signature serves as the entry point.