Test Series - java

Test Number 63/64

Q: 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. -
Solution: -
Q: It is not mandatory to override all or a few methods of the Superclass. State TRUE or FALSE.
A. TRUE
B. FALSE
C. -
D. -
Solution: Yes. It is not mandatory. If the inheriting method serves the purpose, use it directly. Otherwise, write your custom code in the overridden method.
Q: 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.
Solution: All the above.
Q: The Method-Overloading and Method-Overriding are not the same. State TRUE or FALSE.
A. TRUE
B. FALSE
C. -
D. -
Solution: TRUE
Q: 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
Solution: Using the keyword "this" calls the local method of the class but not the method of a superclass.
Q: 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
Solution: The keyword "super" calls the method of a Superclass.
Q: 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
Solution: The return types are different. So, it is not a successful method override.
Q: A successful Method Overriding calls the method of ___ in Java.
A. Superclass
B. Subclass
C. -
D. -
Solution: The method overriding is implemented to give preference to the method of a Subclass.
Q: A failed method overriding calls the method of a ___ in Java.
A. Superclass
B. Subclass
C. Superclass or Subclass
D. None
Solution: 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.

You Have Score    /9