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 testing
  1. a.) Name of the Class must end with “Test”

  2. b.) Name of the method must start with “test”

  3. c.) Return type of the test method must be “void”

  4. d.) Test method must not have any parameter.

  5. e.) All of the above

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

JUnit framework conventions require: test class names ending with 'Test', test method names starting with 'test', test methods returning void, and test methods having no parameters. All of these conventions (Options A-D) are standard JUnit practices, making Option E correct. These conventions help test runners identify and execute test methods correctly.

Multiple choice technology testing
  1. a.) Reflection provides the option to do this.

  2. b.) Copy the method content into the testcase methods.

  3. c.) Place your tests in the same package as the classes under test.

  4. d.) Can't do the testing for protected methods.

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

Protected methods can be tested by placing test classes in the same package as the classes under test (Option C), giving tests access to package-protected members. Reflection (Option A) is possible but not the recommended approach. Copying method content (Option B) defeats the purpose of testing. Option D is incorrect because protected methods are testable.

Multiple choice technology programming languages
  1. Imports, package declaration, classes

  2. Classes, imports, package declarations

  3. Package declaration must come first; order for imports and class definitions is not

  4. Package declaration, imports, classes

  5. Imports must come first; order for package declaration and class definitions is not

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

Java requires a specific order for top-level elements in a source file: the package declaration (if present) must come first, followed by import statements, and then class/interface/enum definitions. This order ensures the compiler can properly resolve package and import information before processing type definitions. Options A, B, and E have incorrect ordering, while option C incorrectly states that imports and classes can be in any order.

Multiple choice technology web technology
  1. line 1,2,3,4

  2. line 3,4

  3. line 3

  4. line 2,3,4

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

In Java interfaces, all methods are implicitly public and abstract. Explicitly adding modifiers like public (line 2), protected (line 3), or private (line 4) is not allowed. Lines 3 and 4 with protected and private modifiers are invalid. Option B correctly identifies lines 3 and 4 as erroneous.

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.