Bird
0
0

Given arr = ["a", "b", "c"], which code will reverse the array and then convert each element to uppercase, modifying arr in place?

hard📝 Application Q9 of 15
Ruby - Methods
Given arr = ["a", "b", "c"], which code will reverse the array and then convert each element to uppercase, modifying arr in place?
Aarr.reverse.map(&:upcase)
Barr.reverse!.map!(&:upcase)
Carr.reverse.map(&:upcase!)
Darr.reverse!.map(&:upcase)
Step-by-Step Solution
Solution:
  1. Step 1: Reverse array in place

    reverse! reverses arr itself.
  2. Step 2: Convert each element to uppercase in place

    map! modifies arr elements in place by replacing them with new uppercase strings returned by &:upcase.
  3. Step 3: Check method chaining correctness

    arr.reverse!.map!(&:upcase) correctly reverses and uppercases arr in place.
  4. Final Answer:

    arr.reverse!.map!(&:upcase) -> Option B
  5. Quick Check:

    Use reverse! and map! to modify array in place [OK]
Quick Trick: Use bang methods to modify arrays and elements in place [OK]
Common Mistakes:
  • Using non-bang reverse or map
  • Failing to reverse the array when using upcase!
  • Using non-bang map which doesn't replace elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes