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

  2. TestB

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

To solve this question, the user needs to understand inheritance and polymorphism in Java.

The TestB class extends the TestA class and overrides its start method. The main method creates a new instance of TestB and casts it to a TestA object.

The start method is called on this TestA object, but since the object being referred to is actually an instance of TestB, the overridden start method in TestB is called instead of TestA's original start method.

Since TestB's start method prints "TestB" to the console, the resulting output when the program is run will be:

TestB

Therefore, the answer is: B.

Multiple choice technology programming languages
  1. Shape s = new Shape(); s.setAnchor(10,10); s.draw();

  2. Circle c = new Shape(); c.setAnchor(10,10); c.draw();

  3. Shape s = new Circle(); s.setAnchor(10,10); s.draw();

  4. Shape s = new Circle(); s->setAnchor(10,10); s->draw();

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

An abstract class cannot be instantiated directly, making option 1 invalid. Option 2 is invalid because you cannot assign a superclass reference to a subclass variable. Option 3 correctly instantiates the concrete subclass Circle and assigns it to the superclass reference Shape.

Multiple choice technology programming languages
  1. A. double getSalesAmount() { return 1230.45; }

  2. B. public double getSalesAmount() { return 1230.45; }

  3. C. private double getSalesAmount() { return 1230.45; }

  4. D. protected double getSalesAmount() { return 1230.45; }

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

Explanation: To answer this question, we need to understand the concept of abstract classes and abstract methods in Java.

An abstract class is a class that cannot be instantiated on its own and must be subclassed. An abstract method is a method that is declared but does not have an implementation in the abstract class. Any subclass of the abstract class must provide an implementation for the abstract method.

In this code, the Employee class is an abstract class that has an abstract method named getSalesAmount(). The Sales class is a subclass of the Employee class and must provide an implementation for the getSalesAmount() method.

Option A) double getSalesAmount() { return 1230.45; } - This option is incorrect because the method needs to be declared as protected or public to provide implementation for the abstract method in the parent class.

Option B) public double getSalesAmount() { return 1230.45; } - This option is correct because it provides a public implementation for the abstract method in the parent class.

Option C) private double getSalesAmount() { return 1230.45; } - This option is incorrect because a private method cannot override a method in the parent class.

Option D) protected double getSalesAmount() { return 1230.45; } - This option is correct because it provides a protected implementation for the abstract method in the parent class.

Therefore, options B and D are the correct answers.

