0
0
Rubyprogramming~20 mins

Ternary operator in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ternary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of a simple ternary expression
What is the output of this Ruby code?
Ruby
result = 5 > 3 ? "yes" : "no"
puts result
Atrue
Bno
Cyes
Dfalse
Attempts:
2 left
💡 Hint
The ternary operator chooses the first value if the condition is true.
Predict Output
intermediate
1:30remaining
Ternary operator with numeric result
What will be the output of this Ruby code?
Ruby
num = 10
result = num.even? ? num / 2 : num * 3
puts result
A20
B30
C10
D5
Attempts:
2 left
💡 Hint
Check if the number is even and then apply the correct operation.
Predict Output
advanced
2:00remaining
Nested ternary operator output
What is the output of this Ruby code with nested ternary operators?
Ruby
score = 75
result = score > 90 ? "A" : score > 80 ? "B" : score > 70 ? "C" : "F"
puts result
AC
BB
CA
DF
Attempts:
2 left
💡 Hint
Evaluate conditions from left to right carefully.
Predict Output
advanced
1:30remaining
Ternary operator with method call
What will this Ruby code output?
Ruby
def check(num)
  num > 0 ? "positive" : "non-positive"
end
puts check(-1)
Aerror
Bnon-positive
Cnil
Dpositive
Attempts:
2 left
💡 Hint
Check the condition num > 0 for -1.
🧠 Conceptual
expert
2:00remaining
Understanding ternary operator precedence
What is the value of variable x after running this Ruby code?
Ruby
x = false || true ? 1 : 2
A1
B2
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Remember that ternary operator has higher precedence than || operator.