MCQs on Methods and Blocks | Ruby

Learn about defining and calling methods, handling parameters, default values, return values, and an introduction to blocks, procs, and lambdas. Perfect for anyone diving into Ruby programming.


Topic 1: Defining and Calling Methods

  1. In Ruby, how do you define a simple method?
    a) def method_name() end
    b) function method_name() end
    c) method method_name()
    d) def method_name end
  2. What keyword is used to define a method in Ruby?
    a) def
    b) method
    c) function
    d) define
  3. Which of the following is the correct way to call a method in Ruby?
    a) method_name()
    b) call method_name()
    c) method_name[]
    d) method_name
  4. What is the purpose of the return keyword in a method?
    a) To end the method
    b) To pass a value back from the method
    c) To print the value
    d) To define the method’s parameters
  5. Which of the following is the correct way to define a method with no parameters in Ruby?
    a) def method_name()
    b) def method_name[]
    c) def method_name{}
    d) def method_name()
  6. What will happen if you call a method without parentheses in Ruby?
    a) An error will occur
    b) The method will be called successfully
    c) It will call the method with default arguments
    d) The method will not be called
  7. How can you call a method with parameters in Ruby?
    a) method_name(arg1, arg2)
    b) method_name(arg1; arg2)
    c) method_name(arg1: arg2)
    d) method_name[args]
  8. What is the correct syntax for defining a method that takes a block as an argument?
    a) def method_name(&block)
    b) def method_name(block)
    c) def method_name(block: true)
    d) def method_name(block)
  9. What is the default return value of a method in Ruby if no explicit return is specified?
    a) nil
    b) false
    c) 0
    d) ""
  10. How do you call a method that is defined within a class in Ruby?
    a) ClassName.method_name()
    b) object.method_name()
    c) method_name()
    d) Class.method_name()

Topic 2: Parameters, Default Values, and Return Values

  1. How do you define a method with default parameter values in Ruby?
    a) def method_name(a = 5)
    b) def method_name(a => 5)
    c) def method_name(a : 5)
    d) def method_name(a: 5)
  2. Which of the following is the correct way to define a method with multiple parameters in Ruby?
    a) def method_name(a, b)
    b) def method_name(a, b, c)
    c) def method_name(a: b, c)
    d) def method_name[a, b]
  3. What will happen if you call a method with fewer arguments than defined in Ruby?
    a) An error will occur
    b) The missing arguments will default to nil
    c) The method will not execute
    d) The default values will be used for missing parameters
  4. Which of the following allows defining a variable number of parameters in Ruby?
    a) *args
    b) *var
    c) ...args
    d) var
  5. How can you return multiple values from a Ruby method?
    a) return val1, val2
    b) return [val1, val2]
    c) return {val1, val2}
    d) return (val1, val2)
  6. What happens when you call a method without passing required parameters?
    a) Ruby assigns nil to missing parameters
    b) An error is raised
    c) Ruby uses default values automatically
    d) The method does not execute
  7. What is the return type when you call a method in Ruby that doesn’t explicitly return anything?
    a) nil
    b) false
    c) undefined
    d) ""
  8. In Ruby, which method can be used to obtain the parameters passed to a method?
    a) method.parameters
    b) method.args
    c) method.get_parameters
    d) method.params
  9. What does the yield keyword do in Ruby?
    a) It returns a value from the method
    b) It passes control to the block of code
    c) It ends the method execution
    d) It creates a new method
  10. What will be the output of the following Ruby code?def test_method(a = 5) return a end puts test_method() a) nil
    b) 5
    c) 0
    d) ""

Topic 3: Blocks, Procs, and Lambdas (Introduction)

  1. What is a block in Ruby?
    a) A piece of reusable code passed to a method
    b) A method that is invoked multiple times
    c) A data structure for storing multiple values
    d) A type of variable
  2. Which of the following is the correct syntax to pass a block to a method in Ruby?
    a) method_name {}
    b) method_name[]
    c) method_name() {}
    d) method_name{};
  3. How do you define a proc in Ruby?
    a) proc = Proc.new { |x| x * 2 }
    b) proc = lambda { |x| x * 2 }
    c) proc = def { |x| x * 2 }
    d) proc = proc { |x| x * 2 }
  4. What is the main difference between a proc and a lambda in Ruby?
    a) A lambda checks the number of arguments, while a proc does not
    b) A proc returns from the method, while a lambda returns to the caller
    c) A proc does not use arguments, while a lambda does
    d) A proc can only be called once, while a lambda can be called multiple times
  5. What happens if a block is called with an incorrect number of arguments?
    a) Ruby raises an error
    b) Ruby assigns nil to missing arguments
    c) Ruby ignores extra arguments
    d) Ruby uses default values for missing arguments
  6. Which of the following is used to pass a block to a method in Ruby?
    a) yield
    b) proc.call
    c) lambda
    d) block.call
  7. How do you define a method with a block in Ruby?
    a) def method_name(&block)
    b) def method_name(block)
    c) def method_name() &block
    d) def method_name{}
  8. What is a lambda in Ruby?
    a) A special type of block that behaves like a method
    b) A function that accepts multiple arguments
    c) A method that cannot be invoked
    d) A block that can take default values
  9. How can you pass a block to a method and use it inside that method?
    a) yield
    b) call
    c) exec
    d) block
  10. What does the call method do with procs or lambdas in Ruby?
    a) It invokes the block of code
    b) It defines the block
    c) It returns the block
    d) It exits the method

Answers Table

QnoAnswer (Option with the text)
1a) def method_name() end
2a) def
3a) method_name()
4b) To pass a value back from the method
5a) def method_name()
6b) The method will be called successfully
7a) method_name(arg1, arg2)
8a) def method_name(&block)
9a) nil
10b) object.method_name()
11a) def method_name(a = 5)
12a) def method_name(a, b)
13b) The missing arguments will default to nil
14a) *args
15b) return [val1, val2]
16b) An error is raised
17a) nil
18a) method.parameters
19b) It passes control to the block of code
20b) 5
21a) A piece of reusable code passed to a method
22a) method_name {}
23a) `proc = Proc.new {
24a) A lambda checks the number of arguments, while a proc does not
25a) Ruby raises an error
26a) yield
27a) def method_name(&block)
28a) A special type of block that behaves like a method
29a) yield
30a) It invokes the block of code

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top