Java OCA OCP Practice Question 3088

Question

Consider the following program:

public class Main {
   public static void foo(Integer i) {
      System.out.println("foo(Integer)");
   }/*from   w w w. ja v a2 s .c  om*/

   public static void foo(short i) {
      System.out.println("foo(short)");
   }

   public static void foo(long i) {
      System.out.println("foo(long)");
   }

   public static void foo(int... i) {
      System.out.println("foo(int ...)");
   }

   public static void main(String[] args) {
      foo(10);
   }
}

Which one of the following options correctly describes the output of this program?

  • a) foo(Integer)
  • b) foo(short)
  • c) foo(long)
  • d) foo(int ...)


c)

Note

For an integer literal, the JVM matches in the following order: int, long, Integer, int.

In other words, it first looks for an int type parameter; if it is not provided, then it looks for long type; and so on.

Here, since the int type parameter is not specified with any overloaded method, it matches with foo(long).




PreviousNext

Related