Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Associative arrays (declare -A) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Associative Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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]}
Ayellow
Bred
Cpurple
Dbanana
Attempts:
2 left
💡 Hint
Look at the key used inside the brackets when accessing the array.
💻 Command Output
intermediate
2: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[@]}
A4
B3
C0
D1
Attempts:
2 left
💡 Hint
${#array[@]} gives the number of elements in the array.
🔧 Debug
advanced
2: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]}
Abash: colors: unbound variable
Bbash: colors: array assignment without declare -A
Cbash: colors: command not found
DNo error, outputs 'apple'
Attempts:
2 left
💡 Hint
Associative arrays require a special declaration in Bash.
🚀 Application
advanced
2: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
AParis Tokyo NewDelhi
B
Paris: France
Tokyo: Japan
NewDelhi: India
C
France: Paris
Japan: Tokyo
India: NewDelhi
DFrance Japan India
Attempts:
2 left
💡 Hint
"${!array[@]}" gives all keys in the associative array.
🧠 Conceptual
expert
2: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}
Ayellow
Bred
Cbanana
Dnone
Attempts:
2 left
💡 Hint
Unset removes the key, and the default value is used if key is missing.

Practice

(1/5)
1. What is the main purpose of using declare -A in a Bash script?
easy
A. To define a function
B. To declare a regular indexed array
C. To create an associative array with named keys
D. To start a loop

Solution

  1. Step 1: Understand the role of declare -A

    This command is used in Bash to declare an associative array, which means the array uses named keys instead of numbers.
  2. Step 2: Differentiate from other declarations

    Regular arrays use numbers as keys and are declared without -A. Functions and loops are declared differently.
  3. Final Answer:

    To create an associative array with named keys -> Option C
  4. Quick Check:

    declare -A means associative array [OK]
Hint: Remember: -A means associative array with named keys [OK]
Common Mistakes:
  • Confusing associative arrays with regular indexed arrays
  • Thinking declare -A creates a function
  • Assuming declare -A starts a loop
2. Which of the following is the correct syntax to declare an associative array named colors in Bash?
easy
A. declare colors=(red green blue)
B. declare -A colors
C. array colors=(red green blue)
D. declare -a colors

Solution

  1. Step 1: Identify the correct declaration for associative arrays

    In Bash, declare -A is used to declare associative arrays, so declare -A colors is correct.
  2. Step 2: Understand other options

    declare -a is for indexed arrays, and the other options are invalid syntax for associative arrays.
  3. Final Answer:

    declare -A colors -> Option B
  4. Quick Check:

    Associative array declaration uses declare -A [OK]
Hint: Use declare -A to declare associative arrays [OK]
Common Mistakes:
  • Using declare -a which is for indexed arrays
  • Trying to assign values during declaration incorrectly
  • Using invalid syntax like array keyword
3. What will be the output of the following Bash script?
declare -A fruits
fruits[apple]=red
fruits[banana]=yellow
for key in "${!fruits[@]}"; do
  echo "$key is ${fruits[$key]}"
done
medium
A. apple banana\nred yellow
B. red is apple\nyellow is banana
C. Syntax error
D. apple is red\nbanana is yellow

Solution

  1. Step 1: Understand the associative array and loop

    The array fruits has keys 'apple' and 'banana' with values 'red' and 'yellow'. The loop iterates over keys and prints key and value pairs.
  2. Step 2: Predict the output format

    Each line prints "key is value", so output lines are "apple is red" and "banana is yellow" (order may vary but both lines appear).
  3. Final Answer:

    apple is red\nbanana is yellow -> Option D
  4. Quick Check:

    Loop prints key and value pairs correctly [OK]
Hint: Loop keys with ${!array[@]} to access associative array keys [OK]
Common Mistakes:
  • Printing values as keys or vice versa
  • Expecting syntax error due to quotes
  • Confusing indexed arrays with associative arrays
4. Identify the error in the following Bash script that uses an associative array:
declare -A animals
animals=([dog]=bark [cat]=meow)
echo ${animals[dog]}
medium
A. No error, script runs correctly
B. Associative array cannot be assigned values this way
C. Missing quotes around keys in assignment
D. Incorrect syntax for echo command

Solution

  1. Step 1: Check associative array assignment syntax

    Assigning values with animals=([dog]=bark [cat]=meow) is valid syntax for associative arrays in Bash.
  2. Step 2: Verify echo command usage

    echo ${animals[dog]} correctly prints the value for key 'dog'. No syntax errors present.
  3. Final Answer:

    No error, script runs correctly -> Option A
  4. Quick Check:

    Associative array assignment and access are correct [OK]
Hint: Assign associative arrays with ([key]=value ...) syntax [OK]
Common Mistakes:
  • Thinking quotes are mandatory around keys in assignment
  • Assuming echo syntax is wrong without quotes
  • Confusing indexed array assignment with associative arrays
5. You want to create an associative array user_ages that stores names as keys and ages as values. Which script correctly adds entries and prints all names with their ages?
hard
A. declare -A user_ages user_ages=( [Alice]=30 [Bob]=25 ) for name in "${!user_ages[@]}"; do echo "$name is ${user_ages[$name]} years old" done
B. declare user_ages user_ages=( Alice=30 Bob=25 ) for name in "${user_ages[@]}"; do echo "$name is ${user_ages[$name]} years old" done
C. declare -A user_ages user_ages=( Alice:30 Bob:25 ) for name in "${!user_ages[@]}"; do echo "$name is ${user_ages[$name]} years old" done
D. declare -a user_ages user_ages=( [Alice]=30 [Bob]=25 ) for name in "${!user_ages[@]}"; do echo "$name is ${user_ages[$name]} years old" done

Solution

  1. Step 1: Check declaration and assignment

    declare -A user_ages user_ages=( [Alice]=30 [Bob]=25 ) for name in "${!user_ages[@]}"; do echo "$name is ${user_ages[$name]} years old" done correctly declares an associative array with declare -A and assigns keys and values using ( [key]=value ) syntax.
  2. Step 2: Verify loop and output

    The loop uses ${!user_ages[@]} to get keys and prints each name with its age correctly.
  3. Final Answer:

    Option A script correctly adds and prints associative array entries -> Option A
  4. Quick Check:

    Use declare -A and [key]=value syntax for associative arrays [OK]
Hint: Use declare -A and [key]=value syntax for associative arrays [OK]
Common Mistakes:
  • Using declare -a which is for indexed arrays
  • Incorrect key-value syntax like colons or missing brackets
  • Looping over values instead of keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard