Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What does the elsif keyword do in Ruby?

easy
A. It ends the if statement.
B. It runs code only if all previous conditions were true.
C. It checks another condition if the previous if or elsif was false.
D. It repeats the if block multiple times.

Solution

  1. Step 1: Understand the role of elsif

    The elsif keyword is used to check another condition only if the previous if or elsif condition was false.
  2. Step 2: Compare with other options

    Options A, C, and D describe behaviors that do not match elsif. It does not end the statement, nor repeats code, nor runs only if all previous conditions were true.
  3. Final Answer:

    It checks another condition if the previous if or elsif was false. -> Option C
  4. Quick Check:

    elsif = check next condition [OK]
Hint: Remember: elsif checks next condition if previous false [OK]
Common Mistakes:
  • Thinking elsif ends the if statement
  • Confusing elsif with else
  • Assuming elsif runs only if all previous are true
2.

Which of the following is the correct syntax for an if statement with an else in Ruby?

?
easy
A. if x > 10 puts "Big" else puts "Small" end
B. if (x > 10) { puts "Big" } else { puts "Small" }
C. if x > 10 then puts "Big" else puts "Small"
D. if x > 10: puts "Big" else: puts "Small"

Solution

  1. Step 1: Identify Ruby syntax for if and else

    Ruby uses if condition, then code indented or on next line, and ends with end. Curly braces or colons are not used.
  2. Step 2: Check each option

    if x > 10 puts "Big" else puts "Small" end matches Ruby syntax exactly. Options A, B, and C use syntax from other languages like colons (A), braces (B), or missing end (C).
  3. Final Answer:

    if x > 10 puts "Big" else puts "Small" end -> Option A
  4. Quick Check:

    Ruby if ends with end [OK]
Hint: Ruby if always ends with end [OK]
Common Mistakes:
  • Using braces {} instead of end
  • Forgetting the end keyword
  • Using colons like Python
3.

What will be the output of this Ruby code?

score = 75
if score >= 90
  puts "Grade A"
elsif score >= 80
  puts "Grade B"
elsif score >= 70
  puts "Grade C"
else
  puts "Grade F"
end
medium
A. Grade A
B. Grade B
C. Grade F
D. Grade C

Solution

  1. Step 1: Check conditions in order

    score is 75. First condition: score >= 90 is false. Second: score >= 80 is false. Third: score >= 70 is true.
  2. Step 2: Output for first true condition

    Since third condition is true, it prints "Grade C" and skips the rest.
  3. Final Answer:

    Grade C -> Option D
  4. Quick Check:

    75 >= 70 is true, so output is Grade C [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
  • Choosing Grade B or A without checking order
  • Ignoring that only first true condition runs
  • Confusing >= with >
4.

Find the error in this Ruby code snippet:

number = 10
if number > 5
  puts "Big"
elsif number < 3
  puts "Small"
else
  puts "Medium"
medium
A. Wrong keyword elsif should be else if.
B. Missing end to close the if statement.
C. The puts statements need parentheses.
D. The comparison operators are incorrect.

Solution

  1. Step 1: Check Ruby syntax completeness

    Ruby if statements must end with end. The code snippet is missing end.
  2. Step 2: Verify other parts

    elsif is correct in Ruby. puts can be used without parentheses. Comparison operators are correct.
  3. Final Answer:

    Missing end to close the if statement. -> Option B
  4. Quick Check:

    Every if needs end [OK]
Hint: Always close if with end [OK]
Common Mistakes:
  • Replacing elsif with else if
  • Adding unnecessary parentheses to puts
  • Changing correct comparison operators
5.

You want to write a Ruby program that prints "Positive", "Negative", or "Zero" depending on the value of num. Which code correctly uses if, elsif, and else to do this?

hard
A. if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end
B. if num >= 0 puts "Positive" elsif num <= 0 puts "Negative" else puts "Zero" end
C. if num > 0 puts "Positive" else if num < 0 puts "Negative" else puts "Zero" end
D. if num > 0 puts "Positive" elsif num > 0 puts "Negative" else puts "Zero" end

Solution

  1. Step 1: Understand the conditions needed

    We want to print "Positive" if num > 0, "Negative" if num < 0, and "Zero" otherwise.
  2. Step 2: Check each option's logic

    if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end matches the logic exactly with correct if, elsif, and else. if num >= 0 puts "Positive" elsif num <= 0 puts "Negative" else puts "Zero" end wrongly treats zero as positive. if num > 0 puts "Positive" else if num < 0 puts "Negative" else puts "Zero" end uses invalid else if syntax in Ruby. if num > 0 puts "Positive" elsif num > 0 puts "Negative" else puts "Zero" end repeats the same condition twice, which is incorrect.
  3. Final Answer:

    if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end -> Option A
  4. Quick Check:

    Correct conditions and syntax = if num > 0 puts "Positive" elsif num < 0 puts "Negative" else puts "Zero" end [OK]
Hint: Use elsif for second condition, else for default [OK]
Common Mistakes:
  • Using else if instead of elsif
  • Repeating same condition in elsif
  • Misclassifying zero as positive or negative
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard