0
0
Rubyprogramming~20 mins

Inline if and unless (modifier form) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Inline If Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inline if modifier
What will be the output of this Ruby code snippet?
Ruby
message = "Hello"
message += " World" if message.length < 10
puts message
AHello World
BHello
CHelloWorld
DError
Attempts:
2 left
💡 Hint
Check the length of the string before the condition.
Predict Output
intermediate
2:00remaining
Using inline unless modifier
What will this Ruby code print?
Ruby
count = 5
count -= 1 unless count > 5
puts count
A4
B5
C6
DError
Attempts:
2 left
💡 Hint
Remember 'unless' runs the code only if the condition is false.
🔧 Debug
advanced
2:00remaining
Identify the error in inline if usage
Which option shows the code that will raise a syntax error?
Aputs "Done" if true; puts "Not done"
Bputs "Done" if true
Cputs "Done" unless false
Dputs "Done" if true else puts "Not done"
Attempts:
2 left
💡 Hint
Inline if cannot be combined with else in this way.
🧠 Conceptual
advanced
2:00remaining
Effect of inline unless modifier
What is the value of variable 'status' after running this code?
Ruby
status = "active"
status = "inactive" unless status == "inactive"
A"active"
B"inactive"
Cnil
DError
Attempts:
2 left
💡 Hint
The 'unless' runs the assignment only if the condition is false.
Predict Output
expert
2:00remaining
Output with inline if and method call
What will this Ruby code output?
Ruby
def check(num)
  puts "Even" if num.even?
  puts "Odd" unless num.even?
end
check(3)
AEven\nOdd
BEven
COdd
DError
Attempts:
2 left
💡 Hint
Check what num.even? returns for 3.