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. Compiler Error

  2. EmpId is 0

  3. EmpId is 100

  4. Exception

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code compiles and runs successfully because empId is a public field in TestInheritance2, and public members are accessible from any package. The subclass TestInheritance_2 directly inherits empId and can access it, so the output is "EmpId is 100". Option A is wrong because public access across packages is valid. Option B is wrong because empId retains its initialized value of 100, not the default 0.

Multiple choice technology programming languages
  1. p= 0

  2. p= 100

  3. Compiler Error

  4. Exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The field 'p' is declared protected in the public class TestInheritance3. Subclasses in other packages inherit protected fields, so TestInheritance_3 can access 'p' directly. The program compiles successfully and prints the value 100.

Multiple choice technology programming languages
  1. Compiler Error

  2. name = TCS

  3. name = null

  4. Exception

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The code fails to compile because the field 'name' has default (package-private) access in TestInheritance4. Default access is restricted to the same package only. Since TestInheritance_4 is in a different package (inheritance1), it cannot access 'name'. Options B and C are wrong because the code won't reach runtime. Option D is wrong because this is a compile-time access violation, not a runtime exception.

Multiple choice technology programming languages
  1. p = 100

  2. Compiler error

  3. p = 0

  4. Exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The code fails to compile because 'p' is protected and TestInheritance_7 is in a different package. While protected members are accessible in subclasses, the access is through a parent class reference (TestInheritance7 T7 = new TestInheritance7(); T7.p). Protected members in different packages can only be accessed through inheritance or subclass references, not parent class instances. Option A is wrong because compilation fails. Option C is wrong for the same reason.

Multiple choice programming languages java
  1. name = JAVA

  2. Compiler Error

  3. Exception

  4. name = null

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Compilation fails due to multiple errors: an incomplete import statement syntax, the package-private class TestInheritance4 being inaccessible from another package, and its default-visibility field 'name' not being inherited across packages.

Multiple choice technology programming languages
  1. Encapsulation

  2. Polymorphism

  3. Overloading

  4. Overriding

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Encapsulation is the OOP concept of wrapping data and code into a single unit (class) while restricting direct access to internal details and exposing functionality only through public methods. Polymorphism, overloading, and overriding relate to method behaviors, not data hiding.

Multiple choice technology programming languages
  1. java.lang.Exception

  2. java.lang.Error

  3. java.lang.Throwable

  4. none of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

In Java, the java.lang.Throwable class is the root class of the Java Exception hierarchy. Both java.lang.Exception and java.lang.Error inherit directly from java.lang.Throwable. Therefore, all exceptions transitively inherit from Throwable.

Multiple choice technology programming languages
  1. TestSup = 100 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

  2. TestSup = 500 ,TestPoly = 500 ,TP = 500 ,TS = 500 ,TSP = 500

  3. TestSup = 100 ,TestPoly = 400 ,TP = 400 ,TS = 100 ,TSP = 500

  4. TestSup = 500 ,TestPoly = 400 ,TP = 500 ,TS = 100 ,TSP = 400

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The variable p is defined as a static class member in the parent class TestSup. Since static fields are shared across all instances and subclasses, modifying p through any reference (TP.p, TS.p, TSP.p) updates the exact same single memory location, resulting in the final value of 500 for all prints.

Multiple choice technology programming languages
  1. 500

  2. 100

  3. 0

  4. Null Pointer Exception

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The subclass TestPolymorphism1 overrides the testPoly() method. The reference variable TSuper is polymorphic and points to an instance of TestPolymorphism1. Calling TSuper.testPoly() invokes the overridden subclass method, which returns an object with p = 500. Printing TSuper.p prints 500.

Multiple choice technology programming languages
  1. tcs

  2. UX

  3. null

  4. Exception

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The testPoly() method returns a new TestPolymorphism2 object with name "UX", but this return value is not assigned to TSuper. TSuper.name remains uninitialized (null) because TSuper itself was never directly modified. The key insight is that calling an overridden method on a polymorphic reference doesn't change the reference itself - only the returned object would have the new value if assigned.

Multiple choice technology programming languages
  1. Compiler Error

  2. p = 200.0, p1 = 30.0

  3. p = 200.0, p1 = 200.0

  4. p = 30.0, p1 = 30.0

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

This code has two compilation errors: (1) variable p1 is used without being declared, and (2) TSU is declared as TestSuperr type, which only has TestPoly(long, float, int), but the call TSU.TestPoly(11, 9.0, 10) passes a double (9.0) which cannot be implicitly narrowed to float. Java requires the reference type's method signatures to match at compile-time, and double-to-float is not an implicit conversion.

Multiple choice technology programming languages
  1. TestSupe = 300 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  2. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 500 ,TSP = 500

  3. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  4. TestSupe = 300 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Static fields are bound to the declared type, not the actual object type - this is called static hiding. TP.p modifies TestPoly1.p (sets to 400), TS.p modifies TestSupe.p (sets to 300), and TSP.p also modifies TestSupe.p (sets to 500) because TSP is declared as TestSupe type. The final values are TestSupe.p=500 and TestPoly1.p=400.

Multiple choice technology programming languages
  1. 0

  2. 1

  3. 2

  4. Compilation fails

  5. It is not possible to know

  6. An exception is thrown at runtime

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

At // doStuff, two CardBoard objects are eligible for garbage collection: c1 and the object originally referenced by c1 before go() was called. The go() method sets its local parameter cb to null, but this does not affect the actual c2 object. After c1 = null, the first CardBoard object becomes eligible. c2 remains referenced.

Multiple choice technology programming languages
  1. The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

  2. When a stack variable goes out of scope it is eligible for GC.

  3. Some reference variables live on the stack, and some live on the heap.

  4. Only objects that have no reference variables referring to them can be eligible for GC.

  5. It's possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.

Reveal answer Fill a bubble to check yourself
C,E Correct answer
Explanation

Reference variables can exist on the stack as local variables or on the heap as instance variables. The garbage collector can be requested using System.gc() or Runtime.getRuntime().gc(). Distractors are incorrect because local variables themselves are not garbage collected, finalize is not guaranteed to run immediately before GC, and islands of isolated objects can be GCed.

Multiple choice technology programming languages
  1. Faster f = Faster.Higher;

  2. Faster f = Better.Faster.Higher;

  3. Better.Faster f = Better.Faster.Higher;

  4. Bigger.Faster f = Bigger.Faster.Higher;

  5. Better. Faster f2; f2 = Better.Faster.Longer;

  6. Better b; b.Faster = f3; f3 = Better.Faster.Longer;

Reveal answer Fill a bubble to check yourself
C,E Correct answer
Explanation

Faster is an enum defined inside class Better. To reference it outside, you need the fully qualified name Better.Faster. Options C and E use correct syntax: Better.Faster is the type, and values are Better.Faster.Higher and Better.Faster.Longer. Options A, B, D are missing the outer class name.