Rust Control Flow Quiz

Rust
0 Passed
0% acceptance

45 comprehensive questions on Rust's control flow constructs, covering if expressions, loops, and control flow keywords — with 15 code examples demonstrating practical control flow usage.

45 Questions
~90 minutes
1

Question 1

What is the primary difference between if statements in other languages and if expressions in Rust?

A
if expressions in Rust return values and can be used in assignments
B
if expressions in Rust don't require parentheses
C
if expressions in Rust are faster
D
if expressions in Rust don't need braces
2

Question 2

What happens if you use an if expression in an assignment without an else branch?

A
Compilation error: if expressions must have else when used as values
B
The expression returns () (unit type)
C
The expression returns the condition value
D
Runtime error
3

Question 3

How do you write a multi-condition if-else if chain in Rust?

A
if condition1 { ... } else if condition2 { ... } else { ... }
B
if condition1 && condition2 { ... } else { ... }
C
switch condition1 { case condition2: ... }
D
if condition1 { ... } and if condition2 { ... }
4

Question 4

What will this if expression evaluate to?

rust
fn main() {
    let number = if true { 5 } else { 6 };
    println!("The number is: {}", number);
}
A
The number is: 5
B
The number is: 6
C
Compilation error
D
Runtime error
5

Question 5

What is if let syntax used for?

A
Matching patterns while checking a condition
B
Creating new variables
C
Looping with conditions
D
Type conversion
6

Question 6

What will this code output?

rust
fn main() {
    let some_option = Some(5);
    if let Some(value) = some_option {
        println!("Got value: {}", value);
    } else {
        println!("No value");
    }
}
A
Got value: 5
B
No value
C
Compilation error
D
Runtime error
7

Question 7

What is the basic syntax of a loop expression?

A
loop { ... }
B
for i in 0..10 { ... }
C
while condition { ... }
D
repeat 10 { ... }
8

Question 8

How do you exit a loop prematurely?

A
Using the break keyword
B
Using the exit keyword
C
Using the return keyword
D
Loops cannot be exited prematurely
9

Question 9

What does the continue keyword do in a loop?

A
Skips the rest of the current iteration and starts the next one
B
Exits the loop entirely
C
Restarts the loop from the beginning
D
Pauses the loop execution
10

Question 10

Can loop expressions return values?

A
Yes, by using break with a value: break value;
B
No, loops don't return values
C
Only infinite loops can return values
D
Only with the return keyword
11

Question 11

What will this loop code output?

rust
fn main() {
    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 3 {
            break counter * 2;
        }
    };
    println!("Result: {}", result);
}
A
Result: 6
B
Result: 3
C
Compilation error
D
Infinite loop
12

Question 12

What is a while loop used for?

A
Looping while a condition remains true
B
Looping a fixed number of times
C
Looping infinitely
D
Looping over collections
13

Question 13

What will this while loop do?

rust
fn main() {
    let mut number = 3;
    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }
    println!("LIFTOFF!!!");
}
A
Print 3!, 2!, 1!, LIFTOFF!!!
B
Print 3!, 2!, 1!, 0!, LIFTOFF!!!
C
Infinite loop
D
Compilation error
14

Question 14

What is while let syntax used for?

A
Looping while a pattern matches
B
Creating variables in loops
C
Type conversion in loops
D
Infinite looping
15

Question 15

What is the syntax for a for loop in Rust?

A
for element in collection { ... }
B
for (int i = 0; i < 10; i++) { ... }
C
for i in 0..10 { ... }
D
All of the above
16

Question 16

What does 0..10 represent in a for loop?

A
A range from 0 (inclusive) to 10 (exclusive)
B
A range from 0 to 10 inclusive
C
An array of numbers
D
A floating-point range
17

Question 17

What will this for loop output?

rust
fn main() {
    for number in (1..4).rev() {
        println!("{}!", number);
    }
    println!("LIFTOFF!!!");
}
A
3!, 2!, 1!, LIFTOFF!!!
B
1!, 2!, 3!, LIFTOFF!!!
C
4!, 3!, 2!, LIFTOFF!!!
D
Compilation error
18

Question 18

When processing a list of items to find a specific value, which loop construct is most appropriate?

A
for element in &array { ... }
B
while let Some(element) = array.next() { ... }
C
loop with manual indexing
D
for i in 0..array.len() { ... }
19

Question 19

What happens when you use break in a nested loop?

A
It breaks out of the innermost loop only
B
It breaks out of all loops
C
Compilation error
D
It breaks out of the outermost loop
20

Question 20

How can you break out of an outer loop from within a nested inner loop?

A
Use labeled breaks: break 'outer_loop;
B
Use return to exit the function
C
Use continue with a label
D
This is not possible
21

Question 21

What will this labeled break code do?

rust
fn main() {
    'outer: loop {
        println!("Entered outer loop");
        'inner: loop {
            println!("Entered inner loop");
            break 'outer;
        }
        println!("This will not print");
    }
    println!("Exited both loops");
}
A
Print 'Entered outer loop', 'Entered inner loop', 'Exited both loops'
B
Print 'Entered outer loop', 'Entered inner loop', 'This will not print', 'Exited both loops'
C
Compilation error
D
Infinite loop
22

Question 22

When should you use continue in a loop?

A
When you want to skip the current iteration and move to the next one
B
When you want to exit the loop
C
When you want to restart the loop
D
When you want to pause execution
23

Question 23

Can continue be used with labels?

A
Yes, to continue an outer loop from an inner loop
B
No, continue only works on the current loop
C
Only in while loops
D
Only in for loops
24

Question 24

When processing a list of items but wanting to skip invalid ones, which approach is most appropriate?

