nerdexam
Oracle

1Z0-809 · Question #119

Given: package p1; public interface DoInterface { void m1(int n); public void m2(int n); } package p3; import p1.DoInterface; public class DoClass implements DoInterface { int x1, x2; DoClass() {…

The correct answer is D. Compilation fails due to an error at line n3. Option D is correct because the Test class calls doi.method1(100) and doi.method2(200), but DoInterface only declares m1 and m2 - the names method1 and method2 simply don't exist. Since doi is statically typed as DoInterface, the compiler resolves method calls against the…

Question

Given: package p1; public interface DoInterface { void m1(int n); public void m2(int n); } package p3; import p1.DoInterface; public class DoClass implements DoInterface { int x1, x2; DoClass() { this.x1 = 0; this.x2 = 10; } public void m1(int p1) { x1+=p1; System.out.println(x1); } // line n2 public void m2(int p1) { x2+=p1; System.out.println(x2); } } package p2; import p1.; import p3.; class Test { public static void main(String[] args) { // line n3 DoInterface doi = new DoClass(); doi.method1(100); doi.method2(200); } } What is the result?

Options

  • A100
  • BCompilation fails due to an error in line n1
  • CCompilation fails due to an error at line n2
  • DCompilation fails due to an error at line n3

How the community answered

(46 responses)
  • A
    7% (3)
  • B
    13% (6)
  • C
    4% (2)
  • D
    76% (35)

Explanation

Option D is correct because the Test class calls doi.method1(100) and doi.method2(200), but DoInterface only declares m1 and m2 - the names method1 and method2 simply don't exist. Since doi is statically typed as DoInterface, the compiler resolves method calls against the interface contract, finds no match, and fails inside the main method block (line n3).

Why the distractors are wrong:

  • A (100): The code never compiles, so no output is produced at all.
  • B (line n1): The interface is valid. In Java interfaces, all methods are implicitly public abstract, so mixing void m1(int n) (implicit public) and public void m2(int n) (explicit public) is perfectly legal - no error there.
  • C (line n2): DoClass.m1 is a valid, properly public implementation of the interface's m1 method; the signature matches exactly and causes no compilation issue.

Memory tip: Think of an interface reference as a locked contract - the compiler only knows what's written in the interface, not what the concrete class can do. If the method name isn't in the interface, the call won't compile, regardless of what the implementing class provides. Always verify method names against the interface declaration, not the class.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice