Bird
0
0

You want to print 'Access denied' only if a user is NOT an admin. Which code correctly uses unless for this?

hard📝 Application Q8 of 15
Ruby - Control Flow
You want to print 'Access denied' only if a user is NOT an admin. Which code correctly uses unless for this?
Aunless user.admin? puts 'Access denied' end
Bunless !user.admin? puts 'Access denied' end
Cunless user.admin puts 'Access denied' end
Dunless user != 'admin' puts 'Access denied' end
Step-by-Step Solution
Solution:
  1. Step 1: Understand the condition

    We want to run code if user is NOT admin, so condition is user.admin? false.
  2. Step 2: Check unless usage

    unless user.admin? runs code when user.admin? is false, which matches requirement.
  3. Final Answer:

    unless user.admin?\n puts 'Access denied'\nend -> Option A
  4. Quick Check:

    Use unless with positive condition for negation [OK]
Quick Trick: Use unless with positive check for negated action [OK]
Common Mistakes:
MISTAKES
  • Double negation with !
  • Using wrong method name
  • Confusing string and method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes