Rust Iterators Quiz

Rust
0 Passed
0% acceptance

Master Rust's powerful iterator system for efficient data processing and transformation

40 Questions
~80 minutes
1

Question 1

What is the Iterator trait in Rust?

A
A trait that defines how to iterate over a sequence of values
B
A trait for creating loops
C
A trait for collections
D
A trait for functions
2

Question 2

What is the signature of the next() method in Iterator?

A
fn next(&mut self) -> Option<Self::Item>
B
fn next(&self) -> Option<Self::Item>
C
fn next(&mut self) -> Self::Item
D
fn next() -> Option<Self::Item>
3

Question 3

What does this iterator code produce?

rust
let v = vec![1, 2, 3];
let mut iter = v.iter();
println!("{:?}", iter.next());
println!("{:?}", iter.next());
A
Some(1) Some(2)
B
Some(&1) Some(&2)
C
1 2
D
Compilation error
4

Question 4

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

A
iter() borrows elements, into_iter() takes ownership
B
iter() takes ownership, into_iter() borrows
C
They are identical
D
into_iter() doesn't exist
5

Question 5

What are iterator adapters?

A
Methods that transform one iterator into another
B
Methods that consume iterators
C
Methods that create iterators
D
Iterator adapters don't exist
6

Question 6

What does the map() adapter do?

A
Transforms each element using a closure
B
Filters elements based on a condition
C
Collects elements into a collection
D
Finds the first matching element
7

Question 7

What does this map() example produce?

rust
let v = vec![1, 2, 3];
let doubled: Vec<i32> = v.iter().map(|x| x * 2).collect();
A
[2, 4, 6]
B
[1, 2, 3]
C
Compilation error
D
Runtime error
8

Question 8

What does the filter() adapter do?

A
Keeps only elements that match a predicate
B
Transforms elements
C
Sorts elements
D
Removes duplicates
9

Question 9

What does this filter() example produce?

rust
let v = vec![1, 2, 3, 4, 5];
let even: Vec<i32> = v.into_iter().filter(|x| x % 2 == 0).collect();
A
[2, 4]
B
[1, 3, 5]
C
[1, 2, 3, 4, 5]
D
[]
10

Question 10

What are iterator consumers?

A
Methods that consume the iterator and return a final value
B
Methods that create iterators
C
Methods that transform iterators
D
Iterator consumers don't exist
11

Question 11

What does the collect() consumer do?

A
Transforms an iterator into a collection
B
Sums all elements
C
Counts elements
D
Finds maximum value
12

Question 12

What does this collect() example do?

rust
let v = vec![1, 2, 3];
let doubled: Vec<i32> = v.iter().map(|&x| x * 2).collect();
A
Creates [2, 4, 6] by dereferencing and doubling
B
Compilation error due to wrong dereference
C
Creates [1, 2, 3]
D
Runtime error
13

Question 13

What does the sum() consumer do?

A
Adds all elements together
B
Multiplies all elements
C
Finds the average
D
Counts elements
14

Question 14

What does this sum() example produce?

rust
let v = vec![1, 2, 3, 4];
let total: i32 = v.iter().sum();
A
10
B
0
C
Compilation error
D
Runtime error
15

Question 15

What does the count() consumer do?

A
Returns the number of elements in the iterator
B
Returns the sum of elements
C
Returns the first element
D
Returns the last element
16

Question 16

What is lazy evaluation in iterators?

A
Operations are performed only when the result is needed
B
Operations are performed immediately
C
Operations are cached
D
Lazy evaluation doesn't apply to iterators
17

Question 17

What happens in this lazy evaluation example?

rust
let v = vec![1, 2, 3, 4, 5];
let result: Vec<i32> = v.iter()
    .map(|x| {
        println!("Mapping {}", x);
        x * 2
    })
    .filter(|x| {
        println!("Filtering {}", x);
        x > &4
    })
    .collect();
A
Prints occur only when collect() is called, showing lazy evaluation
B
Prints occur immediately for all elements
C
No prints occur
D
Compilation error
18

Question 18

How do you create a custom iterator?

A
Implement the Iterator trait with next() method
B
Use a for loop
C
Use collect()
D
Custom iterators are not possible
19

Question 19

What is required to implement a custom iterator?

A
Implement Iterator trait with Item associated type and next() method
B
Just implement next() method
C
Implement IntoIterator trait
D
No implementation needed
20

Question 20

What does this custom iterator example do?

rust
struct Counter {
    count: u32,
}

impl Iterator for Counter {
    type Item = u32;
    
    fn next(&mut self) -> Option<Self::Item> {
        self.count += 1;
        if self.count < 6 {
            Some(self.count)
        } else {
            None
        }
    }
}
A
Creates an iterator that yields 1, 2, 3, 4, 5
B
Creates an iterator that yields 0, 1, 2, 3, 4
C
Compilation error
D
Infinite iterator
21

