Bird
0
0

How can you modify this nested loop to print all pairs of items from two lists?

hard🚀 Application Q9 of 15
Bash Scripting - Loops
How can you modify this nested loop to print all pairs of items from two lists?
list1="a b"
list2="1 2"
for x in $list1; do
  for y in $list2; do
    echo "$x$y"
  done
done
AAdd 'do' after inner for loop variable
BThe script is correct and prints: a1 a2 b1 b2
CUse commas between list items
DUse a single loop with combined lists
Step-by-Step Solution
Solution:
  1. Step 1: Analyze nested loops

    The outer loop runs over list1, inner loop over list2, printing concatenated pairs.
  2. Step 2: Confirm output

    Output will be a1, a2, b1, b2 each on its own line, matching The script is correct and prints: a1 a2 b1 b2.
  3. Final Answer:

    The script is correct and prints: a1 a2 b1 b2 -> Option B
  4. Quick Check:

    Nested loops produce all pairs [OK]
Quick Trick: Nested for loops create all combinations [OK]
Common Mistakes:
MISTAKES
  • Missing 'do' keyword
  • Using commas in lists
  • Trying to combine lists in one loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes