Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard

Question 5

What will be the result of compiling and executing the following program?

public class Main {

    public static void main(String[] args) {

        process(1);

    }


    private static void process(Object obj) {

        System.out.println("Object version");

    }


    private static void process(Number obj) {

        System.out.println("Number version");

    }


    private static void process(Double obj) {

        System.out.println("Double version");

    }

}


  • A

    Double version

  • B

    Compilation error

  • C

    Number version

  • D

    Object version

Reveal correct answer

Correct answer: C

Explanation

There are 3 overloaded methods process. Note all the numeric wrapper classes (Byte, Short, Integer, Long, Float, and Double) extend from Number, and Number extends from Object. The compiler either does implicit casting or Wrapping but not both. 1 is an int literal; the Java compiler can't implicitly cast it to double and then box it to Double, rather it boxes it to Integer and since Number is the immediate superclass of Integer, so the Number version refers to the Integer object. "Number version" is printed on the console.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need