Challenge - 5 Problems
Indexed Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of Indexed Array Declaration and Access
What is the output of this Bash script?
Bash Scripting
arr=(apple banana cherry)
echo ${arr[1]}Attempts:
2 left
💡 Hint
Remember that Bash arrays start indexing at 0.
✗ Incorrect
The array 'arr' has elements indexed from 0. So arr[0] is 'apple', arr[1] is 'banana', and arr[2] is 'cherry'. The script prints arr[1], which is 'banana'.
💻 Command Output
intermediate2:00remaining
Length of Indexed Array
What does this Bash command output?
Bash Scripting
arr=(one two three four)
echo ${#arr[@]}Attempts:
2 left
💡 Hint
${#arr[@]} gives the number of elements in the array.
✗ Incorrect
The array has four elements, so ${#arr[@]} outputs 4.
📝 Syntax
advanced2:00remaining
Identify the Correct Indexed Array Declaration
Which option correctly declares an indexed array with elements 'red', 'green', 'blue' in Bash?
Attempts:
2 left
💡 Hint
Bash arrays use parentheses without commas.
✗ Incorrect
In Bash, indexed arrays are declared with parentheses and space-separated values, like arr=(red green blue). The other options use invalid syntax.
💻 Command Output
advanced2:00remaining
Output of Array Element Assignment
What is the output of this Bash script?
Bash Scripting
arr=() arr[2]=orange arr[0]=lemon echo ${arr[@]}
Attempts:
2 left
💡 Hint
Elements can be assigned at specific indexes; missing indexes are empty.
✗ Incorrect
The array has elements at index 0 ('lemon') and index 2 ('orange'). When printing all elements with ${arr[@]}, Bash outputs them in index order, skipping empty indexes, so 'lemon orange'.
💻 Command Output
expert2:00remaining
Output of Indexed Array with Mixed Assignments
What is the output of this Bash script?
Bash Scripting
arr=(a b c) arr[5]=f echo ${#arr[@]}
Attempts:
2 left
💡 Hint
The length counts the number of assigned elements, not the highest index.
✗ Incorrect
The array initially has 3 elements (indexes 0,1,2). Assigning arr[5]=f adds a fourth element at index 5. The total number of elements is 4, so ${#arr[@]} outputs 4.