Java SE 8 Programmer II Exam: Moveable<Integer> Interface Question

Moveable<Integer> Interface Question

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?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The given code declares a generic interface Moveable with a type parameter Integer. It defines a default method walk that takes an Integer parameter and prints "Walking", and an abstract method run that takes an Integer parameter but has no implementation.

Option A:

scss
Moveable<Integer> animal = n -> System.out.println("Running" + n); animal.run(100); animal.walk(20);

This is a valid use of the Moveable interface. It creates an instance of Moveable using a lambda expression that takes an Integer parameter n and prints "Running" concatenated with n. Then it calls the run method with a parameter value of 100, and the walk method with a parameter value of 20.

Option B:

scss
Moveable<Integer> animal = n -> n + 10; animal.run(100); animal.walk(20);

This is not a valid use of the Moveable interface because the lambda expression used to create the instance of Moveable returns an Integer value rather than void. The run method in the Moveable interface does not return anything, so it cannot be implemented by a lambda expression that returns an Integer value.

Option C:

scss
Moveable animal = (Integer n1, Integer n2) -> n + n2; animal.run(100); animal.walk(20);

This is not a valid use of the Moveable interface because the lambda expression used to create the instance of Moveable takes two Integer parameters n1 and n2, whereas the run method in the Moveable interface takes only one Integer parameter.

Option D:

python
Movable cannot be used in a lambda expression.

This statement is false. The Moveable interface can be used in a lambda expression as shown in Option A.

Therefore, the correct answer is A.