OCA Java SE 8 Method - OCA Mock Question Method 1








Question

Which of the following are true about the following code? (Choose all that apply)

     public class Main { 
       Main() { 
         System.out.print("1 "); 
       } 
       Main(int num) { 
         System.out.print("2 "); 
       } 
       Main(Integer num) { 
         System.out.print("3 "); 
       } 
       Main(Object num) { 
         System.out.print("4 "); 
       } 
       Main(int... nums) { 
         System.out.print("5 "); 
       } 
       public static void main(String[] args) { 
         new Main(1); 
         new Main(1L); 
       } 
     } 
  1. The code prints out 2 4.
  2. The code prints out 3 4.
  3. The code prints out 4 2.
  4. The code prints out 4 4.
  5. The code prints 3 4 if you remove the constructor Main(int num).
  6. The code prints 4 4 if you remove the constructor Main(int num).
  7. The code prints 5 4 if you remove the constructor Main(int num).




Answer



A, E.

Note

1 is an int and so calls the matching int constructor.

When this constructor is removed, Java chooses the Integer constructor.

1L is a long and can't be converted into a smaller type, it is autoboxed into a Long and then the constructor for Object is called.