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
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
What keyword is used to define a method in Ruby? a) def b) method c) function d) define
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
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
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()
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
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]
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)
What is the default return value of a method in Ruby if no explicit return is specified? a) nil b) false c) 0 d) ""
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
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)
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]
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
Which of the following allows defining a variable number of parameters in Ruby? a) *args b) *var c) ...args d) var
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)
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
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) ""
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
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
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)
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
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{};
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 }
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
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
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
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{}
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
How can you pass a block to a method and use it inside that method? a) yield b) call c) exec d) block
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
Qno
Answer (Option with the text)
1
a) def method_name() end
2
a) def
3
a) method_name()
4
b) To pass a value back from the method
5
a) def method_name()
6
b) The method will be called successfully
7
a) method_name(arg1, arg2)
8
a) def method_name(&block)
9
a) nil
10
b) object.method_name()
11
a) def method_name(a = 5)
12
a) def method_name(a, b)
13
b) The missing arguments will default to nil
14
a) *args
15
b) return [val1, val2]
16
b) An error is raised
17
a) nil
18
a) method.parameters
19
b) It passes control to the block of code
20
b) 5
21
a) A piece of reusable code passed to a method
22
a) method_name {}
23
a) `proc = Proc.new {
24
a) A lambda checks the number of arguments, while a proc does not
25
a) Ruby raises an error
26
a) yield
27
a) def method_name(&block)
28
a) A special type of block that behaves like a method