Rust Variables and Mutability Quiz

Rust
0 Passed
0% acceptance

40 comprehensive questions on Rust's variable system, covering let bindings, mut keyword, shadowing, type inference, and constants — with 12 code examples demonstrating practical variable usage patterns.

40 Questions
~80 minutes
1

Question 1

In Rust, what is the default mutability of variables declared with let?

A
Mutable
B
Immutable
C
Depends on the type
D
Depends on the context
2

Question 2

What happens when you try to reassign a value to an immutable variable?

A
The value is updated successfully
B
Compilation error: cannot assign twice to immutable variable
C
Runtime error
D
The variable becomes mutable
3

Question 3

In a scenario where you're building a game loop that needs to update player positions frequently, how should you declare the position variables?

A
let mut x = 0; let mut y = 0;
B
let x = 0; let y = 0;
C
const x: i32 = 0; const y: i32 = 0;
D
Use immutable variables and recreate them each frame
4

Question 4

What is variable shadowing in Rust?

A
Declaring a new variable with the same name as an existing one, hiding the previous variable
B
Making a variable invisible to other parts of the code
C
Changing a variable's type at runtime
D
Moving a variable to a different scope
5

Question 5

When would you typically use variable shadowing?

A
When transforming a value through multiple steps
B
When you want to delete a variable
C
When making variables global
D
When optimizing performance
6

Question 6

What will this Rust code output?

rust
fn main() {
    let x = 5;
    let x = x + 1;
    let x = x * 2;
    println!("The value of x is: {}", x);
}
A
The value of x is: 5
B
The value of x is: 6
C
The value of x is: 12
D
Compilation error
7

Question 7

How does Rust's type inference work?

A
The compiler automatically determines types based on usage and initial values
B
You must always specify types explicitly
C
Types are inferred only for constants
D
Type inference requires special syntax
8

Question 8

In a scenario where you're parsing user input that could be either an integer or a string, how should you handle the variable types?

A
Use shadowing to transform the parsed value
B
Use mutable variables that change type
C
Use constants for parsed values
D
This requires separate variables
9

Question 9

What is the difference between variables and constants in Rust?

A
Constants are always immutable and their values must be known at compile time
B
Constants can be mutable
C
Variables must be known at compile time
D
There is no difference
10

Question 10

What is the correct syntax for declaring a constant in Rust?

A
const MAX_POINTS: u32 = 100_000;
B
const MAX_POINTS = 100_000;
C
let const MAX_POINTS = 100_000;
D
constant MAX_POINTS: u32 = 100_000;
11

Question 11

In a scenario where you need a value that never changes during program execution but is computed from other constants, what should you use?

A
const with a constant expression
B
let variable
C
static variable
D
Runtime computation
12

Question 12

What happens when you try to use a variable before it's declared?

A
The code compiles and runs
B
Compilation error: cannot find value in this scope
C
Runtime error
D
The variable gets a default value
13

Question 13

How do you make a variable mutable in Rust?

A
Use the mut keyword: let mut variable_name
B
Use the var keyword
C
Variables are mutable by default
D
Use the mutable keyword
14

Question 14

In a scenario where you're implementing a counter that gets incremented in a loop, which declaration is most appropriate?

A
let mut counter = 0;
B
let counter = 0;
C
const counter: i32 = 0;
D
Use shadowing for each increment
15

Question 15

What will this code do?

rust
fn main() {
    let mut x = 5;
    x = 10;
    println!("x is {}", x);
}
A
Print 'x is 10'
B
Print 'x is 5'
C
Compilation error
D
Runtime error
16

Question 16

When should you avoid using mutable variables?

A
When the value never changes after initialization
B
When performance is critical
C
When working with constants
D
Mutable variables should always be used
17

Question 17

What is the scope of a variable declared with let inside a function?

A
From the point of declaration to the end of the function
B
Global scope
C
Only within the current block
D
Until the next let declaration
18

Question 18

In a scenario where you have a configuration value that should be accessible throughout your program but never change, what should you use?

A
const declaration at module level
B
let mut variable
C
Global mutable variable
D
Function parameter
19

Question 19

What happens when you shadow a mutable variable with an immutable one?

A
The new variable is immutable, but you can still mutate the original if accessible
B
The original variable becomes immutable
C
Compilation error
D
Both variables become mutable
20

Question 20

How does Rust determine the type of a variable when you don't specify it explicitly?

A
From the initial value and how it's used in the code
B
From the variable name
C
From the function it's declared in
D
Types must always be explicit
21

Question 21

In a scenario where you're processing a series of transformations on user input, which approach is most idiomatic in Rust?

A
Use shadowing to transform the value through multiple steps
B
Use a mutable variable and modify it in place
C
Create separate variables for each step
D
Use constants for each transformation
22

