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
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.
-
Shared object repository
-
Local object repository
-
Per Action object repository
-
Associated object repository
A
Correct answer
Explanation
In HP QuickTest Professional (QTP/UFT), objects that belong to a shared object repository are displayed in gray to distinguish them from local repository objects.
-
B. Foo f= (Delta)x;
-
A. Alpha a = x;
-
D. The code on line 31 throws an exception.
-
C. Foo f= (Alpha)x; D. Beta b = (Beta)(Alpha)x;
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.
-
B. Foo f= (Delta)x;
-
A. Alpha a = x;
-
D. The code on line 31 throws an exception.
-
C. Foo f= (Alpha)x; D. Beta b = (Beta)(Alpha)x;
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.
-
Generic collections accept only Object types
-
Type errors are caught at compile time
-
Generics result in readily understandable code
-
Generics sometimes require type casting
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.
-
Can be used with Generic code
-
Raw types take Type parameters
-
Raw types must be cast to appropriate objects at runtime
-
Type safety
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.
-
Integer
-
Float
-
Number
-
Double
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.
-
<?>
-
<Object>
-
<? extends Object>
-
None of these
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.
-
Data-
-
Embed-
-
Work-
-
Assign-
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.
-
Alpha a = x;
-
Foo f= (Delta)x;
-
Foo f= (Alpha)x;
-
Beta b = (Beta)(Alpha)x;
-
Color skyColor = BLUE;
-
Color treeColor = Color.GREEN;
-
Color purple = new Color( 0xff00ff);
-
if( RED.getRGB() < BLUE.getRGB() ) {}
-
Color purple = Color.BLUE + Color.RED;
-
if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}
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.
-
- Classes in the same package
-
- All classes
-
- Only the class within the package that defined the method
-
- Classes that inherit access
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.
-
- final
-
- native
-
- local
-
- protected
A
Correct answer
Explanation
The final modifier prevents a method from being overridden by subclasses. native indicates JNI implementation, local is not a valid modifier, and protected is an access specifier that does not prevent overriding.
-
- Change public abstract class Vehicles { } to public class Vehicles { }
-
- Change public class Bus { } to public class Bus extends Vehicles { }
-
- Change public class MyClass { to private class My Class {
-
- Change public abstract class Vehicles { } to protected abstract class Vehicles { }
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.
-
- static String [50] sSyntax = new String[];
-
- String [] sSyntax = new String[50];
-
- static String [] sSyntax = new String[50];
-
- static string [] sSyntax = new string[50];
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'.