Java OCA OCP Practice Question 1842

Question

Consider the following code:

public class Main{
   public void test (){
        test1 (10, 20);  //1
    }/*w  w w .  j a v  a 2 s .  co m*/

   public void test1 (int i, int... j){ System.out.println ("1");  }
   public void test1 (int... i ){ System.out.println ("2");  }
   public void test1 (int i, int j){ System.out.println ("3");  }

   public static void main (String [] args){
     new Main ().test ();
   }
}

What will the program print?

Select 1 option

  • A. 1
  • B. 2
  • C. 3
  • D. It will not compile.
  • E. Exception at runtime.


Correct Option is  : C

Note

In cases where multiple methods are applicable, the compiler always calls the most specific one.

In this case, the third one is the most specific one.

If no method is more specific than the other, then the compilation fails.

For example, if the class did not have the third method test1(int i, int j ), then the remaining two methods would have been equally applicable and equally specific.

In that case, it would not compile.




PreviousNext

Related