nerdexam
Oracle

1Z0-819 · Question #145

Given: public class Over { public void analyze(Object o) { System.out.println("I am an object array"); } public void analyze(long[] l) { System.out.println("I am an array"); } public void…

The correct answer is B. The compilation fails due to an error in line 1. Compilation fails because the Over class defines analyze(Object o) twice - Java does not permit two methods with identical signatures in the same class, regardless of what their bodies do. This duplicate method declaration is a compile-time error that prevents the program from…

Java Object-Oriented Approach

Question

Given: public class Over { public void analyze(Object o) { System.out.println("I am an object array"); } public void analyze(long[] l) { System.out.println("I am an array"); } public void analyze(Object o) { System.out.println("I am an object"); } public static void main(String[] args) { int[] nums = new int[10]; new Over().analyze(nums); // line 1 } } What is the output?

Options

  • AI am an object array
  • BThe compilation fails due to an error in line 1
  • CI am an array
  • DI am an object

How the community answered

(22 responses)
  • A
    9% (2)
  • B
    82% (18)
  • C
    5% (1)
  • D
    5% (1)

Explanation

Compilation fails because the Over class defines analyze(Object o) twice - Java does not permit two methods with identical signatures in the same class, regardless of what their bodies do. This duplicate method declaration is a compile-time error that prevents the program from ever running, so the JVM never reaches line 1 or any println statement.

Options A, C, and D are all wrong for the same underlying reason: they assume the program compiles and executes, producing output from one of the three analyze methods. While it's true that an int[] cannot be widened to long[] (making option C unreachable anyway), and that int[] can be autoboxed/upcasted to Object (making A or D plausible candidates), none of this matters because the class itself is illegal.

Memory tip: Before reasoning about which overloaded method gets called, scan the class for duplicate method signatures - same name and same parameter types. If you spot one, the answer is always "compilation fails," and you can skip the overload-resolution analysis entirely.

Topics

#method overloading#duplicate method signatures#compilation errors#method resolution

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice