nerdexam
Oracle

1Z0-809 · Question #104

Given: ``java public interface Moveable<Integer> { public default void walk (Integer distance) { System.out.println("Walking")); } public void run(Integer distance); } `` Which statement is true?

The correct answer is C. Moveable can be used as below: Moveable<Integer> animal = (Integer n) -> System.out.println(n); animal.run(100); Moveable.walk(20). Note on this question: The stated answer of C appears to contain an error that undermines it as the correct choice. Moveable.walk(20) in option C attempts to call a default method as if it were static on the interface - this does not compile in Java. Only static interface…

Question

Given:
public interface Moveable<Integer> {
 public default void walk (Integer distance) {
 System.out.println("Walking"));
 }
 public void run(Integer distance);
}
Which statement is true?

Options

  • AMoveable can be used as below: Moveable<Integer> animal = n -> System.out.println("Running" + n); animal.run(100); animal.walk(20);
  • BMoveable can be used as below: Moveable<Integer> animal = n -> n + 10; animal.run(100); animal.walk(20);
  • CMoveable can be used as below: Moveable<Integer> animal = (Integer n) -> System.out.println(n); animal.run(100); Moveable.walk(20);
  • DMoveable cannot be used in a lambda expression.

How the community answered

(65 responses)
  • A
    3% (2)
  • B
    5% (3)
  • C
    83% (54)
  • D
    9% (6)

Explanation

Note on this question: The stated answer of C appears to contain an error that undermines it as the correct choice.

Moveable.walk(20) in option C attempts to call a default method as if it were static on the interface - this does not compile in Java. Only static interface methods can be invoked as Interface.method(); default methods require an instance reference.

Option A is actually the most valid implementation: the lambda n -> System.out.println("Running" + n) correctly implements the single abstract method run, and animal.walk(20) correctly invokes the default method on the instance.

Why the other options fail:

  • B is wrong because n -> n + 10 produces a value expression, but run has a void return type - the lambda body is incompatible.
  • D is wrong because Moveable has exactly one abstract method (run), making it a functional interface fully eligible for lambda use.
  • C is wrong because Moveable.walk(20) is an invalid static-style call on a default (instance) method.

Memory tip: defaultstatic. A default method lives on instances - always call it as instance.method(). Only static interface methods use the Interface.method() syntax. If your exam insists C is correct, push back - Java's compiler will reject Moveable.walk(20).

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice