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. True

  2. False

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

A Java class can be compiled successfully without a main method. The main method is only required if you want to run the class as an application. Classes without main methods can be compiled and used as libraries, or they can be extended/instantiated by other classes that do have main methods. The Java compiler (javac) compiles any valid Java syntax regardless of whether a main method is present. The main method is an entry point for execution, not for compilation.

Multiple choice technology programming languages
  1. B. Foo f= (Delta)x;

  2. A. Alpha a = x;

  3. D. The code on line 31 throws an exception.

  4. C. Foo f= (Alpha)x; D. Beta b = (Beta)(Alpha)x;

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

x is a Beta object. Option B: Foo f = (Delta)x attempts to cast Beta to Delta, which is not valid since Beta cannot be cast to its subclass Delta. This causes ClassCastException. Option A: Alpha a = x is valid upcast. Option C and D are less direct. The claimed answer A (B) is correct.

Multiple choice technology programming languages
  1. B. Foo f= (Delta)x;

  2. A. Alpha a = x;

  3. D. The code on line 31 throws an exception.

  4. C. Foo f= (Alpha)x; D. Beta b = (Beta)(Alpha)x;

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

The variable x is of type Beta. Casting x to Delta (a subclass of Beta) using (Delta)x will fail at runtime with a ClassCastException because the actual object is a Beta, not a Delta.

Multiple choice technology programming languages
  1. Generic collections accept only Object types

  2. Type errors are caught at compile time

  3. Generics result in readily understandable code

  4. Generics sometimes require type casting

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

Generics provide compile-time type checking (B), making code more readable and understandable (C). While generics reduce casting, there are scenarios where casting is still needed (D), such as when working with legacy code or raw types. Option A is incorrect because generic collections are designed to accept specific type parameters, not just Object types.

Multiple choice technology programming languages
  1. Can be used with Generic code

  2. Raw types take Type parameters

  3. Raw types must be cast to appropriate objects at runtime

  4. Type safety

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

Raw types in Java are legacy generic types used without type parameters. They can interoperate with generic code (A is correct) and require explicit casting at runtime (C is correct). They do NOT take type parameters (B is incorrect - that's what makes them 'raw'), and they do NOT provide type safety (D is incorrect - raw types bypass generic type checking). Raw types exist mainly for backward compatibility with pre-Java 5 code.

Multiple choice technology programming languages
  1. Integer

  2. Float

  3. Number

  4. Double

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

In Java generics, the Number type parameter can be used to accept all numeric types (Integer, Float, Double, etc.) because Number is the superclass of all numeric wrapper classes. Option C is correct because Number provides the common base for numeric generics. Options A, B, and D are specific types that would only work with their respective types, not all numeric types.

Multiple choice technology programming languages
  1. <?>

  2. <Object>

  3. <? extends Object>

  4. None of these

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

In Java generics, Object is the implicit upper bound for all type parameters. Every generic type ultimately extends Object, making Object the superclass of all generic objects. The unbounded wildcard > is shorthand for extends Object>, not a superclass itself.

Multiple choice technology platforms and products
  1. Data-

  2. Embed-

  3. Work-

  4. Assign-

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

The Work- class is the base class in Pega PRPC that contains rules governing the behavior and appearance of all work objects. Data- class is for data structures, Embed- is for embedded pages, and Assign- is for assignments. The Work- class hierarchy is fundamental to Pega's case management architecture.

Multiple choice technology programming languages
  1. Color skyColor = BLUE;

  2. Color treeColor = Color.GREEN;

  3. Color purple = new Color( 0xff00ff);

  4. if( RED.getRGB() < BLUE.getRGB() ) {}

  5. Color purple = Color.BLUE + Color.RED;

  6. if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}

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

Option B compiles because enum constants must be qualified with the enum type name (Color.GREEN). Option F compiles because enum constants have an ordinal() method returning their zero-based position. Options A and C fail - enum constants cannot be referenced without the enum name, and enums cannot be instantiated with new. Option D fails because enum constants must be qualified (RED.getRGB). Option E fails because enums cannot be added with + operator.

Multiple choice technology programming languages
    1. Classes in the same package
    1. All classes
    1. Only the class within the package that defined the method
    1. Classes that inherit access
Reveal answer Fill a bubble to check yourself
A,D Correct answer
Explanation

Protected access combines package-level and subclass access. Classes within the same package can access protected members, and subclasses (even in different packages) can also access them via inheritance. Unrelated classes in other packages cannot.

Multiple choice technology programming languages
    1. Change public abstract class Vehicles { } to public class Vehicles { }
    1. Change public class Bus { } to public class Bus extends Vehicles { }
    1. Change public class MyClass { to private class My Class {
    1. Change public abstract class Vehicles { } to protected abstract class Vehicles { }
Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The go() method returns Vehicles but tries to return a Bus instance. This is only valid if Bus is a subclass of Vehicles. Changing Bus to extend Vehicles establishes this inheritance relationship, allowing the return.

Multiple choice technology programming languages
    1. static String [50] sSyntax = new String[];
    1. String [] sSyntax = new String[50];
    1. static String [] sSyntax = new String[50];
    1. static string [] sSyntax = new string[50];
Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

A static array is shared across all instances of a class, while instance arrays are separate for each object. The correct syntax is 'static String [] sSyntax = new String[50];' which declares a static array of 50 String references. Option D incorrectly uses lowercase 'string' - Java is case-sensitive and the type must be 'String'.