Ruby Arrays Quiz
40 comprehensive questions exploring Ruby array manipulation, indexing, methods, and data structures — with 16 code examples covering array creation, slicing, transformation methods, and nested arrays in this cpp quiz.
Question 1
How do you create a basic array in Ruby?
Question 2
What is array indexing in Ruby and how does negative indexing work?
Question 3
When processing a list of exam scores where you need to add a new score to the end, which array method should you use?
scores = [85, 92, 78]
scores.push(88)Question 4
How does array slicing work with ranges in Ruby?
Question 5
What is the difference between map and select methods in Ruby arrays?
numbers = [1, 2, 3, 4]
even_numbers = numbers.select(&:even?)
doubled = numbers.map { |n| n * 2 }Question 6
In a shopping cart application where items are frequently added and removed from the end, how should you handle the data structure?
Question 7
What does the pop method do and what does it return?
Question 8
How do you create nested arrays (multidimensional arrays) in Ruby?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
element = matrix[1][2]Question 9
What is the difference between inclusive and exclusive ranges when converting to arrays?
Question 10
When processing a queue of customer service requests where new requests are added to the end and processed from the front, which methods should you use?
Question 11
How does the unshift method work compared to push?
arr = [2, 3]
arr.unshift(1)
# arr is now [1, 2, 3]Question 12
In a data analysis application processing sensor readings stored in arrays, how would you filter out invalid readings below a threshold?
Question 13
What happens when you access an array index that is out of bounds?
Question 14
How do you convert a range of numbers directly to an array?
numbers = (1..10).to_a
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Question 15
When implementing a stack data structure where the last added item is processed first, which array methods provide the correct behavior?
Question 16
What does the shift method do in Ruby arrays?
queue = ['first', 'second', 'third']
next_item = queue.shift
# next_item = 'first', queue = ['second', 'third']Question 17
In a web application processing form parameters that arrive as arrays of values, how should you handle multiple selections from checkboxes?
Question 18
What is the difference between map and map! methods?
Question 19
How do you access elements in nested arrays using multiple indices?
grid = [['a', 'b'], ['c', 'd']]
letter = grid[1][0] # 'c'Question 20
When processing large datasets where you need to transform each element without creating intermediate arrays, which method should you choose?
Question 21
What does the include? method check for arrays?
Question 22
In a game leaderboard system where scores need to be kept sorted as new scores are added, how should you maintain the sorted order?
Question 23
How does the compact method work on arrays?
data = [1, nil, 3, nil, 5]
clean = data.compact
# [1, 3, 5]Question 24
What is the behavior of the * operator (splat) when used with arrays?
numbers = [1, 2, 3]
all = [*numbers, 4, 5]
# [1, 2, 3, 4, 5]Question 25
When implementing a circular buffer where you need to efficiently add elements and remove old ones when capacity is reached, which approach should you use?
Question 26
What does the flatten method do to nested arrays?
Question 27
In a data processing pipeline where you need to chain multiple array transformations, how should you combine map and select operations?
Question 28
How do you create an array with default values using Array.new?
zeros = Array.new(5, 0)
# [0, 0, 0, 0, 0]Question 29
What is the difference between uniq and uniq! methods?
Question 30
When processing API responses that contain arrays of objects with nested data structures, how should you extract specific fields from each object?
Question 31
What does the reverse method do to arrays and what type of operation is it?
original = [1, 2, 3]
reversed = original.reverse
# original unchanged, reversed = [3, 2, 1]Question 32
In a caching system where you need to maintain a fixed number of recent items, how should you implement the cache replacement policy?
Question 33
How does the zip method work when combining multiple arrays?
names = ['Alice', 'Bob']
ages = [25, 30]
combined = names.zip(ages)
# [['Alice', 25], ['Bob', 30]]Question 34
When working with sparse arrays where most elements are nil or default values, how should you handle memory efficiently?
Question 35
What does the sample method do for arrays?
Question 36
In a task scheduling system where tasks have priorities and you need to process high-priority tasks first, how should you organize the task array?
Question 37
How does the transpose method work on arrays of arrays?
matrix = [[1, 2, 3], [4, 5, 6]]
transposed = matrix.transpose
# [[1, 4], [2, 5], [3, 6]]Question 38
When implementing a sliding window algorithm for data analysis, how should you efficiently extract consecutive elements?
Question 39
What does the rotate method do to arrays?
Question 40
Considering Ruby's array handling ecosystem as a whole, what fundamental advantage do Ruby arrays provide compared to primitive arrays in other languages?
