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. 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 only to an error on line 3 and 5.

  6. Compilation fails due only to an error on line 3,4 and 5.

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

All lines compile correctly. Line 3's array syntax 'int[] a []' is valid (array of arrays). Line 4 can assign a long array to an Object reference (upcasting). Line 5 assigns a String array to an Object array reference (covariant arrays are allowed in Java).

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

  6. An exception is thrown at runtime.

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

Line 10 attempts 'e2.e = el' but 'el' is undefined (typo for 'e1'). Since e2 was set to null on line 8, this would cause a NullPointerException. The GC eligibility questions become moot because the code throws an exception before completing.

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

Since Faster is a nested enum inside the class Better, it must be referenced using its outer class name. Thus, Better.Faster is the correct type, and Better.Faster.Higher or Better.Faster.Longer are the correct value references. Extra whitespace in Better. Faster is ignored by the compiler, making both selected options valid.

Multiple choice technology programming languages
  1. pre b1 b2 r3 r2 hawk

  2. pre b2 b1 r2 r3 hawk

  3. pre b2 b1 r2 r3 hawk r1 r4

  4. r1 r4 pre b1 b2 r3 r2 hawk

  5. r1 r4 pre b2 b1 r2 r3 hawk

  6. Compilation fails.

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

Java initialization order: static blocks execute top-to-bottom when class is loaded, then for each object: instance initializers (top-to-bottom) then constructor. The trace shows: Bird static (prints nothing), then Raptor static (r1, r4), then 'pre', then Hawk() calls Raptor() which calls Bird(): Bird instance initializer (b1), Bird constructor (b2), then back to Raptor instance initializer (r3), Raptor constructor (r2), then 'hawk'. Result: r1 r4 pre b1 b2 r3 r2 hawk.

Multiple choice technology architecture
  1. They can be applied to both data & methods

  2. They must precede a class's data variables or methods

  3. They can follow a class's data variables or methods

  4. They can appear in any order

  5. They must be applied to data variables first and then to methods

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

Access modifiers (public, private, protected) must appear BEFORE the variable's type or the method's return type in Java declarations. The syntax is: [modifier] type name = value. Option A is true but B is more specific about placement. Options C and E are wrong - modifiers cannot follow the declaration. Option D is wrong - order matters.

Multiple choice technology programming languages
  1. A collection of method declarations and constant values

  2. An abstract base class

  3. A way to get multiple inheritance

  4. A way to inherit variables and method implementations

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

An interface in Java is a collection of abstract method declarations and constant values. It defines a contract without implementation - classes implement interfaces to provide specific behavior while achieving a form of multiple inheritance of type.

Multiple choice technology programming languages
  1. Late binding

  2. Early binding

  3. Java developers are very introspective people

  4. Reflection is possible in all languages

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

Reflection is possible in Java because of late binding - method and field references are resolved at runtime rather than compile time. This allows programs to inspect and modify their own structure and behavior dynamically.

Multiple choice technology programming languages
  1. The Java language has loose type checking

  2. The Java language has tight type checking

  3. To keep tabs on garbage collection

  4. You never know how many you need, so you should always cover the most obvious cases

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

Because Java is a strongly (or tightly) typed language, methods cannot accept arbitrary types without explicit definitions. To allow assertions on different data types (like integers, booleans, floats, and objects), the Assert class must provide multiple overloaded implementations of its assertion methods.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, Throwable is the ultimate superclass of all errors and exceptions. While Exception is the superclass of all checked and unchecked exceptions, it inherits from Throwable. Therefore, saying Exception is the absolute superclass of all exceptional events is technically false because Throwable sits at the top of the hierarchy.

Multiple choice technology programming languages
  1. True

  2. False

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

The code is illegal because ExceptionA is a checked exception thrown by thrower(). The try block only catches ExceptionB, which is a subclass of ExceptionA. Since the superclass exception ExceptionA is neither caught nor declared in the main method's signature, the code fails to compile.

Multiple choice technology programming languages
  1. In Java, to create an applet, you can't call on functions without defining a class to call them.

  2. To appear on the web, Java must use an applet, and without first defining a class, you won't be painting onto anything.

  3. The drawString function is not defined without the "import" statements at the top.

  4. All of the above.

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

In Java applets, painting requires a proper class structure extending Applet or JApplet to provide the drawing surface. Import statements are needed for graphics classes like Graphics. The drawString method must be called within a method like paint() that belongs to a properly defined applet class. All three reasons explain why a single painting line is insufficient.

Multiple choice technology testing
  1. View the supported properties and methods of any object in an open application

  2. Able to define object name and property

  3. Helps to delete the object from object repository

  4. Define an action on the displayed object

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

Object Spy is a QTP diagnostic tool that allows viewing the supported properties, methods, and operations of any object in an open application. It's used for inspection and understanding object structure, not for defining actions, naming objects, or deleting from repository.

Multiple choice technology programming languages
  1. When a method in a class overrides a method in a superclass

  2. When a method in an interface overrides a method in a superinterface

  3. For overridden constructors

  4. When a method in a class implements a method in an interface

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

Javadoc automatically inherits documentation when a method overrides a superclass method, overrides a superinterface method, or implements an interface method. This prevents redundant documentation. However, constructors cannot be overridden in Java, meaning they cannot inherit comments, making that option incorrect.