Java OCA OCP Practice Question 620

Question

Consider the following code:

public class Main{ 
   public void test (){ 
        test1 (10);       //1 
        test1 (10, 20); //2 
    } /*from w  w w.  j  av  a  2  s .  com*/

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

Which of the following lines can be added independently to the above class so that it will run without any errors or exceptions?

Select 2 options

  • A. public void test1 (int i, int j){}
  • B. public void test1 (int i, int... j){}
  • C. public void test1 (int... i){}
  • D. public void test1 (int i...){}
  • E. public void test1 (int [] i){}



Correct Options are  : B C

Note

A var-arg parameter of type T is same as an array of type T.

class TestClass  { 
    String m1 (int [] i)  { return ""+i.length;  } 
    String m2 (int... i)  { return ""+i.length;  } 
} 

javap TestClass produces this:

java.lang.String m1 (int []); 
java.lang.String m2 (int []); 

Conversely, the following code will not compile:

class TestClass  { 
    String m1 (int [] i)  { return ""+i.length;  } 
    String m1 (int... i)  { return ""+i.length;  }  
} 



PreviousNext

Related