MCQs on Object-Oriented Programming in Ruby | Ruby

Explore core Object-Oriented Programming (OOP) concepts in Ruby, from classes and objects to inheritance and polymorphism. Learn how to structure your Ruby applications with these essential OOP principles.


MCQs on Object-Oriented Programming in Ruby

Section 1: Introduction to OOP Concepts (6 Questions)

  1. What is the core principle of Object-Oriented Programming (OOP)?
    • a) Data is isolated in functions
    • b) Data is organized around objects and classes
    • c) Data is processed in a sequence
    • d) Functions operate on global data
  2. Which of the following is not an OOP principle?
    • a) Encapsulation
    • b) Inheritance
    • c) Polymorphism
    • d) Compilation
  3. In OOP, what does encapsulation refer to?
    • a) Using classes to organize data
    • b) Hiding data within objects and restricting access
    • c) Allowing classes to inherit from other classes
    • d) Defining multiple methods for the same functionality
  4. What is the primary purpose of inheritance in OOP?
    • a) To avoid code repetition by reusing properties and methods
    • b) To hide data within a class
    • c) To define new data types
    • d) To define an interface for classes
  5. Which of the following OOP concepts is implemented through the use of polymorphism?
    • a) Different methods having the same name but different implementations
    • b) Classes inheriting properties from parent classes
    • c) Restricting access to object properties
    • d) Wrapping data and functions into a single unit
  6. In Ruby, which keyword is used to define a class?
    • a) object
    • b) def
    • c) class
    • d) module

Section 2: Classes and Objects (6 Questions)

  1. In Ruby, how do you instantiate an object of a class?
    • a) new ClassName()
    • b) ClassName.new
    • c) object.new ClassName()
    • d) new ClassName
  2. What is the primary purpose of a constructor in a Ruby class?
    • a) To define the properties of a class
    • b) To initialize objects of a class
    • c) To invoke methods of the class
    • d) To change the class variables
  3. In Ruby, what method is used to initialize an object of a class?
    • a) initialize()
    • b) new()
    • c) start()
    • d) create()
  4. Which of the following is a valid way to access an object’s instance variable in Ruby?
    • a) object.variable_name
    • b) object.get_variable_name
    • c) object.variable_name()
    • d) object.@variable_name
  5. What happens if you call a method on an object that is not defined in its class?
    • a) Ruby will search for it in the parent class
    • b) Ruby will throw an error immediately
    • c) Ruby will return nil
    • d) Ruby will attempt to create the method dynamically
  6. Which of the following is used to access the class of an object in Ruby?
    • a) object.class
    • b) class(object)
    • c) object.instance_of?
    • d) object.define_class

Section 3: Instance Variables and Methods (6 Questions)

  1. What is an instance variable in Ruby?
    • a) A variable shared by all objects of a class
    • b) A variable that stores methods within an object
    • c) A variable whose value is accessible by all classes
    • d) A variable that is unique to each object of a class
  2. How are instance variables defined in Ruby?
    • a) By using @ before the variable name
    • b) By using $ before the variable name
    • c) By using @ before the method name
    • d) By using @@ before the variable name
  3. Which of the following correctly defines an instance method in Ruby?
    • a) def method_name
    • b) method method_name
    • c) def method_name()
    • d) function method_name()
  4. In Ruby, an instance variable’s value can be accessed or modified using which method?
    • a) Getter and setter methods
    • b) Direct access within the class
    • c) Only getter methods
    • d) Public methods
  5. What will happen if you attempt to access an uninitialized instance variable in Ruby?
    • a) It will throw an error
    • b) It will return nil
    • c) It will initialize the variable automatically
    • d) It will cause an infinite loop
  6. How do you define a getter method for an instance variable in Ruby?
    • a) def variable_name
    • b) def get_variable_name
    • c) def variable_name()
    • d) def variable_name; end

Section 4: Class Variables and Methods (6 Questions)

  1. In Ruby, how do you define a class variable?
    • a) By using @ before the variable name
    • b) By using @@ before the variable name
    • c) By using $ before the variable name
    • d) By using class_variable syntax
  2. What is the scope of a class variable in Ruby?
    • a) It is accessible only inside the class
    • b) It is accessible across all instances of the class and subclasses
    • c) It is accessible only inside one object
    • d) It can only be accessed from outside the class
  3. What keyword is used to define a class method in Ruby?
    • a) class_method
    • b) def
    • c) self
    • d) static
  4. What will be the output of the following Ruby code?rubyCopy codeclass MyClass @@class_variable = 5 def self.print_class_variable puts @@class_variable end end MyClass.print_class_variable
    • a) nil
    • b) 5
    • c) MyClass
    • d) Error
  5. Which of the following is the correct way to define a class method in Ruby?
    • a) def MyClass.method_name
    • b) def self.method_name
    • c) def method_name(self)
    • d) def method_name::self
  6. How can you access a class variable from an instance method in Ruby?
    • a) By using @@variable_name
    • b) By using @variable_name
    • c) By using variable_name
    • d) By using self.variable_name

Section 5: Inheritance and Polymorphism (6 Questions)

  1. What does inheritance allow a class to do in Ruby?
    • a) It allows a class to inherit methods from another class
    • b) It defines the structure of a class
    • c) It adds data attributes to a class
    • d) It allows classes to merge with each other
  2. How do you define a subclass in Ruby?
    • a) class SubClass < SuperClass
    • b) class SubClass() inherits SuperClass
    • c) class SubClass from SuperClass
    • d) class SubClass: SuperClass
  3. Which Ruby keyword is used to refer to the parent class?
    • a) super
    • b) parent
    • c) class
    • d) self
  4. In Ruby, what does polymorphism allow a method to do?
    • a) It allows a method to be invoked with different arguments
    • b) It allows a method to act differently based on the object’s class
    • c) It allows multiple methods with the same name
    • d) It allows a class to inherit methods from other classes
  5. How would you call the superclass method inside a subclass method in Ruby?
    • a) super()
    • b) parent()
    • c) call_super()
    • d) super_method()
  6. Which of the following is an example of polymorphism in Ruby?
    • a) A method that behaves differently based on the class of the object
    • b) A class inheriting from another class
    • c) A method that accepts multiple arguments
    • d) A class that has only one method

Answer Key

QnoAnswer
1b) Data is organized around objects and classes
2d) Compilation
3b) Hiding data within objects and restricting access
4a) To avoid code repetition by reusing properties and methods
5a) Different methods having the same name but different implementations
6c) class
7b) ClassName.new
8b) To initialize objects of a class
9a) initialize()
10a) object.variable_name
11a) Ruby will search for it in the parent class
12a) object.class
13d) A variable that is unique to each object of a class
14a) By using @ before the variable name
15b) It will return nil
16a) Getter and setter methods
17b) It will return nil
18a) def variable_name
19b) By using @@ before the variable name
20b) It is accessible across all instances of the class and subclasses
21c) self
22b) 5
23b) def self.method_name
24a) By using @@variable_name
25a) It allows a class to inherit methods from another class
26a) class SubClass < SuperClass
27a) super
28b) It allows a method to act differently based on the object’s class
29a) super()
30a) A method that behaves differently based on the class of the object

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