11
What is the purpose of the attr_accessor method in Ruby classes?
A)
To define constructor methods for class attributes
B)
To define only a writer method for class attributes
C)
To define both reader and writer methods for class attributes
D)
To define only a reader method for class attributes
Answer & Solution
Answer: Option
C
12
What is the difference between attr_reader, attr_writer, and attr_accessor in Ruby?
A)
attr_reader defines both reader and writer methods, attr_writer defines only reader methods, and attr_accessor defines only writer methods
B)
attr_reader defines both reader and writer methods, attr_writer defines only writer methods, and attr_accessor defines only reader methods
C)
attr_reader defines only writer methods, attr_writer defines both reader and writer methods, and attr_accessor defines only reader methods
D)
attr_reader defines only reader methods, attr_writer defines only writer methods, and attr_accessor defines both
Answer & Solution
Answer: Option
D
13
What is the main advantage of using OOP principles in programming?
A)
Modularity and reusability
B)
Improved performance
C)
Simplicity and ease of use
D)
Reduced memory usage
Answer & Solution
Answer: Option
A
14
What is the purpose of the include keyword in Ruby classes?
A)
To create a new instance
B)
To define a new method
C)
To inherit from another class
D)
To mix in methods from a module into a class
Answer & Solution
Answer: Option
D
15
What does the term "object" refer to in OOP?
A)
A method in a class
B)
A variable in a class
C)
An instance of a class
D)
A module in a class
Answer & Solution
Answer: Option
C
16
What is the purpose of inheritance in OOP?
A)
To hide the implementation details
B)
To allow a class to inherit properties and behaviors from another class
C)
To restrict access to certain methods
D)
None of the above
Answer & Solution
Answer: Option
B
17
Which OOP principle emphasizes the hiding of implementation details and showing only necessary features?
A)
Polymorphism
B)
Encapsulation
C)
Abstraction
D)
Inheritance
Answer & Solution
Answer: Option
C
18
Which concept in OOP allows objects to exhibit multiple behaviors based on their types or class?
A)
Abstraction
B)
Inheritance
C)
Encapsulation
D)
Polymorphism
Answer & Solution
Answer: Option
D
19
Which OOP principle promotes reusability and reduces redundancy in code?
A)
Inheritance
B)
Abstraction
C)
Polymorphism
D)
Encapsulation
Answer & Solution
Answer: Option
A
20
What does the attr_accessor method do in Ruby OOP?
A)
Creates constructor methods
B)
Creates only a writer method
C)
Creates only a reader method
D)
Creates both reader and writer methods for class attributes
Answer & Solution
Answer: Option
D