0
0
Javaprogramming~15 mins

Array traversal in Java - Interactive Code Practice

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

Complete 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
Aj
Bi
Ck
Dn
Attempts:
3 left
2fill in blank
medium

Complete 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
Avalues.length
Bvalues.length()
Cvalues.size
Dvalues.size()
Attempts:
3 left
3fill in blank
hard

Fix 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
Afruits.length - 1
Bfruits.size
Cfruits.length
Dfruits.size()
Attempts:
3 left
4fill in blank
hard

Fill 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
Aword
Bw
Citem
Delement
Attempts:
3 left
5fill in blank
hard

Fill 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
Aw
Cw.toUpperCase()
Dword
Attempts:
3 left