Question 21

What is the IntoIterator trait?

A
Trait that defines how to convert a type into an iterator
B
Trait for creating iterators
C
Trait for consuming iterators
D
IntoIterator doesn't exist
22

Question 22

What is the difference between Iterator and IntoIterator?

A
IntoIterator converts types to iterators, Iterator defines iteration behavior
B
They are the same trait
C
Iterator converts types, IntoIterator defines behavior
D
Neither trait exists
23

Question 23

What does the enumerate() adapter do?

A
Adds an index to each element, returning (index, element) pairs
B
Numbers elements starting from 1
C
Counts total elements
D
Enumerate doesn't exist
24

Question 24

What does this enumerate() example produce?

rust
let v = vec!['a', 'b', 'c'];
let enumerated: Vec<(usize, char)> = v.iter().enumerate().collect();
A
[(0, 'a'), (1, 'b'), (2, 'c')]
B
[(1, 'a'), (2, 'b'), (3, 'c')]
C
['a', 'b', 'c']
D
Compilation error
25

Question 25

What does the zip() adapter do?

A
Combines two iterators into pairs
B
Compresses elements
C
Skips elements
D
Zip doesn't exist
26

Question 26

What does this zip() example produce?

rust
let a = vec![1, 2, 3];
let b = vec!['a', 'b'];
let zipped: Vec<(i32, char)> = a.iter().zip(b.iter()).collect();
A
[(1, 'a'), (2, 'b')]
B
[(1, 'a'), (2, 'b'), (3, ?)]
C
Compilation error
D
[(1, 'a'), (2, 'b'), (3, 'c')]
27

Question 27

What does the take() adapter do?

A
Takes the first n elements from an iterator
B
Takes all elements
C
Takes the last n elements
D
Take doesn't exist
28

Question 28

What does the skip() adapter do?

A
Skips the first n elements and yields the rest
B
Skips all elements
C
Skips every nth element
D
Skip doesn't exist
29

Question 29

What does the chain() adapter do?

A
Combines two iterators into one continuous iterator
B
Chains function calls
C
Creates a linked list
D
Chain doesn't exist
30

Question 30

What does this chain() example produce?

rust
let a = vec![1, 2];
let b = vec![3, 4];
let chained: Vec<i32> = a.iter().chain(b.iter()).collect();
A
[1, 2, 3, 4]
B
[3, 4, 1, 2]
C
[[1, 2], [3, 4]]
D
Compilation error
31

Question 31

What does the fold() consumer do?

A
Accumulates values using a closure that takes accumulator and element
B
Folds elements into a single value
C
Creates multiple values
D
Fold doesn't exist
32

Question 32

What does this fold() example produce?

rust
let v = vec![1, 2, 3, 4];
let sum = v.iter().fold(0, |acc, x| acc + x);
A
10
B
0
C
24
D
Compilation error
33

Question 33

What does the any() consumer do?

A
Returns true if any element matches the predicate
B
Returns true if all elements match
C
Returns the first matching element
D
Any doesn't exist
34

Question 34

What does the all() consumer do?

A
Returns true if all elements match the predicate
B
Returns true if any element matches
C
Returns all matching elements
D
All doesn't exist
35

Question 35

What does the find() consumer do?

A
Returns the first element that matches the predicate
B
Returns all matching elements
C
Returns the index of matching element
D
Find doesn't exist
36

Question 36

What is iterator performance like compared to loops?

A
Iterators can be as fast as loops due to optimization
B
Iterators are always slower than loops
C
Loops are always slower than iterators
D
Performance depends on usage
37

Question 37

What is the advantage of lazy evaluation?

A
Avoids unnecessary computation and enables short-circuiting
B
Makes code run faster
C
Uses less memory
D
Lazy evaluation has no advantages
38

Question 38

How do you implement IntoIterator for a custom type?

A
Implement IntoIterator trait with into_iter() method
B
Implement Iterator trait
C
Use a macro
D
IntoIterator cannot be implemented
39

Question 39

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

A
iter() borrows immutably, iter_mut() borrows mutably
B
iter() takes ownership, iter_mut() borrows
C
They are identical
D
iter_mut() doesn't exist
40

Question 40

In a data processing pipeline where you need to filter a large dataset, transform each element, and collect only the first 10 results, which iterator approach would be most efficient and why?

A
Use filter().map().take(10).collect() for lazy evaluation and early termination
B
Collect first, then filter/map/take to process all data unnecessarily
C
Use loops with break for manual control
D
Process all data and slice the result

QUIZZES IN Rust