Challenge - 5 Problems
Associative Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of Associative Array Access
What is the output of this Bash script?
Bash Scripting
declare -A colors
colors[apple]=red
colors[banana]=yellow
colors[grape]=purple
echo ${colors[banana]}Attempts:
2 left
💡 Hint
Look at the key used inside the brackets when accessing the array.
✗ Incorrect
The script declares an associative array 'colors' and assigns colors to fruits. Accessing colors[banana] prints 'yellow'.
💻 Command Output
intermediate2:00remaining
Count Items in Associative Array
What is the output of this Bash script?
Bash Scripting
declare -A animals
animals[dog]=bark
animals[cat]=meow
animals[cow]=moo
animals[duck]=quack
echo ${#animals[@]}Attempts:
2 left
💡 Hint
${#array[@]} gives the number of elements in the array.
✗ Incorrect
The associative array 'animals' has 4 key-value pairs, so ${#animals[@]} outputs 4.
🔧 Debug
advanced2:00remaining
Identify the Error in Associative Array Declaration
What error does this Bash script produce?
Bash Scripting
colors=( [red]=apple [yellow]=banana )
echo ${colors[red]}Attempts:
2 left
💡 Hint
Associative arrays require a special declaration in Bash.
✗ Incorrect
Associative arrays must be declared with 'declare -A' before assigning key-value pairs. Without it, Bash treats it as a normal array and errors.
🚀 Application
advanced2:00remaining
Result of Looping Over Associative Array Keys
What will this Bash script output?
Bash Scripting
declare -A capitals capitals[France]=Paris capitals[Japan]=Tokyo capitals[India]=NewDelhi for country in "${!capitals[@]}"; do echo "$country: ${capitals[$country]}" done
Attempts:
2 left
💡 Hint
"${!array[@]}" gives all keys in the associative array.
✗ Incorrect
The loop iterates over keys (countries) and prints each key with its value (capital). Output is each country and its capital separated by colon.
🧠 Conceptual
expert2:00remaining
Behavior of Unset Key in Associative Array
What is the output of this Bash script?
Bash Scripting
declare -A fruits
fruits[apple]=red
fruits[banana]=yellow
unset fruits[apple]
echo ${fruits[apple]:-none}Attempts:
2 left
💡 Hint
Unset removes the key, and the default value is used if key is missing.
✗ Incorrect
The key 'apple' is removed with unset. Accessing fruits[apple] returns empty, so the default 'none' is printed.