Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print each element of the array.
Java
int[] numbers = {1, 2, 3, 4, 5};
for (int [1] = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
2fill in blank
mediumComplete the code to sum all elements in the array.
Java
int[] values = {10, 20, 30};
int sum = 0;
for (int i = 0; i < [1]; i++) {
sum += values[i];
}
System.out.println(sum);🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
3fill in blank
hardFix the error in the code to print all elements of the array.
Java
String[] fruits = {"apple", "banana", "cherry"};
for (int i = 0; i <= [1]; i++) {
System.out.println(fruits[i]);
}🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
4fill in blank
hardFill both blanks to create a map of words to their lengths for words longer than 3 characters.
Java
Map<String, Integer> wordLengths = new HashMap<>();
String[] words = {"cat", "elephant", "dog", "lion"};
for (String [1] : words) {
if ([2].length() > 3) {
wordLengths.put([2], [2].length());
}
}🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
5fill in blank
hardFill all three blanks to create a map of uppercase words to their lengths for words longer than 4 characters.
Java
Map<String, Integer> result = new HashMap<>();
String[] words = {"tree", "mountain", "river", "sky"};
for (String [1] : words) {
if ([2].length() > 4) {
result.put([3], [2].length());
}
}🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