Question 22

What will this code output?

rust
fn main() {
    let x = 5;
    {
        let x = 10;
        println!("inner x: {}", x);
    }
    println!("outer x: {}", x);
}
A
inner x: 10, outer x: 10
B
inner x: 10, outer x: 5
C
Compilation error
D
inner x: 5, outer x: 10
23

Question 23

When should you explicitly specify types instead of relying on inference?

A
When the inferred type might not be what you expect, or for public APIs
B
Always, for clarity
C
Never, inference is always better
D
Only for constants
24

Question 24

In a scenario where you're implementing a mathematical constant like PI, which declaration is most appropriate?

A
const PI: f64 = 3.141592653589793;
B
let PI = 3.141592653589793;
C
let mut PI = 3.141592653589793;
D
Use a function that returns PI
25

Question 25

What is the difference between let and const declarations?

A
let creates runtime variables, const creates compile-time constants
B
const can be mutable, let cannot
C
let requires explicit types, const does not
D
There is no functional difference
26

Question 26

In a scenario where you need to track the number of iterations in a loop for debugging purposes, how should you declare the counter?

A
let mut iteration_count = 0;
B
let iteration_count = 0;
C
const ITERATION_COUNT: usize = 0;
D
Use shadowing for each iteration
27

Question 27

What happens when you try to modify a constant?

A
The constant is updated
B
Compilation error: cannot assign to immutable constant
C
Runtime error
D
The constant becomes a variable
28

Question 28

How do you declare a constant that represents the maximum number of connections in a server?

A
const MAX_CONNECTIONS: usize = 1000;
B
let MAX_CONNECTIONS = 1000;
C
let mut MAX_CONNECTIONS = 1000;
D
const MAX_CONNECTIONS = 1000;
29

Question 29

In a scenario where you're implementing a caching system where the cache size should be configurable at compile time, what should you use?

A
const with a reasonable default value
B
let mut variable
C
Runtime configuration file
D
Environment variables only
30

Question 30

What will this code do?

rust
fn main() {
    let mut message = String::from("hello");
    message.push_str(", world!");
    println!("{}", message);
}
A
Print 'hello, world!'
B
Compilation error
C
Print 'hello'
D
Runtime error
31

Question 31

When should you use shadowing instead of mutating a variable?

A
When transforming a value through different types or representations
B
When you want to avoid allocation
C
When performance is critical
D
Shadowing should never be used
32

Question 32

In a scenario where you have a user-provided string that you need to validate and then convert to uppercase, which approach is most Rust-idiomatic?

A
Use shadowing: let input = input.trim().to_uppercase();
B
Use mutable variable and modify in place
C
Create separate variables for each step
D
Use constants for the transformations
33

Question 33

What is the benefit of Rust's immutable-by-default approach?

A
Prevents accidental mutations and makes code easier to reason about
B
Makes code run faster
C
Reduces memory usage
D
Simplifies syntax
34

Question 34

How do you declare a variable with an explicit type annotation?

A
let variable_name: Type = value;
B
let variable_name = value: Type;
C
Type variable_name = value;
D
variable_name: Type = value;
35

Question 35

In a scenario where you're implementing a function that should accept both mutable and immutable references, how should you design the parameters?

A
Use immutable references (&T) and let callers decide mutability
B
Use mutable references (&mut T) for flexibility
C
Use owned values (T)
D
Use constants
36

Question 36

What will this code output?

rust
fn main() {
    let number = 42;
    let number = number.to_string();
    let number = number + " is the answer";
    println!("{}", number);
}
A
42
B
42 is the answer
C
Compilation error
D
42 is the answer42
37

Question 37

When should you use constants instead of literals scattered throughout your code?

A
When the same value is used in multiple places and might need to change
B
When the value is used only once
C
When performance is critical
D
Constants should never be used
38

Question 38

In a scenario where you're implementing a timeout value that should be the same across multiple functions, how should you declare it?

A
const TIMEOUT_SECONDS: u64 = 30;
B
let timeout_seconds = 30;
C
let mut timeout_seconds = 30;
D
Use a global variable
39

Question 39

What is the key difference between variable shadowing and mutable variables?

A
Shadowing creates new variables, mutability allows changing existing variables
B
Shadowing is faster than mutability
C
Mutable variables can shadow, but shadowed variables cannot be mutable
D
There is no difference
40

Question 40

In a scenario where you're implementing a state machine where each state transformation creates a new state, which approach is most appropriate?

A
Use shadowing to transform the state variable through each transition
B
Use a mutable variable and modify it in place
C
Use constants for each state
D
Create separate variables for each state

QUIZZES IN Rust