Bird
0
0

Which Ruby expression correctly returns true if the variable item is nil?

easy📝 Conceptual Q2 of 15
Ruby - Variables and Data Types
Which Ruby expression correctly returns true if the variable item is nil?
A<code>item.empty?</code>
B<code>item == false</code>
C<code>item.nil?</code>
D<code>item == 0</code>
Step-by-Step Solution
Solution:
  1. Step 1: Identify the method to check nil

    Ruby provides the nil? method to check if an object is nil.
  2. Step 2: Evaluate other options

    item == false checks for false, not nil; empty? is for collections; item == 0 checks for zero.
  3. Final Answer:

    item.nil? -> Option C
  4. Quick Check:

    nil.nil? returns true [OK]
Quick Trick: Use .nil? method to check for nil [OK]
Common Mistakes:
MISTAKES
  • Using == false instead of .nil?
  • Using empty? on non-collections

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes