Bird
0
0

What will this script output?

medium📝 Command Output Q5 of 15
Bash Scripting - Arrays
What will this script output?
declare -A animals
animals[dog]=bark
animals[cat]=meow
animals[bird]=tweet
for key in "${!animals[@]}"; do echo "$key: ${animals[$key]}"; done
AError: invalid loop syntax
Bbark: dog meow: cat tweet: bird
Cdog bark cat meow bird tweet
Ddog: bark cat: meow bird: tweet
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop over keys

    The loop iterates over all keys of the associative array using ${!animals[@]}.
  2. Step 2: Print key and value pairs

    Each iteration prints "key: value" format, so output lines are "dog: bark", "cat: meow", "bird: tweet".
  3. Final Answer:

    dog: bark cat: meow bird: tweet -> Option D
  4. Quick Check:

    Loop keys and print key:value pairs [OK]
Quick Trick: Use ${!array[@]} to get all keys [OK]
Common Mistakes:
MISTAKES
  • Printing values as keys
  • Missing colon separator
  • Wrong loop syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes