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 web technology
  1. compiletime error at line 1

  2. forces the class car to be declared as abstract

  3. Runtime Exception

  4. None of the above

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

The abstract class vehicle has an abstract method speed() that must be implemented by concrete subclasses. Class car extends vehicle but doesn't implement speed(), which means car must also be declared abstract. Option B is correct - this forces the car class to be declared abstract.

Multiple choice technology web technology
  1. 1

  2. 2

  3. 3

  4. 4

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

In Java, top-level classes can only be public or abstract (and abstract is implicitly public). The transient modifier applies only to fields, not classes. Private and static modifiers can only be used on nested classes, not top-level classes. Therefore, only declaration 1 is valid.

Multiple choice technology programming languages
  1. A. public abstract class Canine { public Bark speak(); }

  2. B. public abstract class Canine { public Bark speak() { } }

  3. C. public class Canine { public abstract Bark speak(); }

  4. D. public class Canine abstract { public abstract Bark speak(); }

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

To solve this question, the user needs to know the syntax for declaring abstract classes in Java.

Option A: This is not a valid declaration of an abstract class, as it tries to define a method called speak that returns a Bark object, without defining the Bark class or interface. Additionally, it does not declare the class as abstract.

Option B: This is a valid declaration of an abstract class, as it declares the class as abstract using the abstract keyword, and defines an abstract method called speak that returns a Bark object. However, it does not provide an implementation for the method, which makes the class abstract.

Option C: This is not a valid declaration of an abstract class, as it tries to declare an abstract method within a non-abstract class. The class must be declared abstract for it to contain abstract methods.

Option D: This is not a valid declaration of an abstract class, as it tries to declare the class as abstract using the abstract keyword after the class name. The correct syntax is to use abstract before the class name.

Therefore, the correct answers are:

The Answer is: B.

Multiple choice technology programming languages
  1. A. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. B. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. C. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

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

In Java, the extends keyword is used for inheritance between compatible types. A class can only extend another class, and an interface can only extend another interface. A class cannot extend an interface (it must use implements), and an interface cannot extend a class, making the other options incorrect.

Multiple choice technology programming languages
  1. static void doStuff(int... doArgs) { }

  2. static void doStuff (int [] doArgs) { }

  3. static void doStuff(int doArgs...) { }

  4. static void doStuff(int... doArgs, int y) { }

  5. static void doStuff(int x, int... doArgs) { }

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

Java varargs (variable arguments) uses '...' syntax. A (int... doArgs) is valid varargs parameter at the end. E (int x, int... doArgs) is valid with required parameter first, varargs last. C (int doArgs...) is invalid - ellipsis must be between type and name. D (int... doArgs, int y) is invalid - varargs must be the last parameter. B is incorrect not because of array syntax but because doStuff(1) and doStuff(1,2) calls: B takes int[] which requires explicit array construction, while varargs handles these calls directly.

Multiple choice technology programming languages
  1. interface Base2 implements Base { }

  2. abstract class Class2 extends Base { public boolean ml() { return true; } }

  3. abstract class Class2 implements Base { }

  4. abstract class Class2. implements Base { public boolean m1() { return (true); } }

  5. class Class2 implements Base { boolean m1( ) { return false; } byte m2(short s) { return 42; } }

Reveal answer Fill a bubble to check yourself
C,D Correct answer
Multiple choice technology programming languages
  1. public abstract class Canine { public Bark speak(); }

  2. public abstract class Canine { public Bark speak() { } }

  3. public class Canine { public abstract Bark speak(); }

  4. public class Canine abstract { public abstract Bark speak(); }

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

For abstract classes in Java: A is invalid - method 'speak()' has no body and no abstract modifier. B is valid - method has empty body (braces), concrete implementation in abstract class is fine. C is invalid - non-abstract class cannot have abstract method. D is invalid - 'abstract' keyword must come before 'class', not after. Only B declares a compilable abstract class.

Multiple choice technology programming languages
  1. "X extends Y" is correct if and only if X is a class and Y is an interface.

  2. "X extends Y" is correct if and only if X is an interface and Y is a class.

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces.

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

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

In Java, the 'extends' keyword is used for class-to-class inheritance and interface-to-interface inheritance. A class extends another class, and an interface extends another interface. When a class implements an interface, we use 'implements', not 'extends'. Options A and B are incorrect because they mix classes and interfaces with 'extends'. Option D is incorrect because 'extends' cannot work across class-interface boundaries.

Multiple choice technology programming languages
  1. static void doStuff(int... doArgs) { }

  2. static void doStuff (int [] doArgs) { }

  3. static void doStuff(int doArgs...) { }

  4. static void doStuff(int... doArgs, int y) { }

  5. static void doStuff(int x, int... doArgs) { }

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

Java varargs (type...) must be the last parameter and can match zero or more arguments. Option A works because int... can accept single int or multiple ints. Option E works because the required int x matches the first argument, and int... matches the rest. Option B fails because int[] requires an actual array object, not int literals. Option C is invalid syntax - varargs notation goes after the type. Option D fails because varargs must be the last parameter.

Multiple choice technology programming languages
  1. Command Design Pattern

  2. Template Method Design Pattern

  3. Prototype Design Pattern

  4. Singleton Design Pattern

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

RequestProcessor uses the Template Method design pattern. The process() method defines the skeleton of the request processing lifecycle (processPopulate, processValidate, processActionPerform, etc.), with each step being a separate method that can be overridden. This is classic Template Method pattern - define algorithm structure in base class, let subclasses override specific steps. Command pattern (Option A) is about encapsulating requests as objects.

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

The Clidders.flipper() method is marked final, which means it cannot be overridden in subclasses. When Clidlets attempts to override flipper(), it causes a compilation error. The final keyword prevents method overriding by design.

Multiple choice technology programming languages
  1. public abstract class Frob implements Frobnicate { public abstract void twiddle(String s){} }

  2. public abstract class Frob implements Frobnicate { }

  3. public class Frob extends Frobnicate { public void twiddle(Integer i) { } }

  4. public class Frob implements Frobnicate { public void twiddle(Integer i) { } }

  5. public class Frob implements Frobnicate { public void twiddle(String i) { } public void twiddle(Integer s) { } }

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

An abstract class implementing an interface does not need to implement its methods, making 544165 correct. A concrete class must implement all interface methods; 544168 correctly implements twiddle(String) and overloads it with twiddle(Integer). Other options fail due to syntax errors like abstract methods with bodies or using extends instead of implements.

Multiple choice technology programming languages
  1. BD

  2. DB

  3. BDC

  4. DBC

  5. Compilation fails

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

Compilation fails because Bottom2's constructor implicitly calls the no-argument constructor of its superclass Top via super(). Since Top defines a parameterized constructor, the compiler does not generate a default no-argument constructor, leading to a compilation error.

Multiple choice technology programming languages
  1. Clidlet

  2. Clidder

  3. Clidder Clidlet

  4. Clidlet Clidder

  5. Compilation fails.

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

Clidder.flipper() is private, so it is not visible or inherited by Clidlet. The Clidlet class can declare its own flipper() method without any conflict - it's not overriding, just defining a new method. When main() calls new Clidlet().flipper(), it prints "Clidlet".

Multiple choice technology programming languages
  1. 6

  2. 7

  3. 8

  4. 9

  5. Compilation fails.

  6. An exception is thrown.

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

Minor() calls super(y), invoking Uber(int x). That constructor calls this() to Uber(), which increments y from 2 to 3. Then y *= 2 makes y = 6. Back in Minor(), y += 3 makes y = 9. The static variable y holds these values across constructor calls.