Bird
Raised Fist0
Rubyprogramming~20 mins

Method declaration with def in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple method call
What is the output of this Ruby code?
Ruby
def greet(name)
  "Hello, #{name}!"
end

puts greet("Alice")
Agreet
BHello, Alice!
CHello, name!
Dnil
Attempts:
2 left
💡 Hint
Look at how the method uses string interpolation with the parameter.
❓ Predict Output
intermediate
2:00remaining
Return value of method without explicit return
What does this Ruby method return when called with argument 5?
Ruby
def square(x)
  x * x
end

result = square(5)
puts result
ASyntaxError
B5
Cnil
D25
Attempts:
2 left
💡 Hint
In Ruby, the last evaluated expression is returned by default.
🔧 Debug
advanced
2:00remaining
Identify the error in method declaration
What error does this Ruby code produce?
Ruby
def add_numbers(a, b)
  sum = a + b
  puts sum
end

add_numbers(3, 4)
ANo error, outputs 7
BNoMethodError: undefined method 'add_numbers'
CTypeError: no implicit conversion of nil into Integer
DSyntaxError: unexpected end-of-input, expecting keyword_end
Attempts:
2 left
💡 Hint
Check if all method definitions are properly closed.
❓ Predict Output
advanced
2:00remaining
Method with default parameter value
What is the output of this Ruby code?
Ruby
def greet(name = "Guest")
  "Welcome, #{name}!"
end

puts greet
puts greet("Bob")
A
Welcome, Guest!
Welcome, Bob!
B
Welcome, !
Welcome, Bob!
C
Welcome, Guest!
Welcome, Guest!
DSyntaxError
Attempts:
2 left
💡 Hint
Look at the default value assigned to the parameter.
🧠 Conceptual
expert
2:00remaining
Understanding method scope and variable visibility
Consider this Ruby code. What is the output?
Ruby
def change_var(x)
  x = 10
end

value = 5
change_var(value)
puts value
Anil
B10
C5
DNameError
Attempts:
2 left
💡 Hint
Think about whether the method changes the original variable or just a copy.

Practice

(1/5)
1. What keyword do you use in Ruby to start defining a method?
easy
A. method
B. function
C. define
D. def

Solution

  1. Step 1: Understand Ruby method syntax

    In Ruby, methods start with the keyword def and end with end.
  2. Step 2: Identify the correct keyword

    The correct keyword to start a method is def, not function, method, or define.
  3. Final Answer:

    def -> Option D
  4. Quick Check:

    Method start keyword = def [OK]
Hint: Remember: Ruby methods start with 'def' and end with 'end' [OK]
Common Mistakes:
  • Using 'function' like in JavaScript
  • Using 'method' keyword which doesn't exist
  • Forgetting to use 'def' to start method
2. Which of the following is the correct way to declare a method named greet in Ruby?
easy
A. def greet puts "Hello"
B. function greet() { puts "Hello" }
C. def greet() puts "Hello" end
D. method greet() puts "Hello" end

Solution

  1. Step 1: Check Ruby method syntax

    Ruby methods start with def method_name() and end with end. Parentheses are optional but allowed.
  2. Step 2: Identify correct syntax

    def greet() puts "Hello" end uses def greet() puts "Hello" end, which is correct Ruby syntax. Options B and D use wrong keywords, and C misses end.
  3. Final Answer:

    def greet() puts "Hello" end -> Option C
  4. Quick Check:

    Correct Ruby method syntax = def greet() puts "Hello" end [OK]
Hint: Use 'def name() ... end' to declare methods in Ruby [OK]
Common Mistakes:
  • Forgetting the 'end' keyword
  • Using JavaScript style 'function' keyword
  • Missing parentheses or using wrong keywords
3. What will be the output of this Ruby code?
def add(a, b)
  a + b
end

puts add(3, 4)
medium
A. 7
B. 34
C. a + b
D. Error

Solution

  1. Step 1: Understand method parameters and return

    The method add takes two parameters a and b and returns their sum a + b.
  2. Step 2: Calculate the method call result

    Calling add(3, 4) returns 3 + 4 = 7, which is printed by puts.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Method returns last expression value; 3 + 4 = 7 [OK]
Common Mistakes:
  • Thinking it concatenates as strings
  • Expecting explicit return keyword
  • Confusing output with code text
4. Identify the error in this Ruby method declaration:
def say_hello
  puts "Hello"
medium
A. Missing 'end' keyword to close method
B. Missing parentheses after method name
C. Incorrect use of puts inside method
D. Method name cannot have underscore

Solution

  1. Step 1: Check method structure

    Ruby methods must start with def and end with end. This code misses the end keyword.
  2. Step 2: Identify the missing part

    Without end, Ruby will raise a syntax error because the method is not properly closed.
  3. Final Answer:

    Missing 'end' keyword to close method -> Option A
  4. Quick Check:

    Every method needs 'end' [OK]
Hint: Always close methods with 'end' keyword [OK]
Common Mistakes:
  • Forgetting 'end' after method body
  • Thinking parentheses are mandatory
  • Assuming underscores are invalid in names
5. You want to create a method greet_person that takes a name and returns a greeting like "Hello, Alice!". Which code correctly does this?
hard
A. def greet_person(name) puts "Hello, #{name}!" end
B. def greet_person(name) "Hello, " + name + "!" end
C. def greet_person(name) return puts "Hello, " + name + "!" end
D. def greet_person(name) print "Hello, " + name + "!" end

Solution

  1. Step 1: Understand method return vs output

    To return a greeting string, the method should produce the string as its last expression or use return. Using puts or print outputs to console but returns nil.
  2. Step 2: Analyze each option

    def greet_person(name) "Hello, " + name + "!" end returns the greeting string correctly. def greet_person(name) puts "Hello, #{name}!" end prints but returns nil. def greet_person(name) return puts "Hello, " + name + "!" end returns nil because puts returns nil. def greet_person(name) print "Hello, " + name + "!" end prints but does not return the string.
  3. Final Answer:

    def greet_person(name) "Hello, " + name + "!" end -> Option B
  4. Quick Check:

    Return string, not print it [OK]
Hint: Return string directly; puts prints but returns nil [OK]
Common Mistakes:
  • Using puts instead of returning string
  • Returning result of puts (which is nil)
  • Confusing print with return
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