Bird
0
0

Given an associative array grades, how do you create a new associative array passed containing only keys with values >= 50?

hard🚀 Application Q9 of 15
Bash Scripting - Arrays
Given an associative array grades, how do you create a new associative array passed containing only keys with values >= 50?
Adeclare -A passed for k in "${!grades[@]}"; do (( grades[$k] >= 50 )) && passed[$k]=${grades[$k]}; done
Bdeclare -A passed for v in "${grades[@]}"; do if (( v >= 50 )); then passed+=($v); fi; done
Cdeclare -A passed for k in "${grades[@]}"; do if (( k >= 50 )); then passed[$k]=k; fi; done
Ddeclare -A passed passed=(${grades[@]/#>=50/})
Step-by-Step Solution
Solution:
  1. Step 1: Declare new associative array

    Use declare -A passed to create an empty associative array.
  2. Step 2: Loop over keys and check values

    Loop keys with ${!grades[@]} and check if value >= 50 using arithmetic.
  3. Step 3: Assign passing entries to new array

    If condition true, assign passed[$k]=${grades[$k]}.
  4. Final Answer:

    Option A correctly filters and assigns passing grades. -> Option A
  5. Quick Check:

    Filter associative array by value with loop and condition [OK]
Quick Trick: Filter associative arrays by looping keys and testing values [OK]
Common Mistakes:
MISTAKES
  • Looping over values instead of keys
  • Using invalid array assignment syntax
  • Trying to filter with string substitution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes