0
0
Javaprogramming~10 mins

Why interfaces are used in Java - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an interface named Vehicle.

Java
public interface [1] {
    void move();
}
Drag options to blanks, or click blank then click option'
AVehicle
BCar
CDrive
DMoveable
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a class name instead of an interface name.
Forgetting to use the 'interface' keyword.
2fill in blank
medium

Complete the code to make class Car implement the Vehicle interface.

Java
public class Car [1] Vehicle {
    public void move() {
        System.out.println("Car is moving");
    }
}
Drag options to blanks, or click blank then click option'
Ainherits
Bextends
Cimplements
Duses
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'extends' instead of 'implements' for interfaces.
Omitting the 'implements' keyword.
3fill in blank
hard

Fix the error in the code by completing the interface method declaration.

Java
public interface Animal {
    [1] sound();
}
Drag options to blanks, or click blank then click option'
Avoid
Bpublic
Cint
DString
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'void' when a return value is expected.
Omitting the return type.
4fill in blank
hard

Fill both blanks to create a map that maps words to their lengths only if length is greater than 3.

Java
Map<String, Integer> wordLengths = words.stream()
    .filter(word -> word.length() [1] 3)
    .collect(Collectors.toMap(word -> word, word -> word.[2]()));
Drag options to blanks, or click blank then click option'
A>
Blength
C<
Dsize
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '<' instead of '>' in the filter.
Using 'size()' instead of 'length()' for strings.
5fill in blank
hard

Fill all three blanks to create a map of uppercase keys to values only if value is negative.

Java
Map<String, Integer> filteredMap = data.entrySet().stream()
    .filter(entry -> entry.getValue() [1] 0)
    .collect(Collectors.toMap(entry -> entry.getKey().[2](), entry -> entry.[3]()));
Drag options to blanks, or click blank then click option'
A<
BgetValue
CgetKey
DtoUpperCase
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'getKey()' instead of 'getValue()' for values.
Using '>' instead of '<' in the filter.