Rust provides powerful tools for handling collections and iterating over them, including arrays, slices, vectors, hash maps, and iterators. These features allow for efficient data manipulation and traversal in Rust applications, ensuring performance and safety.
1. Arrays and Slices
In Rust, what is the size of an array known at compile-time? a) Fixed b) Dynamic c) Variable d) Not defined
How do you define an array of 5 integers in Rust? a) let arr = [1, 2, 3, 4, 5]; b) let arr = [1, 2, 3]; c) let arr = [5]; d) let arr = [1..5];
What is the main difference between arrays and slices in Rust? a) Arrays have a fixed size, slices do not b) Arrays cannot be mutable, slices are always mutable c) Slices are arrays with unknown sizes d) Slices are used for static data, arrays for dynamic data
How do you create a slice from an array in Rust? a) arr[..3] b) arr.slice(3) c) arr[3:] d) arr.slice(0, 3)
What is the default size of an array in Rust if no size is specified? a) 0 b) 1 c) It must be specified d) 10
2. Vectors and Their Methods
What is the primary advantage of using vectors over arrays in Rust? a) Fixed size b) Dynamic resizing c) Immutable data d) Better performance
How do you create a vector in Rust? a) let vec = Vec::new(); b) let vec = [1, 2, 3]; c) let vec = vec![1, 2, 3]; d) let vec = Vector::new();
Which method adds an element to the end of a vector in Rust? a) push() b) append() c) insert() d) add()
How can you get the length of a vector in Rust? a) vec.size() b) vec.len() c) vec.length() d) vec.count()
What does the pop() method do in a Rust vector? a) Returns the first element b) Removes the last element and returns it c) Clears the vector d) Adds a new element to the vector
3. Hash Maps and Their Usage
What is the primary use case for a hash map in Rust? a) Storing unique values with an index b) Storing key-value pairs c) Storing large datasets d) Storing ordered data
How do you define a hash map in Rust? a) let map = HashMap::new(); b) let map = HashMap::create(); c) let map = HashMap::with_capacity(); d) let map = map![key => value];
Which method is used to insert a key-value pair into a hash map? a) insert() b) add() c) put() d) set()
How do you access a value by key in a hash map in Rust? a) map[key] b) map.get(key) c) map.access(key) d) map.retrieve(key)
What does the remove() method do in a hash map in Rust? a) Clears all data from the map b) Removes an element by key c) Deletes the entire map d) Removes the last inserted item
4. Iterators and Iterator Methods
What is an iterator in Rust? a) A collection of elements b) A function for handling data c) An object that allows traversal over a collection d) A data structure that cannot be mutated
How do you create an iterator from a vector in Rust? a) vec.into_iter() b) vec.iter() c) vec.create_iterator() d) vec.iterator()
Which method consumes the iterator and returns an array of elements? a) collect() b) into_vec() c) to_array() d) gather()
What is the purpose of the map() method in iterators? a) To filter elements from an iterator b) To transform elements of an iterator c) To merge multiple iterators d) To sort the iterator
Which iterator method is used to filter elements based on a condition? a) filter() b) map() c) flat_map() d) reduce()
5. Advanced Iterator Usage
Which method is used to combine the elements of an iterator into a single value? a) reduce() b) collect() c) sum() d) join()
How do you convert an iterator to a vector? a) iterator.to_vec() b) iterator.collect() c) iterator.convert() d) iterator.toArray()
What is the result of calling next() on an iterator in Rust? a) It returns the next element b) It returns the current element and moves to the next c) It terminates the iterator d) It loops through the collection
Which of the following is true about Rust iterators? a) They are lazy and only produce values when needed b) They eagerly compute all values at once c) They can be used with arrays but not vectors d) They can only be used with hash maps
What is the purpose of the enumerate() method in Rust iterators? a) It tracks the index of each element b) It filters out duplicate values c) It sorts the iterator elements d) It creates a new iterator with modified values
How do you iterate over elements of a vector and get their indices? a) Using iter().enumerate() b) Using iter().index() c) Using vec.forEach() d) Using vec.index()
What does the all() method do for iterators? a) Returns true if all elements satisfy a condition b) Returns the first element c) Combines all elements into one d) Filters the iterator
How do you iterate over a vector while mutating the elements? a) Using iter_mut() b) Using into_iter() c) Using mut_iter() d) Using map_mut()
Which of the following methods is used to join iterator elements into a string? a) join() b) concatenate() c) merge() d) combine()
What is the purpose of the skip() method in iterators? a) It skips the first n elements and returns the rest b) It skips the last n elements and returns the rest c) It skips all elements and returns an empty iterator d) It skips the current element and moves to the next
Answer Table
Qno
Answer
1
a) Fixed
2
a) let arr = [1, 2, 3, 4, 5];
3
a) Arrays have a fixed size, slices do not
4
a) arr[..3]
5
c) It must be specified
6
b) Dynamic resizing
7
c) let vec = vec![1, 2, 3];
8
a) push()
9
b) vec.len()
10
b) Removes the last element and returns it
11
b) Storing key-value pairs
12
a) let map = HashMap::new();
13
a) insert()
14
b) map.get(key)
15
b) Removes an element by key
16
c) An object that allows traversal over a collection
17
b) vec.iter()
18
a) collect()
19
b) To transform elements of an iterator
20
a) filter()
21
a) reduce()
22
b) iterator.collect()
23
a) It returns the next element
24
a) They are lazy and only produce values when needed
25
a) It tracks the index of each element
26
a) Using iter().enumerate()
27
a) Returns true if all elements satisfy a condition
28
a) Using iter_mut()
29
a) join()
30
a) It skips the first n elements and returns the rest