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
A
Correct answer
Explanation
You can declare a reference variable of an abstract class type. While you cannot instantiate an abstract class directly using new, the reference can point to instances of any concrete subclasses.
-
Nothing
-
Class object
-
class
-
calss reference
B
Correct answer
Explanation
In Java, every class implicitly extends java.lang.Object. Therefore, the superclass of Class is Object. The option text 'Class object' refers to the Object class, which is the root of the class hierarchy.
-
default access
-
public
-
private
-
protected
A
Correct answer
Explanation
Default (package-private) access is the most restrictive that allows classes in the same package to access members. Public allows access from anywhere, private restricts access to the same class only, and protected allows access to subclasses (even in different packages).
-
An Interface can have a variable
-
An Interface can have a public member
-
An Interface can have a static member
-
An Interface can have a final member
A
Correct answer
Explanation
In Java, interfaces CAN have variables, but they must be public, static, and final constants (implicitly all three). The statement 'An Interface can have a variable' is correct if referring to constant fields. Interfaces cannot have instance variables or mutable fields. Options B, C, and D describe what interface members CAN be (public, static, final), so they are not incorrect statements.
B
Correct answer
Explanation
In .NET, classes declared at the namespace level can only use public or internal access modifiers. The Protected modifier is only valid for class members (nested classes, methods, properties), not for top-level class declarations.
B
Correct answer
Explanation
Classes declared directly within a namespace cannot be declared as private in .NET. Namespace-level classes can only be public or internal. The Private modifier is only valid for nested class declarations inside another class.
-
Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables)
-
State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.
-
When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.
-
All of above
D
Correct answer
Explanation
All three statements describe why Java avoids global variables: they break referential transparency, reduce cohesion, and limit reusability by tying code to a single instance. Since each individual reason is valid, the combined choice “All of above” correctly captures the rationale.
-
private
-
protected
-
Both
-
None
A
Correct answer
Explanation
Class.forName is used to load classes dynamically at runtime by passing their fully qualified name as a string. It is a fundamental mechanism for dynamic class loading in Java.
A
Correct answer
Explanation
An abstract method cannot be declared as final in Java because abstract methods are meant to be overridden in subclasses, whereas final methods cannot be overridden. Thus, this combination causes a compilation error.
B
Correct answer
Explanation
In Java, the String class is declared as final, which prevents any class from extending it. Therefore, you cannot inherit from or extend the String class.
-
this() can be used to invoke a constructor of the same class
-
this() will create the object of the same class
-
There is no function called this(). It is a key word.
-
this() will invoke the constructor of subclass
A
Correct answer
Explanation
In Java, this() is a special syntax used inside a constructor to invoke another overloaded constructor within the same class (constructor chaining). It must be the first statement in the constructor. It does not instantiate new objects directly or call subclass constructors.
-
singleton pattern.
-
creational pattern.
-
command design pattern.
-
composite design pattern.
B
Correct answer
Explanation
The Factory Pattern is classified as a creational design pattern because it provides an interface for creating objects without specifying their exact classes. It delegates object instantiation to subclasses or factory methods, promoting loose coupling by letting code depend on abstractions rather than concrete implementations. This is in contrast to structural patterns like composite or behavioral patterns like command.
-
making all constructors private.
-
making all constructors public.
-
removing all constructors.
-
making all constructor static.
A
Correct answer
Explanation
The singleton pattern requires a private constructor to prevent external instantiation. This ensures only one instance can exist. Making constructors public would allow multiple instances, violating the singleton principle.
A
Correct answer
Explanation
In standard object-oriented theory and subtyping relationships, subclassing is reflexive: every class is considered a subclass of itself (a non-strict subclass). However, in languages like Java, a class is not a strict subclass of itself. Given standard OOP type theory, the statement is accepted as True.