What does the length method do when called on an array in Ruby?
It returns the number of elements in the array. Think of it like counting how many items are in a basket.
Click to reveal answer
beginner
How does the include? method work on an array?
It checks if a specific item is inside the array and returns true if found, otherwise false. Like asking, "Is this toy in my box?"
Click to reveal answer
intermediate
What is the purpose of the flatten method on an array?
It takes an array that has other arrays inside it and turns it into one simple list without nested arrays. Like spreading out a stack of papers into one flat pile.
Click to reveal answer
beginner
What will [1, 2, 3].include?(2) return?
It will return true because the number 2 is inside the array.
Click to reveal answer
intermediate
What is the result of [1, [2, 3], 4].flatten?
The result is [1, 2, 3, 4]. The nested array [2, 3] is flattened into the main array.
Click to reveal answer
What does array.length return?
AThe first element of the array
BThe last element of the array
CThe number of elements in the array
DTrue if array is empty
✗ Incorrect
length counts how many items are in the array.
What will ["apple", "banana"].include?("banana") return?
Atrue
Bfalse
Cnil
Derror
✗ Incorrect
include? returns true if the item is found in the array.
What does flatten do to an array?
AReverses the array
BSorts the array
CRemoves duplicate elements
DTurns nested arrays into a single flat array
✗ Incorrect
flatten removes nested arrays by combining all elements into one list.
What is the output of [1, 2, 3].length?
A3
B2
C1
D0
✗ Incorrect
The array has 3 elements, so length returns 3.
What will [1, [2, [3, 4]]].flatten return?
A[1, 2, [3, 4]]
B[1, 2, 3, 4]
C[[1, 2], 3, 4]
D[1, [2, 3, 4]]
✗ Incorrect
flatten flattens all levels of nesting by default, resulting in [1, 2, 3, 4].
Explain how you would check if an array contains a specific item in Ruby.
Think about asking if your list has a certain thing inside.
You got /3 concepts.
Describe what happens when you use the flatten method on an array with nested arrays.
Imagine spreading out a stack of papers into one flat pile.
You got /3 concepts.
Practice
(1/5)
1. What does the length method do when called on an array in Ruby?
easy
A. Returns the number of elements in the array
B. Checks if an element exists in the array
C. Flattens nested arrays into a single array
D. Removes duplicate elements from the array
Solution
Step 1: Understand the purpose of length
The length method counts how many items are inside an array.
Step 2: Compare with other methods
include? checks for presence, flatten merges nested arrays, so they do different things.
Final Answer:
Returns the number of elements in the array -> Option A
Quick Check:
length = count items [OK]
Hint: Remember length counts items, not checks or changes arrays [OK]
Common Mistakes:
Confusing length with include? method
Thinking length flattens nested arrays
Assuming length removes duplicates
2. Which of the following is the correct syntax to check if the array arr includes the number 5?
easy
A. arr.include(5)
B. arr.includes?(5)
C. arr.has?(5)
D. arr.include?(5)
Solution
Step 1: Recall the correct method name and syntax
The method to check presence is include? with a question mark at the end.
Step 2: Verify method call on array
It should be called as arr.include?(5), not include or includes?.
Final Answer:
arr.include?(5) -> Option D
Quick Check:
Correct method is include? with question mark [OK]
Hint: Remember Ruby methods with ? end with a question mark [OK]
Common Mistakes:
Omitting the question mark in include?
Using incorrect method names like includes? or has?