A
Use continue to skip invalid items
B
Use break to exit on invalid items
C
Use return to skip items
D
Remove invalid items from the list first
25

Question 25

What is the difference between break and return in a loop?

A
break exits the loop, return exits the function
B
return exits the loop, break exits the function
C
They are the same
D
break only works in loops, return only in functions
26

Question 26

What will this code output?

rust
fn main() {
    let mut i = 0;
    loop {
        i += 1;
        if i == 2 {
            continue;
        }
        if i == 4 {
            break;
        }
        println!("i = {}", i);
    }
}
A
i = 1, i = 3
B
i = 1, i = 2, i = 3
C
i = 1, i = 3, i = 4
D
Infinite loop
27

Question 27

How do you iterate over a vector in reverse order?

A
for element in vec.iter().rev() { ... }
B
for element in vec.rev() { ... }
C
for i in (0..vec.len()).rev() { ... }
D
All of the above
28

Question 28

What is the most common use case for loop expressions?

A
When you need an infinite loop that breaks based on internal conditions
B
When iterating over collections
C
When you know the number of iterations
D
When looping while a condition is true
29

Question 29

When implementing a game loop that should run until the player quits, which loop construct is most appropriate?

A
loop { ... }
B
while running { ... }
C
for _ in 0.. { ... }
D
Any of the above could work
30

Question 30

When working with Options, if let is useful for conditionally executing code when an Option is Some(value). What does this demonstrate about Rust's control flow?

A
Integration of pattern matching with conditional execution
B
Type conversion capabilities
C
Memory management features
D
Performance optimizations
31

Question 31

How does Rust prevent infinite loops in while loops?

A
It doesn't - you must ensure the condition eventually becomes false
B
It automatically adds break conditions
C
It requires loop bounds
D
It uses timeouts
32

Question 32

What will this code do?

rust
fn main() {
    let numbers = vec![1, 2, 3, 4, 5];
    for num in &numbers {
        if *num == 3 {
            break;
        }
        println!("num = {}", num);
    }
}
A
Print num = 1, num = 2
B
Print num = 1, num = 2, num = 3
C
Compilation error
D
Print all numbers
33

Question 33

When should you prefer for loops over while loops?

A
When iterating over collections or ranges
B
When you need manual index control
C
When the number of iterations is unknown
D
When you need infinite loops
34

Question 34

What is the key difference between if and while let?

A
if let executes once, while let executes repeatedly while the pattern matches
B
while let executes once, if let executes repeatedly
C
They are identical
D
if let is for loops, while let is for conditions
35

Question 35

What will this code output?

rust
fn main() {
    let condition = true;
    let number = if condition {
        let x = 5;
        x + 1
    } else {
        10
    };
    println!("Number: {}", number);
}
A
Number: 6
B
Number: 5
C
Number: 10
D
Compilation error
36

Question 36

How can you create a loop that runs exactly 10 times?

A
for _ in 0..10 { ... }
B
let mut i = 0; while i < 10 { i += 1; ... }
C
loop { static mut count = 0; count += 1; if count == 10 { break; } ... }
D
All of the above
37

Question 37

What happens when you try to return a value from a loop without break?

A
Compilation error
B
The loop returns () (unit)
C
Runtime panic
D
The loop never terminates
38

Question 38

In a text processing application where you need to skip whitespace characters while processing a string, which control flow construct would be most appropriate?

A
for c in text.chars() { if c.is_whitespace() { continue; } ... }
B
while let Some(c) = chars.next() { if !c.is_whitespace() { ... } }
C
loop { let c = get_next_char(); if c.is_whitespace() { break; } ... }
D
All could work depending on the context
39

Question 39

What will this nested loop code do?

rust
fn main() {
    for i in 1..3 {
        for j in 1..3 {
            if i == j {
                continue;
            }
            println!("i={}, j={}", i, j);
        }
    }
}
A
Print i=1, j=2 and i=2, j=1
B
Print all combinations except i=1,j=1 and i=2,j=2
C
Print i=1, j=1 and i=2, j=2
D
Compilation error
40

Question 40

When implementing a search algorithm that needs to examine elements until finding a match, which loop type is most efficient?

A
for loop with break
B
while loop with manual indexing
C
loop with iterator
D
All have similar performance
41

Question 41

What will this code output?

rust
fn main() {
    let mut sum = 0;
    for i in 1..=5 {
        if i % 2 == 0 {
            continue;
        }
        sum += i;
    }
    println!("Sum: {}", sum);
}
A
Sum: 9
B
Sum: 15
C
Sum: 25
D
Compilation error
42

Question 42

How do you create a loop that never terminates unless explicitly broken?

A
loop { ... }
B
while true { ... }
C
for _ in 0.. { ... }
D
All of the above
43

Question 43

In a web server application that needs to handle requests in a loop until shutdown, which loop construct would be most appropriate?

A
loop { if shutdown_signal { break; } handle_request(); }
B
while !shutdown { handle_request(); }
C
for request in request_stream { handle_request(request); }
D
Any could work depending on the shutdown mechanism
44

Question 44

What will this code output?

rust
fn main() {
    let result = 'outer: loop {
        for i in 1..4 {
            if i == 2 {
                break 'outer i * 10;
            }
        }
    };
    println!("Result: {}", result);
}
A
Result: 20
B
Result: 2
C
Compilation error
D
Infinite loop
45

Question 45

When should you use labeled loops instead of unlabeled ones?

A
When you have nested loops and need to break out of outer loops
B
When loops are more than 3 levels deep
C
When performance is critical
D
Always, for clarity

QUIZZES IN Rust