0
0
Bash Scriptingscripting~20 mins

Array length in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script?
Consider the following Bash script that defines an array and prints its length. What will be the output?
Bash Scripting
arr=(apple banana cherry date)
echo ${#arr[@]}
A4
B1
C3
DSyntax error
Attempts:
2 left
💡 Hint
Remember that ${#array[@]} gives the number of elements in the array.
💻 Command Output
intermediate
2:00remaining
What does this script print?
Given this Bash script, what is the output?
Bash Scripting
arr=(one two three)
echo ${#arr}
A11
B3
CSyntax error
D0
Attempts:
2 left
💡 Hint
${#arr} returns the length of the first element, not the number of elements.
💻 Command Output
advanced
2:00remaining
What is the output of this Bash script with sparse array?
Consider this Bash script where an array has elements assigned at non-contiguous indices. What will be the output?
Bash Scripting
arr=()
arr[0]=zero
arr[3]=three
arr[5]=five
echo ${#arr[@]}
ASyntax error
B5
C3
D6
Attempts:
2 left
💡 Hint
Array length counts the number of elements, not the highest index.
💻 Command Output
advanced
2:00remaining
What does this script output when printing array length?
What is the output of this Bash script?
Bash Scripting
arr=(a b c d e)
length=${#arr[@]}
echo $length
unset arr[2]
echo ${#arr[@]}
ASyntax error
B
5
5
C
5
3
D
5
4
Attempts:
2 left
💡 Hint
Unsetting an element removes it, reducing the count.
💻 Command Output
expert
2:00remaining
What is the output of this Bash script with indirect reference?
Given this script, what will be the output?
Bash Scripting
arr=(x y z)
name=arr
echo ${#name[@]}
echo ${#arr[@]}
A
1
3
B
3
3
C
Syntax error
3
D
0
3
Attempts:
2 left
💡 Hint
Variable name is a string, not an array reference.