Java Method Overriding Interview MCQ Questions and Answers

Study and learn Interview MCQ Questions and Answers on Method Overriding in Java. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Method Overriding before reading these objective questions.



1) What is method overriding in Java?
A) Writing a method in a subclass with the same name of superclass's method
B) Mentioning the same return type of the method of the superclass
C) The argument list in the method of subclass and the method of superclass should be the same
D) All the above
Answer [=]
D
2) Method Overriding is useful to add extra functionality or code to a method of subclass with the same name as the inherited method. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
3) It is not mandatory to override all or a few methods of the Superclass. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes. It is not mandatory. If the inheriting method serves the purpose, use it directly. Otherwise, write your custom code in the overridden method.

4) Why should a method be overridden in Java instead of writing a method with a different name?
A) Large projects heavily depend on inheritance
B) The code-base refers to the same method with the same method signature in different classes of the project
C) It is not possible to change the method calling code at all occurrences of the project. It may break the whole project.
D) All the above.
Answer [=]
D
Explanation:

All the above.

5) The Method-Overloading and Method-Overriding are not the same. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

TRUE

6) What is the output of the below Java program with Method Overriding?
class Bus
{
  void seatingCapacity()
  {
    System.out.println("Superclass Seats=32");
  }
}
class ElectricBus extends Bus
{
  void seatingCapacity()
  {
    System.out.println("Subclass Seats=20");
  }
  void showInfo()
  {
    seatingCapacity();
    this.seatingCapacity();
  }
}
public class MethodOverriding1
{
  public static void main(String[] args)
  {
    ElectricBus eb = new ElectricBus();
    eb.showInfo();
  }
}
A)
Subclass Seats=20
Superclass Seats=32
B)
Superclass Seats=32
Subclass Seats=20
C)
Superclass Seats=32
Superclass Seats=32
D)
Subclass Seats=20
Subclass Seats=20
Answer [=]
D
Explanation:

Using the keyword "this" calls the local method of the class but not the method of a superclass.

7) What is the output of the below Java program with Method Overriding and SUPER keyword?
class Car
{
  void showTransmission()
  {
    System.out.println("Transmission Manual");
  }
}
class ElectricCar extends Car
{
  void showTransmission()
  {
    System.out.println("Transmission AMT");
  }
  void showInfo()
  {
    this.showTransmission();
    super.showTransmission();
  }
}
public class MethodOverriding2
{
  public static void main(String[] args)
  {
    ElectricCar ec = new ElectricCar();
    ec.showInfo();
  }
}
A)
Transmission AMT
Transmission Manual
B)
Transmission Manual
Transmission AMT
C)
Transmission Manual
Transmission Manual
D)
Transmission AMT
Transmission AMT
Answer [=]
A
Explanation:

The keyword "super" calls the method of a Superclass.



8) Identify INVALID Java Method Overriding in the below code snippets? Follow the notation "superclassMethod" and "subclassMethod".
A)
void superclassMethod(int a, float b){ }

void subclassMethod(int a, float b) { }
B)
void superclassMethod(){ }

void subclassMethod(){ }
C)
int superclassMethod(int a, float b){ }

void subclassMethod(int a, float b) { }
D) None
Answer [=]
C
Explanation:

The return types are different. So, it is not a successful method override.

9) A successful Method Overriding calls the method of ___ in Java.
A) Superclass
B) Subclass
C) -
D) -
Answer [=]
B
Explanation:

The method overriding is implemented to give preference to the method of a Subclass.

10) A failed method overriding calls the method of a ___ in Java.
A) Superclass
B) Subclass
C) Superclass or Subclass
D) None
Answer [=]
C
Explanation:

If a method override fails, the JVM may call the method of either a Superclass or Subclass. It depends on the parameters passed in the method call.

11) What is the output of the below Java program with method overriding?
class Cat
{
  int jumpingHeight(int weight)
  {
    System.out.println(10);
    return 10;
  }
}
class WildCat extends Cat
{
  void jumpingHeight(int weight)
  {
    System.out.println("20");
  }
}
public class MethodOverriding3
{
  public static void main(String[] args)
  {
    WildCat wc = new WildCat();
    wc.jumpingHeight(30);
  }
}
A) 10
B) 20
C) 30
D) Compiler error
Answer [=]
D
Explanation:

If the argument list is the same, the return types can not be the incomptible-types. So, the compiler reports an error "The return type is incompatible with Cat.jumpingHeight(int)".

12) What is the output of the below Java program with Method Overriding?
class Sparrow{ }
class BigSparrow extends Sparrow { }
class Cat2
{
  Sparrow jumpingHeight(int weight)
  {
    System.out.println(40);
    return new Sparrow();
  }
}
class WildCat2 extends Cat2
{
  BigSparrow jumpingHeight(int weight)
  {
    System.out.println("50");
    return new BigSparrow();
  }
}
public class MethodOverriding4
{
  public static void main(String[] args)
  {
    WildCat2 wc = new WildCat2();
    wc.jumpingHeight(80);
  }
}
A) 40
B) 50
C) 80
D) Compiler error
Answer [=]
B
Explanation:

