Rust Collections Quiz

Rust
0 Passed
0% acceptance

40 comprehensive questions on Rust's collection types, covering Vec, HashMap, VecDeque, iteration patterns, and capacity management — with 11 code examples demonstrating practical collection usage.

40 Questions
~80 minutes
1

Question 1

What is a Vec in Rust?

A
A growable array that stores elements of the same type
B
A fixed-size array
C
A linked list
D
A hash table
2

Question 2

How do you create an empty Vec?

A
Vec::new()
B
vec![]
C
Vec::empty()
D
Both A and B
3

Question 3

How do you create a Vec with initial values?

A
vec![1, 2, 3]
B
Vec::from([1, 2, 3])
C
Both A and B
D
Neither
4

Question 4

What will this code do?

rust
fn main() {
    let v = vec![1, 2, 3];
    println!("First element: {}", v[0]);
}
A
Print 'First element: 1'
B
Compilation error
C
Runtime panic
D
Print nothing
5

Question 5

What happens when you access an invalid index in a Vec?

A
Runtime panic
B
Compilation error
C
Returns default value
D
Wraps around
6

Question 6

How do you safely access Vec elements?

A
get() method returns Option<&T>
B
at() method
C
safe_get() method
D
Direct indexing only
7

Question 7

How do you add elements to a Vec?

A
push() method
B
add() method
C
append() method
D
insert() method
8

Question 8

What does pop() do on a Vec?

A
Removes and returns the last element
B
Removes the first element
C
Clears the entire vector
D
Returns the last element without removing
9

Question 9

How do you iterate over a Vec?

A
for element in &vec { ... }
B
for i in 0..vec.len() { ... }
C
Both A and B
D
Neither
10

Question 10

What is the difference between iter() and iter_mut()?

A
iter() borrows immutably, iter_mut() mutably
B
iter() is faster
C
They are identical
D
iter_mut() doesn't exist
11

Question 11

What will this code do?

rust
fn main() {
    let mut v = vec![1, 2, 3];
    for x in &mut v {
        *x += 1;
    }
    println!("{:?}", v);
}
A
Print [2, 3, 4]
B
Compilation error
C
Print [1, 2, 3]
D
Runtime panic
12

Question 12

What is a HashMap in Rust?

A
A collection of key-value pairs with fast lookup
B
A sorted map
C
A vector of pairs
D
A linked list
13

Question 13

How do you create an empty HashMap?

A
HashMap::new()
B
hashmap!{}
C
Both A and B
D
Neither
14

Question 14

How do you insert into a HashMap?

A
insert() method returns Option<V>
B
put() method
C
add() method
D
set() method
15

Question 15

How do you get a value from a HashMap?

A
get() returns Option<&V>
B
get_mut() for mutable access
C
Both A and B
D
Direct indexing with []
16

Question 16

What happens when you access a non-existent key with []?

A
Runtime panic
B
Returns None
C
Compilation error
D
Returns default value
17

Question 17

How do you check if a key exists in a HashMap?

A
contains_key() method
B
has_key() method
C
exists() method
D
key_exists() method
18

Question 18

What does entry() method do?

A
Provides Entry API for conditional insertion
B
Returns a single entry
C
Lists all entries
D
Removes an entry
19

Question 19

What will this code do?

rust
use std::collections::HashMap;
fn main() {
    let mut scores = HashMap::new();
    scores.insert("Alice", 10);
    scores.insert("Alice", 20);
    println!("Alice's score: {}", scores[&"Alice"]);
}
A
Print 'Alice's score: 20'
B
Print 'Alice's score: 10'
C
Compilation error
D
Runtime panic
20

Question 20

What is a VecDeque?

A
A double-ended queue with efficient front/back operations
B
A circular buffer
C
A priority queue
D
A stack
21

Question 21

How do you add to the front of a VecDeque?

A
push_front() method
B
push() method
C
add_front() method
D
Cannot add to front
22

Question 22

What does pop_back() do on VecDeque?

A
Removes and returns the back element
B
Removes the front element
C
Returns without removing
D
Clears the deque
23

Question 23

When should you use VecDeque instead of Vec?

A
When you need efficient front operations
B
When elements are always added to the end
C
When you need random access
D
Always
24

Question 24

What is capacity in collections?

A
Allocated memory size without reallocation
B
Number of elements stored
C
Maximum possible size
D
Memory used by elements
25

Question 25

How do you reserve capacity in a Vec?

A
reserve() or with_capacity()
B
allocate() method
C
capacity() method
D
Cannot reserve capacity
26

Question 26

What happens when you exceed Vec capacity?

A
Automatic reallocation with more capacity
B
Panic
C
Compilation error
D
Data loss
27

Question 27

How do you shrink a Vec to fit its contents?

A
shrink_to_fit() method
B
shrink() method
C
fit() method
D
Cannot shrink capacity
28

Question 28

What is the difference between len() and capacity()?

A
len() is elements used, capacity() is allocated space
B
They are identical
C
capacity() is elements used
D
len() is allocated space
29

Question 29

How do you iterate over a HashMap?

A
for (key, value) in &map { ... }
B
for element in map { ... }
C
for i in 0..map.len() { ... }
D
HashMaps cannot be iterated
30

Question 30

What does into_iter() do?

A
Consumes the collection, returning owned elements
B
Borrows immutably
C
Borrows mutably
D
Creates a copy
31

Question 31

How do you sort a Vec?

A
sort() method
B
sort_by() for custom comparison
C
Both A and B
D
Vec cannot be sorted
32

Question 32

What does retain() do on a Vec?

A
Keeps elements that match a predicate
B
Removes all elements
C
Keeps only the first element
D
Duplicates all elements
33

Question 33

How do you remove elements from a Vec by index?

A
remove() shifts elements, swap_remove() swaps with last
B
delete() method
C
erase() method
D
Cannot remove by index
34

Question 34

What is the entry API pattern?

A
map.entry(key).or_insert(value) for conditional insertion
B
map.get_entry(key)
C
map.insert_entry(key, value)
D
Entry API doesn't exist
35

Question 35

How do you clear all elements from a collection?

A
clear() method
B
empty() method
C
reset() method
D
truncate(0)
36

Question 36

What is the most efficient way to build a large Vec?

A
Use with_capacity() then push() elements
B
Push elements without pre-allocation
C
Use collect() on iterator
D
Create with known size
37

Question 37

How do you check if a Vec is empty?

A
is_empty() method
B
len() == 0
C
Both A and B
D
Neither
38

Question 38

What does dedup() do on a Vec?

A
Removes consecutive duplicate elements
B
Removes all duplicate elements
C
Sorts and removes duplicates
D
Adds duplicate elements
39

Question 39

How do you convert between collections?

A
Use From/Into traits or collect()
B
Direct casting
C
Conversion methods
D
Cannot convert between collections
40

Question 40

When should you use HashMap over Vec?

A
When you need key-based lookup instead of positional access
B
When elements are always accessed sequentially
C
When you need to maintain insertion order
D
Always

QUIZZES IN Rust