Data Structures Quiz
Test your knowledge of Ruby data structures! This comprehensive quiz covers ranges, sets, structs, OpenStruct, queues, stacks, and enumerators. Challenge yourself with practical questions and code examples.
Quiz Instructions
- Answer each question to the best of your ability
- Try to predict the output before running the code
- Use the interactive code runner to test your answers
- Review the explanations to deepen your understanding
- Take your time and think through each concept
Question 1: Ranges
What will be the output of the following code? Think about inclusive vs exclusive ranges.
Your Answer: [Write your prediction here]
Explanation: Inclusive ranges (..) include the end value, while exclusive ranges (...) exclude it. The size method returns the number of elements in the range.
Interactive Code Runner
Ruby Code Editor
Question 2: Sets
What will be the result of these set operations? Remember that sets automatically remove duplicates.
Your Answer: [Write your prediction here]
Explanation: | is union (all elements), & is intersection (common elements), - is difference (elements in A but not B), and ^ is symmetric difference (elements in either set but not both).
Interactive Code Runner
Ruby Code Editor
Question 3: Structs
What will be the output of this struct code? Consider how structs handle equality and conversion.
Your Answer: [Write your prediction here]
Explanation: Structs automatically provide equality comparison based on attribute values, to_h converts to hash, and members returns the attribute names.
Interactive Code Runner
Ruby Code Editor
Question 4: OpenStruct
What will happen when we try to access attributes on this OpenStruct? Think about dynamic attribute creation.
Your Answer: [Write your prediction here]
Explanation: OpenStruct allows dynamic attribute creation. respond_to? returns true for attributes that have been set and false for those that haven't.
Interactive Code Runner
Ruby Code Editor
Question 5: Queue & Stack
What will be the order of elements when we pop from this queue and stack? Remember FIFO vs LIFO.
Your Answer: [Write your prediction here]
Explanation: Queue follows FIFO (First In, First Out) - elements are removed in the order they were added. Stack follows LIFO (Last In, First Out) - elements are removed in reverse order.
Interactive Code Runner
Ruby Code Editor
Question 6: Enumerators
What will be the output of this enumerator code? Consider lazy evaluation and infinite sequences.
Your Answer: [Write your prediction here]
Explanation: The Fibonacci enumerator creates an infinite sequence. take(8) gets the first 8 values. next advances the enumerator, peek shows the next value without advancing.
Interactive Code Runner
Ruby Code Editor
Final Challenge: Data Structure Combination
This challenge combines multiple data structures. Can you predict what this code will do?
Your Answer: [Write your prediction here]
Explanation: This combines structs, sets, OpenStruct, and ranges. Sets remove duplicate structs (based on equality), OpenStruct allows dynamic attributes, and ranges can be converted to arrays.
Interactive Code Runner
Ruby Code Editor
Quiz Summary & Key Concepts
Data Structure Types
- Ranges: Inclusive (..) vs Exclusive (...)
- Sets: Unique collections with set operations
- Structs: Lightweight data containers
- OpenStruct: Dynamic attribute objects
- Queues: FIFO (First In, First Out)
- Stacks: LIFO (Last In, First Out)
- Enumerators: Lazy evaluation and iteration
Key Operations
- Set Operations: Union (|), Intersection (&), Difference (-)
- Struct Methods: to_h, members, values, equality
- OpenStruct: Dynamic attributes, respond_to?
- Queue/Stack: push/pop, enqueue/dequeue
- Enumerators: next, peek, rewind, take
- Lazy Evaluation: Memory-efficient processing
Congratulations! You've completed the Ruby Data Structures Quiz. These concepts are fundamental to effective Ruby programming. Practice with real-world examples and experiment with different combinations of data structures!