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

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. Flip a Clidlet

  2. Flip a Clidder

  3. Flip a Clidder Flip a Clidlet

  4. Flip a Clidlet Flip a Clidder

  5. Compilation fails

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

A method marked as final cannot be overridden in subclasses. The parent class Clidders declares flipper() as final, but the child class Clidlets attempts to override it with its own flipper() method. This violates Java's rules for final methods, causing a compilation error.

Multiple choice technology programming languages
  1. Flip a Clidlet

  2. Flip a Clidder

  3. Flip a Clidlet Flip a Clidder

  4. Flip a Clidder Flip a Clidlet

  5. Compilation fails.

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

Private methods are not visible to subclasses and therefore cannot be overridden. The Clidder class has a private final flipper(), but since it's private, Clidlet can define its own public flipper() method without any conflict. This is not overriding but defining a new method. The code compiles and outputs 'Flip a Clidlet'.

Multiple choice technology programming languages
  1. MyOuter.MyInner m = new MyOuter.Mylnner();

  2. MyOuter.MyInner mi = new MyInner();

  3. MyOuter m = new Myouter(); MyOuter.MyInner mi = m.new Myouter.MyInner();

  4. MyInner mi = new MyOuter.MyInner();

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

MyInner is a static nested class. To instantiate it from outside MyOuter, we use the syntax MyOuter.MyInner m = new MyOuter.MyInner();. Option A has a typo ('Mylnner' with an 'l' instead of 'I' in the constructor call), but conceptually it represents this correct instantiation syntax.

Multiple choice technology programming languages
  1. I am in Sub 144670

  2. Compiler Error

  3. I am in Super 144670

  4. Exception

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

Overriding cannot reduce visibility. The superclass has default (package-private) access, while the subclass tries private. This violates Java's overriding rules, causing a compiler error.

Multiple choice technology programming languages
  1. p in Super =2.0

  2. p in Sub =2.0

  3. Compiler error

  4. Exception

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

The subclass narrows the thrown exception from Exception to ArithmeticException, which is allowed because ArithmeticException is a subclass of Exception. Due to polymorphism, the subclass version runs, printing p in Sub.

Multiple choice technology programming languages
  1. I am in Sub 144670

  2. I am in Super 144670

  3. Compiler error

  4. Exception

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

The subclass widens access from default to public, which is valid in overriding. When calling through a superclass reference pointing to a subclass object, Java uses the actual object's type (Sub), so the subclass version executes.

Multiple choice technology programming languages
  1. Compiler Error

  2. I am Sub

  3. I am Super

  4. Exception

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

A method marked final cannot be overridden. The superclass declares testSuper() as final, but the subclass attempts to override it. This is a compiler error.

Multiple choice technology programming languages
  1. Compiler Error

  2. Exception

  3. Sub Add 10

  4. Super Add 10

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

Overriding requires identical method signatures including return type. Changing return type from int to long with the same parameters is not a valid override in Java. This causes a compiler error.