Bird
0
0

Which of the following is the correct syntax to define a predicate method named empty? in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Methods
Which of the following is the correct syntax to define a predicate method named empty? in Ruby?
Adef empty?; true; end
Bdef empty; true; end
Cdef empty? (true) end
Ddef empty?? true end
Step-by-Step Solution
Solution:
  1. Step 1: Check method definition syntax

    Ruby methods are defined with def, method name, optional parentheses, body, and end.
  2. Step 2: Validate predicate method syntax

    Method name must end with '?', and the body can be a single expression followed by end.
  3. Final Answer:

    def empty?; true; end -> Option A
  4. Quick Check:

    Correct predicate method syntax = def name?; body; end [OK]
Quick Trick: Use def name?; body; end to define predicate methods [OK]
Common Mistakes:
  • Omitting '?' in method name
  • Missing 'end' keyword
  • Incorrect parentheses usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes