What does the == operator do when used between two arrays in Ruby?
It checks if both arrays have the same elements in the same order.
Click to reveal answer
beginner
How do you find the common elements between two arrays in Ruby?
Use the & operator to get the intersection of two arrays.
Click to reveal answer
beginner
What does the | operator do with arrays in Ruby?
It returns a new array with all unique elements from both arrays combined (union).
Click to reveal answer
intermediate
Explain the difference between - and ^ (exclusive or) operations on arrays in Ruby.
- returns elements from the first array that are not in the second. Ruby does not have a built-in ^ operator for arrays, but you can simulate symmetric difference by combining - and |.
Click to reveal answer
intermediate
How can you check if one array is a subset of another in Ruby?
Use array1 - array2 == [] to check if all elements of array1 are in array2.
Click to reveal answer
What will [1, 2, 3] & [2, 3, 4] return in Ruby?
A[2, 3]
B[1, 2, 3, 4]
C[1]
D[4]
✗ Incorrect
The & operator returns the intersection, so only elements common to both arrays: 2 and 3.
Which operator returns all unique elements from two arrays combined?
A-
B&
C|
D==
✗ Incorrect
The | operator returns the union of two arrays with unique elements.
What does [1, 2, 3] - [2] return?
A[2]
B[1, 3]
C[1, 2, 3]
D[]
✗ Incorrect
The - operator removes elements in the second array from the first, so 2 is removed.
How can you check if two arrays have the same elements in the same order?
AUse <code>array1 & array2</code>
BUse <code>array1 - array2</code>
CUse <code>array1 | array2</code>
DUse <code>array1 == array2</code>
✗ Incorrect
The == operator compares arrays element by element in order.
Which expression checks if arr1 is a subset of arr2?
Aarr1 - arr2 == []
Barr1 & arr2 == arr1
Carr1 | arr2 == arr2
Darr1 == arr2
✗ Incorrect
If removing all elements of arr2 from arr1 leaves an empty array, arr1 is a subset of arr2.
Describe how to find the intersection, union, and difference of two arrays in Ruby.
Think about set operations and their Ruby symbols.
You got /3 concepts.
Explain how to check if two arrays are equal and how to verify if one array is a subset of another.
Consider comparing elements and removing elements.
You got /2 concepts.
Practice
(1/5)
1.
Which operator in Ruby returns the common elements between two arrays?
[1, 2, 3] ? [2, 3, 4]
easy
A. & (intersection)
B. | (union)
C. - (difference)
D. == (equality)
Solution
Step 1: Understand the operators
The & operator returns elements common to both arrays.
Step 2: Apply to example arrays
For arrays [1, 2, 3] and [2, 3, 4], common elements are 2 and 3.
Final Answer:
& (intersection) -> Option A
Quick Check:
Common elements = & [OK]
Hint: Use & for common items between arrays [OK]
Common Mistakes:
Confusing union (|) with intersection (&)
Using - for common elements
Thinking == compares elements regardless of order
2.
Which of the following is the correct syntax to find the difference between two arrays a and b in Ruby?
easy
A. a == b
B. a - b
C. a | b
D. a & b
Solution
Step 1: Recall difference operator
The - operator returns elements in a not in b.
Step 2: Confirm syntax correctness
Using a - b is the correct Ruby syntax for difference.
Final Answer:
a - b -> Option B
Quick Check:
Difference = - [OK]
Hint: Use a - b to get items in a but not in b [OK]
Common Mistakes:
Using & instead of - for difference
Using | which is union
Using == which checks equality
3.
What is the output of the following Ruby code?
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
result = a | b
puts result.inspect
medium
A. [1, 2, 3, 4, 5, 6]
B. [3, 4]
C. [1, 2]
D. [1, 2, 3, 4]
Solution
Step 1: Understand the union operator
The | operator combines all unique elements from both arrays.
Step 2: Apply union to arrays
Arrays a and b combined uniquely are [1, 2, 3, 4, 5, 6].
Final Answer:
[1, 2, 3, 4, 5, 6] -> Option A
Quick Check:
Union = | [OK]
Hint: Use | to combine unique elements from both arrays [OK]
Common Mistakes:
Confusing union with intersection
Expecting duplicates in output
Using - which removes elements
4.
Identify the error in the following Ruby code snippet:
a = [1, 2, 3]
b = [2, 3, 4]
result = a & b
puts result
What is the problem?
medium
A. Syntax error due to missing parentheses
B. Outputs all elements from both arrays
C. Runtime error because & is not defined for arrays
D. No error; code runs and outputs common elements
Solution
Step 1: Check syntax and operators
The & operator is valid for arrays to find intersection.
Step 2: Verify output behavior
The code outputs common elements [2, 3] without error.
Final Answer:
No error; code runs and outputs common elements -> Option D
Quick Check:
Intersection & works fine [OK]
Hint: & works for arrays to find common items without syntax errors [OK]