Multiple choice technology programming languages
  1. . public class Employee extends Info implements Data { public void load() { /do something/ }

  2. public class Employee implements Info extends Data { public void load() { /do something/ }

  3. public class Employee extends Info implements Data { public void load() { /*do something */ }

  4. public class Employee implements Info extends Data { public void Data.load() { /*d something */ }

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

To solve this question, the user needs to understand the difference between an interface and an abstract class in Java. An interface is a contract that specifies a set of methods that a class must implement, while an abstract class can contain both abstract and non-abstract methods and serves as a base class for other classes to inherit from.

Option A is correct because it correctly uses the Data interface and Info class. The Employee class extends the abstract Info class and implements the Data interface, thus fulfilling the requirements of both. The load() method in Employee must be implemented because it is declared in both the Data interface and the Info abstract class.

Option B is incorrect because the order of the interface and extends keywords is wrong. It should be "implements Info extends Data", not "extends Data implements Info". Additionally, the load() method must be implemented because it is declared in both the Data interface and the Info abstract class.

Option C is correct because it correctly uses the Data interface and Info class. The Employee class extends the abstract Info class and implements the Data interface, thus fulfilling the requirements of both. The load() method in Employee must be implemented because it is declared in both the Data interface and the Info abstract class.

Option D is incorrect because it attempts to implement the load() method by prefixing it with the Data interface name, which is not valid syntax. The correct way to implement the load() method is to define it without any prefix, as in options A and C.

Therefore, the correct answer is:

The Answer is: A. public class Employee extends Info implements Data { public void load() { /do something/ }

Multiple choice technology programming languages
  1. public class Circle implements Shape { private int radius;

  2. public abstract class Circle extends Shape { private int radius;

  3. public class Circle extends Shape {

  4. public abstract class Circle implements Shape {

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

Abstract classes use 'extends', not 'implements'. Option B (abstract extending) is valid, and C (concrete class extending) works if it implements draw(). 'implements' with an abstract class (A, D) is invalid.

Multiple choice technology programming languages
  1. go in Goban go in Sente go in Sente

  2. go in Sente go in Sente go in Goban

  3. go in Sente go in Goban go in Goban

  4. D. go in Goban go in Goban go in Sente

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

Sente's go() prints 'go in Sente.'. Goban overrides it to print 'go in Goban'. Stone inherits from Goban but doesn't override go(), so uses Goban's version. Output: go in Sente, go in Goban, go in Goban.

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

  2. Color treeColor = Color.GREEN;

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

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

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

In a static context like main(), enum constants must be accessed via the enum type name. Option B correctly uses Color.GREEN. Option D correctly uses the ordinal() method to compare enum constant positions. Option A fails without the Color. prefix, and C fails because enums cannot be instantiated with new.

Multiple choice technology programming languages
  1. Foo.beta() is a valid invocation of beta().

  2. Foo.alpha() is a valid invocation of alpha().

  3. Method beta() can directly call method alpha().

  4. Method alpha() can directly call method beta().

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

Static methods can be called on the class name (Foo.alpha()), while instance methods require an object reference. Instance methods can call static methods without an object, but static methods cannot call instance methods without an object reference since they don't operate on a specific instance.

Multiple choice technology programming languages
  1. public void foo() { }

  2. public int foo() { return 3; }

  3. public Two foo() { return this; }

  4. public Object foo() { return this; }

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

When overriding methods, the return type can be a subtype (covariant return), but not a supertype or unrelated type. Option C works because Two is a subclass of One, making it a valid covariant return type. Option A (void) and B (int) have incompatible return types, and D (Object) attempts to widen the return type.

Multiple choice technology programming languages
  1. int foo() { /* more code here */ }

  2. void foo() { /* more code here */ }

  3. public void foo() { /* more code here */ }

  4. protected void foo() { /* more code here */ }

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

When overriding methods, the return type must match exactly (void cannot become int). Access modifiers can be widened but not narrowed. Option B keeps the same package-private access. Options C and D widen access to public and protected respectively, which is valid.

Multiple choice technology programming languages
  1. Compilation will succeed for all classes and interfaces.

  2. Compilation of class C will fail because of an error in line 2.

  3. Compilation of class C will fail because of an error in line 6.

  4. Compilation of class AImpl will fail because of an error in line 2.

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

To solve this question, users need to understand the concept of interfaces and inheritance in Java programming language.

  • The interface A declares a method called doSomething that takes a string parameter.
  • The class AImpl implements the A interface and provides an implementation for the doSomething method.
  • The class B declares a method called doit that returns an object of type A.
  • The class C extends B and declares a method called doit that returns an object of type AImpl. It also declares a method called execute that overrides the execute method in class B and returns an object of type Object.

Now let's go through each option:

A. Compilation will succeed for all classes and interfaces.

This option is incorrect. Although there are no syntax errors in the code, there is a logical error in class C. It overrides the doit method in class B with a method that returns AImpl instead of A. This violates the Liskov Substitution Principle, which states that subclasses should be substitutable for their base classes. As a result, compilation will fail for class C.

B. Compilation of class C will fail because of an error in line 2.

This option is incorrect. There are no syntax errors in line 2 of class C. The error is a logical error, as explained above.

C. Compilation of class C will fail because of an error in line 6.

This option is correct. The execute method in class C attempts to override the execute method in class B. However, the return type of the method in class C is Object, which is not a subtype of the return type of the method in class B, which is String. As a result, compilation will fail for class C.

D. Compilation of class AImpl will fail because of an error in line 2.

This option is incorrect. There are no syntax errors in line 2 of class AImpl.

Therefore, the correct answer is:

The Answer is: C. Compilation of class C will fail because of an error in line 6.

Multiple choice technology programming languages
  1. Point p = new Point();

  2. Line.Point p = new Line.Point();

  3. The Point class cannot be instatiated at line 15

  4. Line 1 = new Line() ; 1.Point p = new 1.Point();

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

To solve this question, the user needs to know how to create an instance of a nested class.

Option A: Point p = new Point(); This option is incorrect because the Point class is a nested static class and must be accessed through the outer class. In this case, the outer class is Line and the correct way to access the nested class is Line.Point.

Option B: Line.Point p = new Line.Point(); This option is correct. Since Point is a nested static class, it can be accessed using the outer class name followed by a dot and the nested class name. This option correctly creates an instance of the Point class defined in Line.

Option C: The Point class cannot be instantiated at line 15. This option is incorrect. The Point class can be instantiated from within the Triangle class, but it must be done using the correct syntax since it is a nested class.

Option D: Line 1 = new Line() ; 1.Point p = new 1.Point(); This option is incorrect because it contains a syntax error. The variable 1 cannot be used as a variable name in Java.

The Answer is: B. Line.Point p = new Line.Point();

Multiple choice technology programming languages
  1. Point p = Line.getPoint();

  2. Line.Point p = Line.getPoint();

  3. Point p = (new Line()).getPoint();

  4. Line.Point p = (new Line()).getPoint();

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

Point is an inner class of Line, so it must be accessed as Line.Point from outside the enclosing class. The getPoint() method is an instance method requiring a Line object to invoke. Option D correctly creates a Line instance, calls its getPoint() method, and uses the proper inner class syntax for the variable declaration.

Multiple choice technology programming languages
  1. 1

  2. 3

  3. 123

  4. 321

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

In Java, when a constructor is called, it first implicitly calls the constructor of its parent class via super(). The chain is: new Three() → Three constructor calls super() → Two constructor calls super() → One constructor prints '1', returns to Two which prints '2', returns to Three which prints '3'. The final output is '123'.