Bird
0
0

Which of the following hash definitions will cause a syntax error in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Hashes
Which of the following hash definitions will cause a syntax error in Ruby?
A{ name => "Bob", age => 25 }
B{ :name => "Bob", :age => 25 }
C{ "name" => "Bob", "age" => 25 }
D{ name: "Bob", age: 25 }
Step-by-Step Solution
Solution:
  1. Step 1: Review valid key syntax

    Symbol keys can be written as :name => or name:. String keys must be quoted and use rocket syntax.
  2. Step 2: Identify invalid syntax

    { name => "Bob", age => 25 } uses bare words as keys without colon or rocket, which is invalid and causes syntax error.
  3. Final Answer:

    SyntaxError -> Option A
  4. Quick Check:

    Bare words as keys need colon or rocket [OK]
Quick Trick: Keys must be symbols with colon or strings with quotes and rocket [OK]
Common Mistakes:
MISTAKES
  • Using bare words as keys without colon or rocket
  • Mixing string keys without quotes
  • Confusing colon placement for symbols

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes