Classes And Objects In Ruby

Name: _____________________

Date: _____________________

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

Question 1:

Which keyword is used to refer to the current instance of a class in Ruby?

A. self
B. instance
C. @
D. this
Answer: _________
Question 2:

What is the method used to define a class variable in Ruby?

A. @@variable_name
B. @variable_name
C. def variable_name
D. self.variable_name
Answer: _________
Question 3:

What is the method used to access class variables in Ruby?

A. Using accessor methods
B. Using instance methods
C. Using class methods
D. Using global variables
Answer: _________
Question 4:

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

A. To create getter and setter methods for instance variables
B. To define class methods
C. To define instance methods
D. To create new instances
Answer: _________
Question 5:

What is the syntax for defining an instance method in Ruby?

A. def method_name
B. def self.method_name
C. def method_name.self
D. def method_name.static
Answer: _________
Question 6:

Which method is used to define getter and setter methods for instance variables in Ruby?

A. attr_accessor
B. get_set
C. setter_getter
D. set_get
Answer: _________
Question 7:

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

A. To create only getter methods for instance variables
B. To create only setter methods for instance variables
C. To create both getter and setter methods for instance variables
D. To create instance methods
Answer: _________
Question 8:

Which keyword is used to call a method on an object in Ruby?

A. call
B. invoke
C. execute
D. none of the above
Answer: _________
Question 9:

What is the syntax for defining a class in Ruby?

A. class MyClass
B. class
C. def class
D. define class
Answer: _________
Question 10:

How do you access class variables in Ruby?

A. Using accessor methods
B. Using instance methods
C. Using class methods
D. Using global variables
Answer: _________
Question 11:

What is the method used to define instance methods in Ruby?

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

What is the keyword used to refer to the current instance of a class in Ruby?

A. self
B. this
C. current
D. object
Answer: _________
Question 13:

Which method is used to define class variables in Ruby?

A. @@variable
B. @variable
C. $variable
D. variable
Answer: _________
Question 14:

What does the private keyword do in a Ruby class?

A. It makes methods private, accessible only within the class
B. It makes methods public, accessible from outside the class
C. It makes methods protected, accessible only within the class and its subclasses
D. It defines class variables
Answer: _________
Question 15:

What does the protected keyword do in a Ruby class?

A. It makes methods protected, accessible only within the class and its subclasses
B. It makes methods private, accessible only within the class
C. It makes methods public, accessible from outside the class
D. It defines class variables
Answer: _________
Question 16:

How do you access instance variables in Ruby?

A. Using accessor methods
B. Using instance methods
C. Using class methods
D. Using global variables
Answer: _________
Question 17:

What is the method used to define instance variables in Ruby?

A. @variable_name
B. def variable_name
C. self.variable_name
D. instance.variable_name
Answer: _________
Question 18:

Which keyword is used to inherit a class in Ruby?

A. inherit
B. superclass
C. extends
D. <
Answer: _________
Question 19:

What does the super keyword do in Ruby classes?

A. Calls the method of the superclass with the same name
B. Calls the method of the subclass with the same name
C. Exits the method
D. Returns a value
Answer: _________
Question 20:

What is the syntax for defining a class method in Ruby?

A. def self.method_name
B. def class.method_name
C. def method_name.self
D. def method_name.static
Answer: _________
Question 21:

What is the method used to define a class method in Ruby?

A. def self.method_name
B. def class.method_name
C. def method_name.self
D. def method_name.static
Answer: _________
Question 22:

What is the method used to create a new instance of a class in Ruby?

A. new
B. create
C. initialize
D. instance
Answer: _________
Question 23:

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

A. To initialize instance variables with default values
B. To define instance methods
C. To create new instances
D. To initialize class variables
Answer: _________
Question 24:

What is the method used to access instance variables in Ruby?

A. Using accessor methods
B. Using instance methods
C. Using class methods
D. Using global variables
Answer: _________
Question 25:

Which keyword is used to define a class in Ruby?

A. class MyClass
B. class
C. def class
D. define class
Answer: _________
Question 26:

Which method is used to define a class method in Ruby?

A. def self.method_name
B. def class.method_name
C. def method_name.self
D. def method_name.static
Answer: _________
Question 27:

What is the keyword used to define a class in Ruby?

A. class
B. def class
C. class def
D. define_class
Answer: _________
Question 28:

Which keyword is used to create a new instance of a class in Ruby?

A. new
B. create
C. instantiate
D. instance
Answer: _________
Question 29:

How do you define a constructor in Ruby?

A. def initialize
B. def constructor
C. def init
D. def constructor()
Answer: _________
Question 30:

Which of the following is used to access instance variables in Ruby?

A. @variable
B. $variable
C. @@variable
D. variable
Answer: _________
Question 31:

What does the attr_accessor method do in Ruby?

A. Creates getter and setter methods for an instance variable
B. Creates only getter methods for an instance variable
C. Creates only setter methods for an instance variable
D. Creates private methods for an instance variable
Answer: _________
Question 32:

What is the purpose of the initialize method in Ruby?

A. To initialize class variables
B. To define instance methods
C. To create object instances
D. To initialize object properties
Answer: _________
Question 33:

What is the method used to inherit from a superclass in Ruby?

A. <
B. -<
C. extends
D. inherit
Answer: _________
Question 34:

What does the super keyword do in Ruby?

A. Calls the method of the superclass with the same name
B. Calls the method of the subclass with the same name
C. Exits the method
D. Returns a value
Answer: _________
Question 35:

What is the method used to check if an object is an instance of a particular class in Ruby?

A. instance_of?
B. is_instance?
C. is_a?
D. object_of?
Answer: _________
Question 36:

What is the method used to define class methods in Ruby?

A. def self.method_name
B. def class.method_name
C. def method_name.self
D. def method_name.static
Answer: _________

Answer Key

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