Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the length of the array.
Java
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[1]);🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
2fill in blank
mediumComplete the code to loop through the array using its length property.
Java
int[] values = {10, 20, 30};
for (int i = 0; i < values[1]; i++) {
System.out.println(values[i]);
}🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
3fill in blank
hardFix the error in accessing the array length property.
Java
String[] names = {"Anna", "Bob", "Cara"};
int size = names[1];
System.out.println(size);🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
4fill in blank
hardFill both blanks to create a map of array elements to their lengths.
Java
Map<String, Integer> map = new HashMap<>();
String[] words = {"cat", "dog", "bird"};
for (String word : words) {
map.put(word, word[1]);
}
System.out.println(map.size() == words[2]);🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
5fill in blank
hardFill all three blanks to create a dictionary of word lengths for words longer than 3 characters.
Java
Map<String, Integer> wordLengths = new HashMap<>();
String[] words = {"apple", "bat", "carrot", "dog"};
for (String [1] : words) {
if ([1].[2]() > 3) {
wordLengths.put([3], [3].length());
}
}
System.out.println(wordLengths);🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
