Bird
0
0

Given arr=(a b c d e), which command prints elements at even indexes only (0,2,4)?

hard🚀 Application Q9 of 15
Bash Scripting - Arrays
Given arr=(a b c d e), which command prints elements at even indexes only (0,2,4)?
Aecho ${arr[@]:0:5:2}
Becho ${arr[0]} ${arr[2]} ${arr[4]}
Cecho ${arr[@]:0:5}
Decho ${arr[@]::2}
Step-by-Step Solution
Solution:
  1. Step 1: Identify elements at even indexes

    Indexes 0,2,4 correspond to elements a, c, e.
  2. Step 2: Check Bash syntax for skipping elements

    Bash does not support step in slicing like Python; must access individually.
  3. Final Answer:

    echo ${arr[0]} ${arr[2]} ${arr[4]} -> Option B
  4. Quick Check:

    Access elements individually for non-contiguous indexes [OK]
Quick Trick: Bash slicing has no step; access elements individually [OK]
Common Mistakes:
MISTAKES
  • Assuming slicing supports step parameter
  • Using invalid slicing syntax with 4 parts
  • Expecting error when accessing multiple elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes