Bird
0
0

The following code is intended to check if the array nums contains the number 10, but it raises an error. What is the problem?

medium📝 Debug Q14 of 15
Ruby - Arrays
The following code is intended to check if the array nums contains the number 10, but it raises an error. What is the problem?
nums = [5, 10, 15]
if nums.include(10)
  puts "Found 10"
end
AThe array should be flattened before checking
BThe method should be <code>include?</code> with a question mark
CThe number 10 should be a string "10"
DThe <code>if</code> statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Identify the method name error

    The method to check presence is include? with a question mark, not include.
  2. Step 2: Understand why error occurs

    Calling include without question mark is undefined, causing an error.
  3. Final Answer:

    The method should be include? with a question mark -> Option B
  4. Quick Check:

    Use include? to check presence [OK]
Quick Trick: Check method names carefully; include? needs question mark [OK]
Common Mistakes:
MISTAKES
  • Using include without question mark
  • Thinking flatten is needed to check presence
  • Confusing number and string types
  • Miswriting if statement syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes