MCQs on Performance Optimization | Ruby

Optimizing Ruby code is essential for better performance. This chapter covers profiling tools, garbage collection, memory management, and techniques to enhance Ruby program efficiency. The MCQs below will help assess your understanding.


1. Profiling Ruby Code (Benchmark, memory_profiler)

  1. Which Ruby gem is commonly used to measure the execution time of code?
    • A) memory_profiler
    • B) benchmark
    • C) ruby-prof
    • D) stackprof
  2. How can you measure memory usage in Ruby code?
    • A) Using the benchmark gem
    • B) Using the memory_profiler gem
    • C) Using ObjectSpace
    • D) Using the gc module
  3. What does the Benchmark.measure method return?
    • A) The memory usage of a block of code
    • B) The time taken to execute the block of code
    • C) The number of objects created in the block
    • D) The garbage collection statistics
  4. Which of the following is NOT a valid method in the Benchmark module in Ruby?
    • A) Benchmark.realtime
    • B) Benchmark.measure
    • C) Benchmark.time
    • D) Benchmark.bmbm
  5. What is the primary use of the memory_profiler gem in Ruby?
    • A) To find memory leaks in Ruby programs
    • B) To profile CPU usage
    • C) To optimize method execution time
    • D) To generate garbage collection reports

2. Garbage Collection and Memory Management

  1. In Ruby, what does garbage collection do?
    • A) It optimizes CPU usage
    • B) It frees up memory by removing unreachable objects
    • C) It caches memory for faster access
    • D) It compresses memory to save space
  2. Which method is used to manually trigger garbage collection in Ruby?
    • A) ObjectSpace.garbage_collect
    • B) GC.start
    • C) GC.collect
    • D) ObjectSpace.cleanup
  3. How can you check the current memory usage of your Ruby application?
    • A) Using the ObjectSpace module
    • B) Using the GC module
    • C) Using MemoryStats gem
    • D) Using the memory_profiler gem
  4. What does the GC.stat method return in Ruby?
    • A) The current memory usage
    • B) The garbage collection statistics
    • C) The total time taken by garbage collection
    • D) The memory freed by the garbage collector
  5. Which garbage collection strategy does Ruby use for automatic memory management?
    • A) Reference Counting
    • B) Mark and Sweep
    • C) Tracing Garbage Collection
    • D) Generational Garbage Collection

3. Optimizing Ruby Code

  1. What is the main goal of code optimization in Ruby?
    • A) To reduce memory usage
    • B) To improve execution speed and efficiency
    • C) To increase the number of features
    • D) To make the code more readable
  2. Which of the following is a way to optimize memory usage in Ruby?
    • A) Use more memory-intensive data structures
    • B) Avoid object creation in loops
    • C) Use eval to execute code dynamically
    • D) Use unnecessary nested loops
  3. How can you improve performance when working with large datasets in Ruby?
    • A) Use more memory-heavy data structures
    • B) Use lazy enumerators to process data on demand
    • C) Avoid using indexes
    • D) Avoid loops entirely
  4. Which of the following can improve Ruby performance when accessing large arrays?
    • A) Use Array#each instead of each_with_index
    • B) Use Array#map instead of each
    • C) Avoid arrays and use hashes
    • D) Avoid using loops for array manipulation
  5. How can you avoid memory leaks in Ruby?
    • A) Always free memory manually
    • B) Avoid using blocks
    • C) Avoid creating too many objects
    • D) Use proper object references and rely on garbage collection

4. Profiling and Analyzing Performance Bottlenecks

  1. Which Ruby tool can be used to generate a detailed report on memory usage and object allocations?
    • A) memory_profiler
    • B) ruby-prof
    • C) stackprof
    • D) Benchmark
  2. How can you find the memory consumption of a specific object in Ruby?
    • A) Using ObjectSpace.memsize_of
    • B) Using GC.stat
    • C) Using ObjectSpace.memory_size
    • D) Using MemoryStats gem
  3. What is the ruby-prof gem used for in Ruby?
    • A) To profile the memory usage of objects
    • B) To measure the execution time of Ruby code
    • C) To generate detailed performance profiles for method calls
    • D) To profile database queries
  4. How can you identify performance bottlenecks in your Ruby code?
    • A) By using Benchmark.measure
    • B) By using memory_profiler
    • C) By running your code through ruby-prof
    • D) All of the above
  5. What does the ruby-prof gem provide that is useful for optimization?
    • A) Graphical representations of memory usage
    • B) Detailed method call statistics and execution time
    • C) A summary of code errors
    • D) A way to track variable assignments

5. Advanced Optimization Techniques

  1. Which of the following methods can help reduce method call overhead in Ruby?
    • A) Use Symbol instead of String
    • B) Use Array#map instead of loops
    • C) Use def instead of lambda
    • D) Use caching and memoization for frequent method calls
  2. Which technique is recommended for improving performance in Ruby when handling multiple method calls in a loop?
    • A) Use eval
    • B) Use method aliasing
    • C) Use memoization to store results of expensive computations
    • D) Use global variables instead of local variables
  3. In Ruby, what is the effect of using #freeze on an object?
    • A) It makes the object immutable, preventing modifications
    • B) It improves memory performance by preventing garbage collection
    • C) It increases the object’s allocation size
    • D) It speeds up method execution
  4. What can be a negative side effect of excessive use of the #dup method in Ruby?
    • A) Increased memory usage
    • B) Reduced method call performance
    • C) Reduced garbage collection frequency
    • D) Increased object immutability
  5. How does memoization help in optimizing Ruby code performance?
    • A) By reducing memory usage
    • B) By preventing unnecessary method re-evaluations
    • C) By speeding up method calls using caching
    • D) By optimizing object creation

6. Garbage Collection and Optimization

  1. What is the purpose of using GC.compact in Ruby?
    • A) To reduce the memory usage of the application
    • B) To manually trigger garbage collection
    • C) To optimize memory after garbage collection
    • D) To collect unused variables
  2. What does the ObjectSpace.each_object method allow you to do in Ruby?
    • A) Get the current status of garbage collection
    • B) Iterate through all objects in memory
    • C) Get memory statistics
    • D) Collect objects for garbage collection
  3. Which Ruby command is useful for debugging memory issues in an application?
    • A) ruby-prof
    • B) ObjectSpace.each_object
    • C) memory_profiler
    • D) gc.garbage_collect
  4. How does Ruby’s garbage collector identify objects for removal?
    • A) By analyzing object references and marking unreachable objects
    • B) By checking for variable assignment errors
    • C) By analyzing object creation times
    • D) By using reference counting
  5. What is the effect of the GC::Profiler module in Ruby?
    • A) It tracks and provides garbage collection statistics
    • B) It reduces the frequency of garbage collection
    • C) It increases memory usage
    • D) It freezes objects during garbage collection

Answer Table

QnoAnswer (Option with text)
1B) benchmark
2B) using the memory_profiler gem
3B) The time taken to execute the block of code
4C) Benchmark.time
5A) To find memory leaks in Ruby programs
6B) It frees up memory by removing unreachable objects
7B) GC.start
8D) Using the memory_profiler gem
9B) The garbage collection statistics
10D) Generational Garbage Collection
11B) To improve execution speed and efficiency
12B) Avoid object creation in loops
13B) Use lazy enumerators to process data on demand
14A) Use Array#each instead of each_with_index
15D) Use proper object references and rely on garbage collection
16A) memory_profiler
17A) Using ObjectSpace.memsize_of
18C) To generate detailed performance profiles for method calls
19D) All of the above
20B) Detailed method call statistics and execution time
21D) Use caching and memoization for frequent method calls
22C) Use memoization to store results of expensive computations
23A) It makes the object immutable, preventing modifications
24A) Increased memory usage
25B) By preventing unnecessary method re-evaluations
26C) To optimize memory after garbage collection
27B) Iterate through all objects in memory
28B) ObjectSpace.each_object
29A) By analyzing object references and marking unreachable objects
30A) It tracks and provides garbage collection statistics

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