Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Control Flow
What is the output of this Ruby code?
num = 7
puts num.even? ? "Even" : "Odd"
AOdd
Btrue
CEven
Dfalse
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate num.even? method

    num is 7, which is not even, so num.even? returns false.
  2. Step 2: Apply ternary operator

    Since condition is false, the expression returns the value after the colon, which is "Odd".
  3. Final Answer:

    Odd -> Option A
  4. Quick Check:

    False condition returns value after colon [OK]
Quick Trick: If condition false, ternary returns second value after colon [OK]
Common Mistakes:
  • Assuming 7 is even
  • Confusing true/false branches
  • Ignoring method call result

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes