Test Series - java

Test Number 53/64

Q: To successfully overload a method in Java, the return types must be ___.
A. Same
B. Different
C. Same but using superclass or subclass types also work
D. None
Solution: no solution
Q: To successfully overload a method in Java, the argument-list or parameter-list must be ___.
A. Same
B. Different
C. compiler error
D. None
Solution: no solution
Q: To successfully overload a method in Java, the method names must be ___.
A. Same
B. Different
C. Same or different
D. None
Solution: no solution
Q: What is the output of the below Java program?
public class MethodOverloading2
{
  int info()
  {
    System.out.println("PLANE");
    return 0;
  }

  void info()
  {
    System.out.println("AIRPORT");
  }

  public static void main(String[] args)
  {
    MethodOverloading2 m = new MethodOverloading2();
    int a = m.info();
  }
}
A. PLANE
B. AIRPORT
C. Compiler error
D. None
Solution: Both methods with the same name "info" do not overload successfully as the return types are different (void and int).
Q: What is the output of the below Java program with method overloading?
class Wood{ }
class SubWood extends Wood{ }

public class MethodOverloading3
{
  Wood display(int a)
  {
    System.out.println("PINE");
    return new Wood();
  }
  SubWood display()
  {
    System.out.println("TEAK");
    return new SubWood();
  }

  public static void main(String[] args)
  {
    MethodOverloading3 m = new MethodOverloading3();
    m.display();
  }
}
A. PINE
B. TEAK
C. Compiler error
D.  None
Solution: Return types for the method display() are Wood and SubWood. As these types are of superclass-subclass, it is a valid method overloading.
Q: What is the output of the below Java program trying to overload a method "jump"?
class Rabbit{ }
class WildRabbit extends Rabbit{ }

public class MethodOverloading4
{
  Rabbit jump()
  {
    System.out.println("Rabbit Jump");
    return new Rabbit();
  }
  WildRabbit jump()
  {
    System.out.println("Wild Rabbit Jump");
    return new WildRabbit();
  }

  public static void main(String[] args)
  {
    MethodOverloading4 obj = new MethodOverloading4();
    obj.jump();
  }
}
A. Rabbit Jump
B. Wild Rabbit Jump
C. Compiler error
D. None
Solution: The above program does not overload the method jump() properly as the argument list is not distinct. So the compiler reports "duplicate method error".
Q: Java method overloading implements the OOPS concept ___.
A.  Inheritance
B. Polymorphism
C. Encapsulation
D. None
Solution: no solution
Q: Which is the overloaded static method of Math class to get absolute value in Java?
A. Math.abs(int)
B. Math.abs(float)
C. Math.abs(double)
D. All the above
Solution: Notice that static methods can be overloaded in Java without any compiler error. So, Math.abs() works out of the box with different type arguments.
Q: What is the output of the below Java program that tries to overload a method "abs"?
public class MyAbs
{
  static int abs(int a)
  {
    return a<0?-a:a;
  }
  static float abs(float b)
  {
    return b<0?-b:b;
  }
  static double abs(double c)
  {
    return c<0?-c:c;
  }
  public static void main(String[] args)
  {
    int a=-10;
    float b=-4.56f;
    double c=-10.123;
    System.out.println(MyAbs.abs(a) + ", " + MyAbs.abs(b) + ", " + MyAbs.abs(c));
  }
}
A. No output
B. Compiler error
C. 10, 4.56, 10.123
D. -10, -4.56, -10.123
Solution: The above program demonstrates the usage of Overloaded methods. "abs" function returns the absolute value of the input.

You Have Score    /9