0
0
Rubyprogramming~20 mins

If, elsif, else statements in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else Mastery
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 with if, elsif, else?
Consider the following Ruby code snippet. What will it print when run?
Ruby
score = 75
if score >= 90
  puts "Excellent"
elsif score >= 60
  puts "Good"
else
  puts "Needs Improvement"
end
ANo output
BExcellent
CGood
DNeeds Improvement
Attempts:
2 left
💡 Hint
Think about which condition matches the score 75 first.
Predict Output
intermediate
2:00remaining
What does this Ruby code print?
Look at this Ruby code using if, elsif, and else. What will be printed?
Ruby
temperature = 30
if temperature > 35
  puts "It's very hot"
elsif temperature > 25
  puts "It's warm"
elsif temperature > 15
  puts "It's cool"
else
  puts "It's cold"
end
AIt's very hot
BIt's warm
CIt's cool
DIt's cold
Attempts:
2 left
💡 Hint
Check the first condition that matches temperature 30.
🔧 Debug
advanced
2:00remaining
Why does this Ruby code raise an error?
This Ruby code is intended to print a message based on age, but it raises an error. What is the cause?
Ruby
age = 20
if age < 13
  puts "Child"
elsif age < 20
  puts "Teenager"
else
  puts "Adult"
end
AMissing 'end' keyword to close the if statement
BUsing 'elsif' instead of 'else if' causes syntax error
CThe variable 'age' is not defined
DThe comparison operators are invalid
Attempts:
2 left
💡 Hint
Check if all blocks are properly closed.
🧠 Conceptual
advanced
2:00remaining
What is the value of 'result' after this Ruby code runs?
Analyze the code and determine the final value of the variable 'result'.
Ruby
number = 10
result = if number > 10
  "Greater"
elsif number == 10
  "Equal"
else
  "Smaller"
end
A"Greater"
Bnil
C"Smaller"
D"Equal"
Attempts:
2 left
💡 Hint
Remember that if-elsif-else in Ruby returns the last evaluated expression.
📝 Syntax
expert
2:00remaining
Which option causes a syntax error in Ruby if-elsif-else statement?
Identify which code snippet will cause a syntax error when run.
A
if x &gt; 0
  puts "Positive"
elsif x == 0
  puts "Zero"
else
  puts "Negative"
B
if x &gt; 0
  puts "Positive"
elsif x == 0
  puts "Zero"
else
  puts "Negative"
end
C
dne
"evitageN" stup  
esle
"oreZ" stup  
0 == x fisle
"evitisoP" stup  
0 &gt; x fi
D
f x &gt; 0
  puts "Positive"
elsif x == 0
  puts "Zero"
else
  puts "Negative"
end
Attempts:
2 left
💡 Hint
Check if all if statements are properly closed.