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
  1. It can be any class

  2. No class has access to base

  3. The class must belong to the geometry package

  4. The class must be a subclass of the class Hypotenuse

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

InnerTriangle is a package-private class (no access modifier before 'class'), meaning only classes within the geometry package can access it. Since base is a public field inside InnerTriangle, any class that can access InnerTriangle can also access base. Therefore, only classes in the geometry package can reference the variable base.

Multiple choice technology
  1. Inheritance represents an is-a relationship

  2. Inheritance represents a has-a relationship

  3. Interfaces must be used when creating a has-a relationship

  4. Instance variables can be used when creating a has-a relationship.

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

Inheritance in Java represents an 'is-a' relationship - a subclass 'is a' type of its superclass. Has-a relationships represent composition and are typically implemented using instance variables (e.g., a Car 'has an' Engine). Interfaces are not required for has-a relationships - you use composition directly with instance variables. Options A and D are both correct statements.

Multiple choice technology
  1. Compilation fails because of an error in line 3

  2. Compilation fails because of an error in line 7

  3. Compilation fails because of an error in line 9

  4. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.

  5. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.

  6. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

Reveal answer Fill a bubble to check yourself
F Correct answer
Multiple choice technology
  1. foofoofoofoofoo

  2. foobarfoobarbar

  3. foobarfoofoofoo

  4. foobarfoobarfoo

  5. barbarbarbarbar

  6. foofoofoobarbar

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

Static members are accessed based on the reference type, not the actual object type. Base.FOO prints 'foo' (accessing Base's static variable). Sub.FOO prints 'bar' (accessing Sub's static variable). b.FOO prints 'foo' because b is declared as Base type. s.FOO prints 'bar' because s is declared as Sub type. ((Base)s).FOO prints 'foo' because the cast changes the reference type to Base. The result is 'foobarfoobarfoo'. Static members are not polymorphic - they depend on compile-time type.

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.