Java OCA OCP Practice Question 2640

Question

Consider the following program:

class Overload {//  www  . jav a2 s .co  m
       private Overload(Object o) {
               System.out.println("Object");
       }
       private Overload(double [] arr) {
               System.out.println("double []");
       }
       private Overload() {
               System.out.println("void");
       }
       public static void main(String[]args) {
               new Overload(null);     // MARKER
       }
}

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

  • a) It throws a compiler error in the line marked with the comment MARKER for ambiguous overload.
  • b) When executed, the program prints the following: Object.
  • c) When executed, the program prints the following: double [].
  • d) When executed, the program prints the following: void.


c)

Note

The overload resolution matches to the most specific overload.

When the argument null is passed, there are two candidates, Overload(Object) and Overload(double[]), and of these two, Overload(double[]) is the most specific overload, so the compiler resolves to calling that method.




PreviousNext

Related