0
0
Rubyprogramming~20 mins

Ternary operator usage in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ternary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using ternary operator?
Consider the following Ruby code snippet. What will it print?
Ruby
x = 5
puts x > 3 ? "Greater" : "Smaller"
Afalse
BSmaller
Ctrue
DGreater
Attempts:
2 left
💡 Hint
Check if x is greater than 3 and what the ternary operator returns.
Predict Output
intermediate
2:00remaining
What does this nested ternary operator print?
Analyze the output of this Ruby code with nested ternary operators.
Ruby
a = 10
b = 20
puts a > b ? "a is bigger" : a == b ? "equal" : "b is bigger"
Ab is bigger
Ba is bigger
Cequal
DSyntaxError
Attempts:
2 left
💡 Hint
Check the first condition a > b, then the second condition a == b.
🔧 Debug
advanced
2:00remaining
What error does this Ruby code raise?
Identify the error raised by this Ruby code snippet using ternary operator.
Ruby
num = 7
result = num > 5 ? "High" :
"Low"
puts result
ANo error, prints High
BSyntaxError
CNo error, prints Low
DNameError
Attempts:
2 left
💡 Hint
Check if the line break affects the ternary operator syntax.
Predict Output
advanced
2:00remaining
What is the value of variable 'output' after running this code?
Determine the value stored in 'output' after this Ruby code executes.
Ruby
value = nil
output = value ? "Exists" : "Nil or false"
puts output
AExists
BNil or false
Cnil
Dfalse
Attempts:
2 left
💡 Hint
Remember how Ruby treats nil in conditionals.
Predict Output
expert
2:00remaining
What is the output of this Ruby code with complex ternary and method call?
Analyze the output of this Ruby code snippet.
Ruby
def check(num)
  num.even? ? "Even" : num > 10 ? "Odd and big" : "Odd and small"
end

puts check(7)
AEven
BOdd and big
COdd and small
DNoMethodError
Attempts:
2 left
💡 Hint
Check the method call and nested ternary logic carefully.