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
  1. When a class has multiple methods with the same name but different input parameters, then such methods are considered to be overloaded.

  2. Method overloading increases the readability of a program.

  3. Method overloading can be done by changing the number of input parameters.

  4. Method overloading can be done by changing the return type of method.

  5. Main() method can be overloaded.

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

This is a false statement. Method overloading cannot be done by changing the return type of a method.

Multiple choice
  1. The method name in subclass must be identical to that of the superclass method.

  2. The return type of both the methods must be different.

  3. Methods of the superclass declared as public cannot be overrided using private keyword in the subclass.

  4. An overriding method cannot raise more exceptions than those raised by superclass.

  5. The overrided methods cannot have different number of arguments.

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

This is an incorrect condition as for overriding of methods they must have same return type, both in superclass and subclass.

Multiple choice
  1. public

  2. abstract

  3. protected

  4. synchronized

  5. default access

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

default access is the package oriented access modifier. Option 1 and 3 are wrong because public and protected are less restrictive. Option 2 and 4 are wrong because abstract and synchronized are not access modifiers.

Multiple choice
  1. Constructor must not have the same name as the class name, whereas the methods can have any name.

  2. Constructor must have the same name as the class name whereas the methods can have any name.

  3. Constructor can have return type whereas the methods may or may not have return methods.

  4. Constructor and the methods can be called any number of times in the object life cycle.

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

Constructor should have the class name as its name because when the object is instantiated, it will call the default constructor. The methods can have any name.

Multiple choice
  1. ii and iv are correct

  2. i and ii are correct

  3. only iii is correct

  4. only i is correct

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

This answer is correct. Overloaded methods are multiple methods which have the same name but different argument list. It is part of polymorphism whereas overridden methods are those methods which have the same name, same return type and same argument list.

Multiple choice
  1. i and iv are correct

  2. only i is correct

  3. only ii is correct

  4. only iv is correct

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

This answer is correct. A method marked as as final or static cannot be overridden.

Multiple choice
  1. i and ii both are correct

  2. ii and iii both are correct

  3. i and iv both are correct

  4. i and iii both are correct

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

This answer is correct. Always the super() method when used in constructor should be placed as first code. If it is placed at any other place, compilation errors will occur. Also, super keyword is used to access method and member variables from the superclass.

Multiple choice
  1. Only i is correct

  2. Only ii is correct

  3. Only iii is correct

  4. Only iv is correct

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

This answer is correct because any static method cannot be overridden and so main method cannot be overridden.

Multiple choice
  1. An interface can have only abstract methods.

  2. Constructors cannot be present in an interface.

  3. An interface can have instance variables.

  4. An interface cannot have static methods.

  5. A class can implement multiple interfaces.

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

This is a false statement. An interface cannot have instance variables.