Methods In Ruby

Name: _____________________

Date: _____________________

Instructions: Answer all questions. Write your answers clearly in the space provided.

Question 1:

What is the syntax for defining a method in Ruby?

A. def method_name
B. method method_name
C. function method_name
D. define method_name
Answer: _________
Question 2:

Which keyword is used to call a method in Ruby?

A. call
B. method
C. invoke
D. none of the above
Answer: _________
Question 3:

What does the return keyword do in Ruby methods?

A. Exits the method
B. Continues the method
C. Restarts the method
D. None of the above
E. Exits the method
F. Returns a value
G. Jumps to the next iteration of the loop
H. Resets the method
Answer: _________
Question 4:

In Ruby, how do you define default parameter values for a method?

A. def method(param = value)
B. def method(param: value)
C. def method(param = default)
D. def method(param => value)
Answer: _________
Question 5:

What does the attr_reader method do in Ruby?

A. Defines a reader method for instance variables
B. Defines a writer method for instance variables
C. Defines both reader and writer methods for instance variables
D. None of the above
Answer: _________
Question 6:

Which of the following is a correct way to call a method with arguments in Ruby?

A. method_name(arguments)
B. method_name.arguments
C. MethodName(arguments)
D. None of the above
Answer: _________
Question 7:

What is the purpose of the splat operator (*) in Ruby method definitions?

A. To accept a variable number of arguments
B. To multiply two numbers
C. To access instance variables
D. None of the above
Answer: _________
Question 8:

In Ruby, what does the private keyword do when used in a class?

A. Restricts access to methods below it
B. Allows access to methods below it
C. Makes all methods public
D. None of the above
Answer: _________
Question 9:

In Ruby, what does the `yield` keyword do in a method?

A. Exits the method
B. Calls the block passed to the method
C. Raises an exception
D. None of the above
Answer: _________
Question 10:

What does the `super` keyword do when called inside a method in Ruby?

A. Calls the superclass method with the same name
B. Calls the subclass method with the same name
C. Calls the method again recursively
D. None of the above
Answer: _________
Question 11:

What does the `public` keyword do in Ruby?

A. Makes methods accessible from anywhere in the code
B. Restricts access to methods below it
C. Makes methods accessible from outside the class
D. None of the above
Answer: _________
Question 12:

In Ruby, what is the purpose of the `include` keyword in a class definition?

A. To include a module
B. To include a superclass
C. To include a subclass
D. To include a method
Answer: _________
Question 13:

What does the `protected` keyword do in Ruby?

A. Makes methods accessible from outside the class
B. Restricts access to methods below it
C. Makes methods accessible from anywhere in the code
D. None of the above
Answer: _________
Question 14:

In Ruby, what does the `singleton_method_defined?` method do?

A. Checks if a class method is defined
B. Checks if an instance method is defined
C. Checks if a singleton method is defined
D. Checks if a private method is defined
Answer: _________
Question 15:

What is the purpose of the `public` keyword in Ruby?

A. Makes methods accessible from anywhere in the code
B. Restricts access to methods below it
C. Makes methods accessible from outside the class
D. None of the above
Answer: _________
Question 16:

What does the private keyword do in Ruby?

A. Makes methods accessible from outside the class
B. Restricts access to methods below it
C. Makes methods accessible from anywhere in the code
D. None of the above
Answer: _________
Question 17:

Which of the following is true about optional parameters in Ruby methods?

A. They must be specified during method definition
B. They have default values assigned
C. They are always required
D. They cannot be used with block parameters
Answer: _________
Question 18:

In Ruby, which method is used to check if an object responds to a certain method?

A. method_defined?
B. respond_to?
C. has_method?
D. check_method
Answer: _________
Question 19:

What does the `super` keyword do in Ruby?

A. Calls the method with the same name in the superclass
B. Calls the method with the same name in the subclass
C. Returns the superclass of the current class
D. Jumps to the next iteration of the loop
Answer: _________
Question 20:

What is the purpose of the `alias` keyword in Ruby?

A. To create an alias for a method
B. To define a new method
C. To remove a method
D. To call a method with a different name
Answer: _________
Question 21:

What is the purpose of the yield keyword in Ruby?

A. To exit a method
B. To call another method
C. To define a block
D. To pass control and arguments to a block
Answer: _________
Question 22:

In Ruby, what does the attr_reader method do?

A. Defines a setter method
B. Defines a getter method
C. Defines both getter and setter methods
D. None of the above
Answer: _________
Question 23:

What is the purpose of the alias_method keyword in Ruby?

A. To create an alias for a method
B. To define a new method
C. To remove a method
D. To call a method with a different name
Answer: _________
Question 24:

In Ruby, which keyword is used to define a class method?

A. def self.method_name
B. method method_name
C. class method_name
D. def class.method_name
Answer: _________
Question 25:

Which of the following is used to define a class method in Ruby?

A. def self.method_name
B. method method_name
C. class method_name
D. def method_name
Answer: _________
Question 26:

What is the purpose of the `attr_accessor` method in Ruby?

A. Defines both reader and writer methods for instance variables
B. Defines a reader method for instance variables
C. Defines a writer method for instance variables
D. None of the above
Answer: _________
Question 27:

In Ruby, which method is called automatically when an object is created?

A. initialize
B. constructor
C. create
D. None of the above
Answer: _________
Question 28:

What does the def keyword signify in Ruby?

A. Declares a method
B. Declares a class
C. Declares a variable
D. Declares a module
Answer: _________
Question 29:

In Ruby, which keyword is used to call a method?

A. call
B. method
C. execute
D. invoke
Answer: _________
Question 30:

What is the purpose of the initialize method in Ruby classes?

A. To initialize class variables
B. To define instance variables
C. To define class methods
D. To define instance methods
Answer: _________
Question 31:

What is the result of calling a method in Ruby without parentheses?

A. Error
B. Returns nil
C. Returns the method itself
D. Executes the method
Answer: _________
Question 32:

In Ruby, which method is used to define a getter and setter for an instance variable?

A. get_set
B. attr
C. read_write
D. instance
Answer: _________
Question 33:

Which of the following is a correct way to define a method that takes multiple arguments in Ruby?

A. def method(arg1, arg2)
B. def method(arg1, arg2) end
C. def method(arg1 arg2) end
D. def method(arg1 arg2)
Answer: _________
Question 34:

Which of the following is NOT a valid method definition syntax in Ruby?

A. def method_name end
B. define method_name end
C. define_method(:method_name) { } end
D. def self.method_name end
Answer: _________

Answer Key

1: A
2: D
3: A, F
4: A
5: A
6: A
7: A
8: A
9: B
10: A
11: A
12: A
13: B
14: C
15: A
16: B
17: B
18: B
19: A
20: A
21: D
22: B
23: A
24: A
25: A
26: A
27: A
28: A
29: D
30: A
31: D
32: B
33: A
34: B