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
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)- A3% (2)
- B5% (3)
- C83% (54)
- D9% (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 + 10produces a value expression, butrunhas avoidreturn type - the lambda body is incompatible. - D is wrong because
Moveablehas 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 adefault(instance) method.
Memory tip: default ≠ static. 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.