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

Multiple choice technology programming languages
  1. Yes, Only Concrete constructor

  2. Yes, Only Abstract constructor.

  3. No

  4. I don't know.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. No, and they are not accessible

  2. Yes, but they are not accessible

  3. May be

  4. I don't know.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Public Inheritance

  2. Protected Inheritance

  3. Private Inheritance

  4. Virtual Inheritance

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. nothing wrong will complie fine.

  2. class cannot inherit from struct

  3. struct cannot inherit from class

  4. struct cannot have constructor

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. import sun.scjp.; import static sun.scjp.Color.;

  2. import sun.scjp.Color; import static sun.scjp.Color.*;

  3. import sun.scjp.Color.*;

  4. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Compilation fails.

  2. TestB

  3. TestA

  4. An exception is thrown at runtime

Reveal answer Fill a bubble to check yourself
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'.

Multiple choice technology programming languages
  1. public int a []

  2. static int [] a

  3. private int a [3]

  4. public final int [] a

  5. public [] int a

  6. private int [3] a []

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. public class Thing {}

  2. public class Thing { public Thing(void) { } }

  3. public class Thing { public Thing() { } }

  4. public class Thing { public Thing(String s) { } }

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. The default constructor initializes method variables.

  2. The default constructor has the same access as its class.

  3. The default constructor invoked the no-arg constructor of the superclass.

  4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.

  5. The compiler creates a default constructor only when there are no other constructors for the class.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Explicitly setting myA to null marks that instance to be eligible for garbage collection

  2. Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.

  3. Any call on tryIt() causes the private instance of A to be marked for garbage collection.

  4. There are no instanced of A that will become eligible for garbage collection.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Implementation details of the class are hidden.

  2. Access modifiers can be omitted on class data members.

  3. Internal operation of the class can be modified without impacting clients of that class.

  4. Code that uses the encapsulation class can access data members directly.

  5. Performance of class methods is improved.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology
  1. a) PegaRP-Work-Object-Admin-

  2. b) PegaRP-Data-ProductArea

  3. c) PegaRP-Work-Object-Admin

  4. d) PegaRP-Work-Project

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology
  1. a) Cover

  2. b) Container

  3. c) Parent

  4. d) None of the above

Reveal answer Fill a bubble to check yourself
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.