Java OCA OCP Practice Question 3273

Question

What will the program print when compiled and run?.

public class Main {
  public static void main(String[] args) {
    callType(10);//from w w  w  .j  a  v a  2s  .  co m
  }

  private static void callType(Number num){
    System.out.println("Number passed");
  }

  private static void callType(Object obj){
    System.out.println("Object passed");
  }
}

    

Select the one correct answer.

  • (a) The program compiles and prints: Object passed
  • (b) The program compiles and prints: Number passed
  • (c) The program fails to compile, because the call to the callType() method is ambiguous.
  • (d) None of the above.


(b)

Note

The method with the most specific signature is chosen.

In this case the int argument 10 is converted to an Integer which is passed to the Number formal parameter, as type Number is more specific than Object.




PreviousNext

Related