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. b = (Number instanceof s);

  2. b = (s instanceof Short);

  3. b = s.instanceof(Short);

  4. b = (s instanceof Number);

  5. b = s.instanceof(Object);

  6. b = (s instanceof String);

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

Short is a wrapper class and inherits from Number. Options B and D are correct: s instanceof Short checks if s is a Short instance (true), and s instanceof Number checks if s is a Number instance (also true, since Short extends Number). Option A has reversed instanceof syntax. Options C, E use dot notation instead of instanceof keyword.

Multiple choice technology programming languages
  1. Compilation succeeds.

  2. Compilation fails due only to an error on line 3

  3. Compilation fails due only to an error on line 4

  4. Compilation fails due only to an error on line 5

  5. Compilation fails due to errors on lines 3 and 5

  6. Compilation fails due to errors on lines 3, 4, and 5

Reveal answer Fill a bubble to check yourself
A Correct answer
Multiple choice technology programming languages
  1. After line 8 runs

  2. After line 9 runs.

  3. After line 10 runs.

  4. After line 11 runs.

  5. An exception is thrown at runtime.

  6. Never in this program

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

Line 10 attempts e2.e = el, but e2 was set to null on line 8. Accessing e2.e throws NullPointerException. Note: el is a typo (should be e1), but the null dereference happens first.

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. Compilation fails.

  2. An error occurs at runtime.

  3. foobarhi

  4. barhi

  5. hi

  6. foohi

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

new Foo() prints 'foo'. makeBar() instantiates an anonymous subclass of inner class Bar using new Bar() {}, which executes Bar's constructor and prints 'bar'. It then calls go() which prints 'hi', resulting in 'foobarhi'.

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.

Multiple choice technology programming languages
  1. Exception

  2. Compiler Error

  3. I am in Constructor

  4. Message Hello

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

The main method is missing the 'static' keyword. Java requires the main method to be public static void to serve as an entry point - the JVM must be able to call it without instantiating an object. A non-static main method cannot be invoked by the JVM, causing an exception at runtime.

Multiple choice technology programming languages
  1. A class

  2. An object

  3. A method

  4. A data field

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

A class is a blueprint or template that defines the structure and behavior of objects of the same type. An object is a specific instance (option B is wrong). A method is a function defined in a class (option C is wrong). A data field is a single variable within a class (option D is wrong).

Multiple choice technology programming languages
  1. program

  2. class

  3. method

  4. data

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

An object is created as an instance of a class. The class defines the type, and when you instantiate it with 'new', you create an object that belongs to that class type. A program contains many objects, methods are behaviors defined in classes, and data is what objects store.