0
0
Javaprogramming~15 mins

Array length property 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 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
A.length()
B.count
C.size
D.length
Attempts:
3 left
2fill in blank
medium

Complete 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
A.count
B.size()
C.length
D.length()
Attempts:
3 left
3fill in blank
hard

Fix 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
A.length()
B.length
C.size
D.count
Attempts:
3 left
4fill in blank
hard

Fill 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
A.length()
B.length
C.size()
D.count
Attempts:
3 left
5fill in blank
hard

Fill 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
Aword
Blength
Clength()
Dwords
Attempts:
3 left