MCQs on Advanced Enumerables and Enumerators | Ruby

Mastering advanced enumerables and enumerators in Ruby is essential for efficient data manipulation. Learn about Custom Enumerables, Lazy Enumerators, and how to work with Infinite Collections for optimized performance.


Custom Enumerables (Questions 1-10)

  1. Which of the following is the correct way to define a custom enumerable in Ruby?
    a) class MyClass; include Enumerable; end
    b) class MyClass; extend Enumerable; end
    c) class MyClass; include Enum; end
    d) class MyClass; extend Enum; end
  2. What is required when implementing the Enumerable module in Ruby?
    a) Defining the each method
    b) Defining the map method
    c) Defining the select method
    d) Defining the collect method
  3. Which of the following methods is available to a class after including the Enumerable module?
    a) each
    b) each_with_index
    c) map
    d) All of the above
  4. How do you define the each method in a custom enumerable?
    a) Define it as a class method
    b) Define it as an instance method
    c) Define it as a class variable
    d) Define it as a module method
  5. Which of the following Ruby methods can be used with custom enumerables?
    a) collect
    b) find
    c) any?
    d) All of the above
  6. When you include the Enumerable module in a class, what is the primary purpose of the each method?
    a) To iterate over the collection
    b) To filter elements
    c) To modify elements
    d) To count elements
  7. In a custom enumerable, what does the all? method do?
    a) Returns true if all elements are true
    b) Returns true if any element is true
    c) Returns false if all elements are false
    d) Returns false if any element is false
  8. Which method in Enumerable returns the first element that satisfies a condition?
    a) find
    b) map
    c) select
    d) reject
  9. How does the inject method work in a custom enumerable?
    a) It transforms elements into a new collection
    b) It combines elements to return a single value
    c) It filters elements based on a condition
    d) It iterates over the collection
  10. Which of the following methods is NOT part of the Enumerable module in Ruby?
    a) each
    b) any?
    c) add
    d) collect

Lazy Enumerators (Questions 11-20)

  1. What is a lazy enumerator in Ruby?
    a) An enumerator that processes all elements immediately
    b) An enumerator that processes elements only when required
    c) An enumerator that never processes any elements
    d) An enumerator that throws an error on large collections
  2. Which method in Ruby is used to create a lazy enumerator?
    a) to_lazy
    b) lazy
    c) lazy_enum
    d) enumerate
  3. Which of the following is an advantage of using lazy enumerators in Ruby?
    a) Increased memory usage
    b) Processing elements only when needed
    c) Decreased performance
    d) Immediate collection of all elements
  4. How does the lazy method affect the behavior of enumerables in Ruby?
    a) It processes all elements immediately
    b) It allows operations to be chained without executing until required
    c) It skips all elements
    d) It sorts the collection
  5. What happens when you call lazy on an array in Ruby?
    a) It converts the array into a lazy enumerable
    b) It creates a copy of the array
    c) It makes the array immutable
    d) It filters the array’s elements
  6. Which of the following methods can be used with a lazy enumerator?
    a) map
    b) select
    c) take
    d) All of the above
  7. What is the primary benefit of chaining methods like map, select, and take on a lazy enumerator?
    a) They all execute immediately and store results
    b) They allow for deferred evaluation
    c) They increase memory usage
    d) They prevent the use of multiple enumerators
  8. Which of the following operations will trigger the evaluation of a lazy enumerator?
    a) each
    b) map
    c) to_a
    d) select
  9. How does the take method behave in the context of a lazy enumerator?
    a) It takes a specified number of elements from the collection
    b) It skips a specified number of elements
    c) It sorts the collection
    d) It executes all methods on the enumerator
  10. In which scenario would a lazy enumerator be most beneficial?
    a) When working with small datasets
    b) When memory consumption is a concern with large datasets
    c) When immediate processing of all elements is required
    d) When filtering a collection

Working with Infinite Collections (Questions 21-30)

  1. How do you create an infinite collection in Ruby?
    a) Using the Range.new method
    b) Using the loop method
    c) Using the Enumerator.new method with an infinite block
    d) Using infinite method
  2. Which method in Ruby can be used to create an infinite sequence of numbers?
    a) to_enum
    b) Enumerator.new
    c) infinite
    d) each
  3. What does the following code return in Ruby?rubyCopy codeEnumerator.new { |y| y.yield 1; y.yield 2; y.yield 3 } a) A range of numbers
    b) An infinite enumerator
    c) An enumerator that returns 1, 2, 3
    d) An error
  4. How can you limit the number of elements from an infinite collection in Ruby?
    a) Using the take method
    b) Using the limit method
    c) Using the first method
    d) Using the map method
  5. Which of the following is an example of an infinite collection in Ruby?
    a) (1..)
    b) 1..Float::INFINITY
    c) (1...infinite)
    d) Both a and b
  6. Which of the following methods can be used to take elements from an infinite collection?
    a) first
    b) each
    c) take
    d) drop
  7. In Ruby, how do you create a lazy infinite sequence of even numbers?
    a) Enumerator.new { |y| y.yield 2; y.yield 4; y.yield 6 }
    b) (2..Float::INFINITY).lazy.select(&:even?)
    c) Enumerable.new { |y| y.yield 2; y.yield 4; y.yield 6 }
    d) 2.upto(Float::INFINITY)
  8. What is the purpose of the lazy method when working with infinite collections?
    a) It allows only the required elements to be evaluated
    b) It computes all elements immediately
    c) It limits the range of elements
    d) It creates a fixed-length collection
  9. Which Ruby method can be used to produce a finite collection from an infinite collection?
    a) take
    b) first
    c) slice
    d) select
  10. How can you generate an infinite collection of Fibonacci numbers in Ruby?
    a) Enumerator.new { |y| y.yield 1; y.yield 1; y.yield 2 }
    b) (1..).lazy.select { |x| x % 2 == 0 }
    c) Enumerator.new { |y| a, b = 0, 1; loop { y.yield a; a, b = b, a + b } }
    d) (1...infinite).lazy

Answer Key

QNoAnswer (Option with Text)
1a) class MyClass; include Enumerable; end
2a) Defining the each method
3d) All of the above
4b) Define it as an instance method
5d) All of the above
6a) To iterate over the collection
7a) Returns true if all elements are true
8a) find
9b) It combines elements to return a single value
10c) add
11b) An enumerator that processes elements only when required
12b) lazy
13b) Processing elements only when needed
14b) It allows operations to be chained without executing until required
15a) It converts the array into a lazy enumerable
16d) All of the above
17b) They allow for deferred evaluation
18c) to_a
19a) It takes a specified number of elements from the collection
20b) When memory consumption is a concern with large datasets
21c) Using the Enumerator.new method with an infinite block
22b) Enumerator.new
23c) An enumerator that returns 1, 2, 3
24a) Using the take method
25d) Both a and b
26c) take
27b) (2..Float::INFINITY).lazy.select(&:even?)
28a) It allows only the required elements to be evaluated
29a) take
30c) `Enumerator.new {

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