Bird
0
0

Given this Ruby code, what will be the output?

hard📝 Application Q15 of 15
Ruby - Control Flow

Given this Ruby code, what will be the output?

data = [3, 7, 12, 18]
case data
in [Integer, Integer, Integer, Integer]
  puts "All integers"
in [Integer, Integer, Integer, 18]
  puts "Last is 18"
else
  puts "No match"
end
ANo match
BLast is 18
CAll integers
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand array pattern matching order

    The case checks patterns in order. The first pattern matches any array of four integers.
  2. Step 2: Check if data matches first pattern

    Data is [3,7,12,18], all integers, so it matches the first pattern.
  3. Step 3: Confirm second pattern is not reached

    Since first pattern matches, second is skipped.
  4. Final Answer:

    All integers -> Option C
  5. Quick Check:

    First matching pattern runs, so output is "All integers" [OK]
Quick Trick: Patterns checked top-down; first match wins [OK]
Common Mistakes:
  • Assuming second pattern runs despite first matching
  • Confusing element types with values in patterns
  • Expecting syntax error due to pattern complexity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes