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
-
Yes, Only Concrete constructor
-
Yes, Only Abstract constructor.
-
No
-
I don't know.
A
Correct answer
Explanation
Abstract classes CAN have constructors in C# and other OOP languages. The constructor is called when a concrete subclass is instantiated, as part of the object initialization chain. Option A's wording 'Only Concrete constructor' refers to this - constructors exist but are only invoked through concrete subclass instantiation.
-
No, and they are not accessible
-
Yes, but they are not accessible
-
May be
-
I don't know.
B
Correct answer
Explanation
Private class-level variables ARE inherited by subclasses in OOP, but they are NOT directly accessible. They exist in the subclass's memory layout but cannot be accessed by name - this is called 'inherited but not accessible'. Private members are only accessible within the class that declares them.
-
Use Abstract class
-
Use Private Constructor
-
Both of the above
-
Use Sealed class
-
Public Inheritance
-
Protected Inheritance
-
Private Inheritance
-
Virtual Inheritance
C
Correct answer
Explanation
Private inheritance represents implementation inheritance in C++ because the base class's public and protected members become private in the derived class, hiding the implementation details from external users. This creates an 'is-implemented-in-terms-of' relationship rather than 'is-a' relationship. Public inheritance models interface inheritance (is-a relationship), while protected inheritance is rarely used.
-
nothing wrong will complie fine.
-
class cannot inherit from struct
-
struct cannot inherit from class
-
struct cannot have constructor
A
Correct answer
Explanation
The code compiles without errors. In C++, structs and classes can inherit from each other. They are nearly identical, differing only in default access (public for struct, private for class). Constructors are allowed in structs.
-
import sun.scjp.; import static sun.scjp.Color.;
-
import sun.scjp.Color; import static sun.scjp.Color.*;
-
import sun.scjp.Color.*;
-
import sun.scjp.Color; import static sun.scjp.Color.GREEN;
B,D
Correct answer
Explanation
To use an enum from a different package, you need both: (1) a regular import for the enum class itself, and (2) a static import for the enum constants (either .* for all constants or the specific constant name). Option B imports the class and all constants statically. Option D imports the class and just the GREEN constant specifically.
-
Compilation fails.
-
TestB
-
TestA
-
An exception is thrown at runtime
B
Correct answer
Explanation
This demonstrates Java's dynamic method dispatch (polymorphism). Even though the object is cast to TestA type, the actual object is a TestB instance. At runtime, JVM calls the overridden method in the actual object type (TestB), not the reference type (TestA). The start() method in TestB executes, printing 'TestB'.
-
public int a []
-
static int [] a
-
private int a [3]
-
public final int [] a
-
public [] int a
-
private int [3] a []
A,B,D
Correct answer
Explanation
Valid Java array declarations can place brackets before or after the variable name, but cannot specify array size in the declaration. Option A (public int a []) is valid. Option B (static int [] a) is valid. Option C is invalid - cannot specify [3] in declaration. Option D (public final int [] a) is valid. Option E is wrong syntax ([] must follow type). Option F is invalid - cannot specify [3] in declaration.
-
public class Thing {}
-
public class Thing { public Thing(void) { } }
-
public class Thing { public Thing() { } }
-
public class Thing { public Thing(String s) { } }
A,C
Correct answer
Explanation
To instantiate with new Thing(), the class must have a no-argument constructor. Option A has no explicit constructor, so compiler generates a default no-arg constructor - works. Option C explicitly defines a no-arg constructor - works. Option B is invalid syntax (void is not a valid parameter type). Option D only has a parameterized constructor, so new Thing() won't work.
-
The default constructor initializes method variables.
-
The default constructor has the same access as its class.
-
The default constructor invoked the no-arg constructor of the superclass.
-
If a class lacks a no-arg constructor, the compiler always creates a default constructor.
-
The compiler creates a default constructor only when there are no other constructors for the class.
B,C,E
Correct answer
Explanation
Default constructor behavior: (A) False - it doesn't initialize method variables, only instance variables. (B) True - default constructor has same access modifier as the class (public class gets public constructor). (C) True - it calls super() implicitly, invoking no-arg superclass constructor. (D) False - if any constructor exists, compiler doesn't create default. (E) True - default constructor only when no other constructors defined.
-
Explicitly setting myA to null marks that instance to be eligible for garbage collection
-
Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.
-
Any call on tryIt() causes the private instance of A to be marked for garbage collection.
-
There are no instanced of A that will become eligible for garbage collection.
A,B
Correct answer
Explanation
The private A instance (myA) can only become eligible for GC when the Alpha instance containing it becomes eligible. Option A is misleading - explicitly setting myA to null within tryIt() doesn't happen (the method sets a local parameter to null, not myA itself). Option B is correct - when Alpha is GC'd, its private myA field becomes unreachable and eligible for GC. Option C is false - tryIt() doesn't affect myA's reachability.
-
Implementation details of the class are hidden.
-
Access modifiers can be omitted on class data members.
-
Internal operation of the class can be modified without impacting clients of that class.
-
Code that uses the encapsulation class can access data members directly.
-
Performance of class methods is improved.
A,C
Correct answer
Explanation
Encapsulation provides two key benefits: (A) True - hiding implementation details behind public methods. (C) True - internal changes don't break client code as long as the public interface remains stable. Option B is false - you need access modifiers (private/protected) to encapsulate. Option D is false - clients access via methods, not directly. Option E is false - encapsulation may add slight overhead from getter/setter calls.
-
a) Link-
-
b) Assign-
-
c) Index-
-
d) A & B
B
Correct answer
Explanation
Pega's class hierarchy includes Assign- as a fundamental base class alongside Work- and Data-. The Assign- class is used for assignment objects in workflows.
-
a) PegaRP-Work-Object-Admin-
-
b) PegaRP-Data-ProductArea
-
c) PegaRP-Work-Object-Admin
-
d) PegaRP-Work-Project
A
Correct answer
Explanation
In Pega, classes ending with a dash (-) are abstract classes. 'PegaRP-Work-Object-Admin-' has a trailing dash indicating it is abstract and cannot be directly instantiated. The other options lack this dash notation, making them concrete classes.
-
a) Cover
-
b) Container
-
c) Parent
-
d) None of the above
D
Correct answer
Explanation
The question describes a class that corresponds to a class group with a Data-Admin-DB-ClassGroup instance. This describes a Cover class, but the options include 'None of the above' as correct. This suggests the question may be testing that neither Cover nor Container is the precise terminology in this context.