Test Series - java

Test Number 64/64

Q:  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
Solution: 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)".
Q: 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
Solution: 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.
Q: A method of a Superclass can not override the method of the Subclass. State TRUE or FALSE.
A. TRUE
B. FALSE
C. -
D. -
Solution: True. Only subclass methods can override the methods of a superclass.
Q: 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. -
Solution: Yes. The to be called at runtime is decided at runtime based on successful or failed Overriding.
Q: 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
Solution: no solution
Q: 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
Solution: An Overriding method belongs to the Subclass and the Overridden method belongs to the Superclass.

You Have Score    /6