Java OCA OCP Practice Question 992

Question

Which of the following are true about the following code?

Choose all that apply

public class Main { 
  Main() { /*from w w  w. j a  v a2  s .  co  m*/
    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(100); 
    new Main(1000L); 
  } 
} 
  • A. The code prints out 2 4.
  • B. The code prints out 3 4.
  • C. The code prints out 4 2.
  • D. The code prints out 4 4.
  • E. The code prints 3 4 if you remove the constructor Main(int num).
  • F. The code prints 4 4 if you remove the constructor Main(int num).
  • G. The code prints 5 4 if you remove the constructor Main(int num).


A, E.

Note

The 100 parameter is an int and so calls the matching int constructor.

When this constructor is removed, Java looks for the next most specific constructor.

Java prefers autoboxing to varargs, and so chooses the Integer constructor.

The 100L parameter is a long.

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




PreviousNext

Related