It is perfectly alright to use a subclass type return type when overriding a method in Java. BigSparrow is the subclass of Sparrow. Always, the overriding method will be called.

13) What is the output of the below Java program with method overriding?
class Steel
{
  void setGrade(int g)
  {
    System.out.print(",GRADE="+g);
  }
}
class CarbonSteel extends Steel
{
  void setGrade(char grade)
  {
    System.out.print(",Grade="+grade);
  }
}
public class MethodOverriding5
{
  public static void main(String[] args)
  {
    Steel s = new CarbonSteel();
    s.setGrade(5);
    s.setGrade('A');
  }
}
A) ,GRADE=5,GRADE=A
B) ,GRADE=5,GRADE=65
C) ,Grade=5,Grade=65
D) Compiler error
Answer [=]
B
Explanation:

As the superclass reference "s" is used, it calls the methods of the superclass only. As the method signatures of the "setGrade" method are different with different argument types, it is not a successful override. It is an overloading of the superclass's method.

14) What is the output of the below Java program with method overriding?
class Wood
{
  void setQuality(int q)
  {
    System.out.print(",QUALITY="+q);
  }
}
class PlyWood extends Wood
{
  void setQuality(char qual)
  {
    System.out.print(",quality="+qual);
  }
}
public class MethodOverriding6
{
  public static void main(String[] args)
  {
    PlyWood pw = new PlyWood();
    pw.setQuality(10);
    pw.setQuality('B'); //ASCII of B=66
  }
}
A) ,QUALITY=10,quality=B
B) ,QUALITY=10,quality=65
C) ,quality=10,quality=B
D) Compiler error
Answer [=]
A
Explanation:

The method "setQuality" is not overridden successfully as the argument types are different. The subclass type reference can call a method of superclass and subclass.



15) What is the output of the below Java program with method overriding?
class Amplifier
{
  void addGain(int a)
  {
    System.out.println((a + 10)+"dB");
  }
}
class DigitalAmplifier extends Amplifier
{
  void addGain(int a)
  {
    super.addGain(a+5);
  }
}
public class MethodOverriding7
{
  public static void main(String[] args)
  {
    DigitalAmplifier da = new DigitalAmplifier();
    da.addGain(12);
  }
}
A) 22dB
B) 27dB
C) 22dB 27dB
D) Compiler error
Answer [=]
B
Explanation:

The subclass DigitalAmplifier successfully overrides the method of the superclass "Amplifier".  In the subclass's method, an extra gain of 5 is added and passed to the superclass's method. So, it becomes 27(12+5+10).

16) If the method signature of a Superclass and the method signature of a Subclass are the same, then the subclass method is said to be _____ the superclass's method.
A) Overriding
B) Overloading
C) -
D) -
Answer [=]
A
17) A method of a Superclass can not override the method of the Subclass. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

True. Only subclass methods can override the methods of a superclass.

18) Method overriding increases the burden on the JVM in terms of runtime checks and resolution. State TRUE or FALSE.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
Explanation:

Yes. The to be called at runtime is decided at runtime based on successful or failed Overriding.

19) What are the advantages of Method Overriding in Java?
A) A subclass can add extra functionality to the overriding method.
B) A subclass can call both the overridden method and overriding method.
C) It supports polymorphism. A superclass reference can be used to call the common method of all subclasses.
D) All the above
Answer [=]
D
20) An Overridden method is the method of ____ class and the overriding method is the method of ___ class.
A) super, sub
B) sub, super
C) super, super
D) sub, sub
Answer [=]
A
Explanation:

An Overriding method belongs to the Subclass and the Overridden method belongs to the Superclass.

21) To successfully override a superclass method in Java, the access modifier of the method of the subclass can be ___ restrictive.
A) Less
B) More
C) Less or Same
D) None
Answer [=]
C
Explanation:

The access modifier can be less restrictive.

protected void show() { }
.
.
public void show() { } //public is less restrictive than private.


22) Which is the correct way of overriding a method throwing exceptions in Java?
A)
void show() throws IOException{ }
.
.
void show() throws FileNotFoundException{ }
B)
void show() throws IOException{ }
.
.
void show() throws ArithmeticException{ }
C)
void show() throws ArithmeticException{ }
.
.
void show() throws IllegalFormatException{ }
D) None
Answer [=]
A
Explanation:

The exception thrown by the subclass's method can be a subclass type of the exception thrown by the superclass's method. The superclass of FileNotFoundException is IOException only.