Bird
0
0

Write a Ruby if-elsif-else structure that prints "Child" if age is less than 13, "Teen" if age is between 13 and 19 inclusive, and "Adult" otherwise. Which code is correct?

hard📝 Application Q8 of 15
Ruby - Control Flow

Write a Ruby if-elsif-else structure that prints "Child" if age is less than 13, "Teen" if age is between 13 and 19 inclusive, and "Adult" otherwise. Which code is correct?

Aif age < 13 puts "Child" elsif age >= 13 && age <= 19 puts "Teen" else puts "Adult" end
Bif age <= 13 puts "Child" elsif age > 13 && age < 19 puts "Teen" else puts "Adult" end
Cif age < 13 puts "Child" elsif age > 13 && age < 19 puts "Teen" else puts "Adult" end
Dif age < 13 puts "Child" elsif age >= 13 || age <= 19 puts "Teen" else puts "Adult" end
Step-by-Step Solution
Solution:
  1. Step 1: Check conditions for each age group

    Child: age < 13, Teen: 13 <= age <= 19, Adult: else.
  2. Step 2: Verify logical operators and ranges

    if age < 13 puts "Child" elsif age >= 13 && age <= 19 puts "Teen" else puts "Adult" end correctly uses age >= 13 && age <= 19 for Teen.
  3. Final Answer:

    if age < 13 puts "Child" elsif age >= 13 && age <= 19 puts "Teen" else puts "Adult" end -> Option A
  4. Quick Check:

    Use && for range checks in elsif = A [OK]
Quick Trick: Use && to combine conditions in elsif [OK]
Common Mistakes:
MISTAKES
  • Using || instead of &&
  • Wrong boundary operators
  • Overlapping conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes