Bird
Raised Fist0
Rubyprogramming~20 mins

Method naming conventions (? and ! suffixes) in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Method Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code with a method ending with '?'?
Consider the following Ruby code. What will be printed when calling even_number??
Ruby
def even_number?(num)
  num % 2 == 0
end

puts even_number?(4)
puts even_number?(5)
Anil\nnil
Bfalse\ntrue
C4\n5
Dtrue\nfalse
Attempts:
2 left
💡 Hint
Methods ending with '?' usually return true or false.
❓ Predict Output
intermediate
2:00remaining
What does the method ending with '!' do differently?
Look at this Ruby code. What will be the output after calling capitalize! on the string?
Ruby
str = "hello"
result = str.capitalize!
puts str
puts result
Ahello\nHello
BHello\nHello
CHello\nhello
Dhello\nhello
Attempts:
2 left
💡 Hint
Methods ending with '!' usually modify the object itself.
🔧 Debug
advanced
2:00remaining
Why does this method with '?' suffix raise an error?
This Ruby method is intended to check if a number is positive. Why does it raise a syntax error?
Ruby
def positive?(num)
  if num > 0
    return true
  else
    return false
  end
end
ABecause the 'end' keyword is followed by an extra '?', which is invalid syntax.
BBecause method names cannot end with '?' and have an extra '?' after 'end'.
CBecause 'return' cannot be used inside methods ending with '?'.
DBecause the method is missing a closing parenthesis.
Attempts:
2 left
💡 Hint
Check the syntax after the method body ends.
❓ Predict Output
advanced
2:00remaining
What is the output of this code using both '?' and '!' methods?
Given this Ruby code, what will be printed?
Ruby
class User
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def name?
    !@name.nil? && !@name.empty?
  end

  def clear_name!
    @name = ""
  end
end

user = User.new("Alice")
puts user.name?
user.clear_name!
puts user.name?
Atrue\ntrue
Bfalse\ntrue
Ctrue\nfalse
Dfalse\nfalse
Attempts:
2 left
💡 Hint
The '?' method checks if name is present; the '!' method clears it.
🧠 Conceptual
expert
2:00remaining
Why should methods ending with '!' be used carefully in Ruby?
Which of the following best explains the purpose and caution behind methods ending with '!' in Ruby?
AThey indicate methods that modify the object in place and may have side effects, so use them only when you want to change the original object.
BThey are methods that always return true or false and should be used for checks only.
CThey are methods that raise exceptions on failure and never modify the object.
DThey are deprecated methods and should be avoided in modern Ruby code.
Attempts:
2 left
💡 Hint
Think about what the '!' means in method names in Ruby conventions.

Practice

(1/5)
1. Which method name follows Ruby convention for a method that returns a boolean value?
? is used at the end of method names that return true or false.
easy
A. save
B. update!
C. calculate
D. valid?

Solution

  1. Step 1: Understand the meaning of ? suffix

    In Ruby, methods ending with ? return true or false, indicating a boolean check.
  2. Step 2: Identify the method that returns a boolean

    Among the options, valid? clearly suggests a boolean check, while others do not.
  3. Final Answer:

    valid? -> Option D
  4. Quick Check:

    Boolean methods end with ? = valid? [OK]
Hint: Boolean methods end with ? to show true/false result [OK]
Common Mistakes:
  • Confusing ! with ? suffix
  • Using ? for methods that modify objects
  • Not using ? for boolean-returning methods
2. Which of the following method names is correctly written to indicate it modifies the object in Ruby?
easy
A. delete
B. delete!
C. delete?
D. deleted

Solution

  1. Step 1: Understand the meaning of ! suffix

    In Ruby, methods ending with ! usually modify the object or perform a 'dangerous' action.
  2. Step 2: Identify the method that modifies the object

    delete! clearly indicates modification, while delete may not, and delete? suggests a boolean check.
  3. Final Answer:

    delete! -> Option B
  4. Quick Check:

    Modification methods end with ! = delete! [OK]
Hint: Use ! for methods that change the object [OK]
Common Mistakes:
  • Using ? instead of ! for modifying methods
  • Not adding ! for dangerous methods
  • Confusing method names without suffixes
3. What will be the output of this Ruby code?
class User
  def active?
    true
  end

  def deactivate!
    @active = false
    "User deactivated"
  end
end

user = User.new
puts user.active?
puts user.deactivate!
puts user.active?
medium
A. true User deactivated true
B. true User deactivated false
C. false User deactivated false
D. true User deactivated nil

Solution

  1. Step 1: Analyze the active? method

    This method always returns true because it returns the literal true without checking any variable.
  2. Step 2: Analyze the deactivate! method and second active? call

    deactivate! sets @active = false but active? does not use @active, so it still returns true.
  3. Final Answer:

    true User deactivated true -> Option A
  4. Quick Check:

    active? returns true always = true User deactivated true [OK]
Hint: Check if boolean method uses instance variables or fixed return [OK]
Common Mistakes:
  • Assuming deactivate! changes active? output
  • Confusing method side effects
  • Ignoring method return values
4. Identify the error in this Ruby method definition:
def save?
  # saves the object
  puts "Saving..."
  true
end

What is wrong with this method name?
medium
A. Method name cannot contain ?
B. Method should end with ! instead of ?
C. Method ends with ? but performs an action, not a boolean check
D. No error, method is correct

Solution

  1. Step 1: Understand the meaning of ? suffix

    Methods ending with ? should return true or false and not perform side effects like saving.
  2. Step 2: Analyze the method behavior

    This method prints "Saving..." which is a side effect, so it should not end with ?.
  3. Final Answer:

    Method ends with ? but performs an action, not a boolean check -> Option C
  4. Quick Check:

    Methods with ? should not have side effects [OK]
Hint: ? means boolean check, not side effects [OK]
Common Mistakes:
  • Using ? for methods that change state
  • Ignoring method side effects
  • Confusing ! and ? suffix meanings
5. You want to write a Ruby method that checks if a string is empty and also a method that clears the string content. Which method names follow Ruby conventions?
hard
A. empty? and clear!
B. empty! and clear?
C. empty and clear!
D. empty? and clear

Solution

  1. Step 1: Identify boolean check method

    The method checking if a string is empty should return true or false, so it should end with ?, e.g., empty?.
  2. Step 2: Identify modifying method

    The method that clears the string modifies it, so it should end with !, e.g., clear!.
  3. Final Answer:

    empty? and clear! -> Option A
  4. Quick Check:

    Boolean check ends with ?; modifying method ends with ! [OK]
Hint: Boolean methods end with ?; modifying methods end with ! [OK]
Common Mistakes:
  • Swapping ? and ! suffixes
  • Not using suffixes for clarity
  • Using no suffix for modifying methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard