Rust is a systems programming language focused on speed, memory safety, and concurrency. Understanding variables, data types, constants, and type inference is crucial for writing efficient and safe Rust programs. This set of 30 multiple-choice questions (MCQs) will help you explore fundamental Rust concepts like immutability, data types, constants, and type inference.
1. Variables and Immutability
In Rust, which of the following is the default behavior for variables? a) Mutable b) Immutable c) Constant d) Dynamically typed
To make a variable mutable in Rust, which keyword is used? a) let b) mutable c) mut d) var
What happens if you try to change an immutable variable in Rust? a) The program compiles successfully b) The compiler gives a warning c) The program causes a runtime error d) The compiler gives an error
How can you make a variable mutable after declaring it in Rust? a) By using the mut keyword in the declaration b) By assigning a new value to it c) By using the var keyword d) Rust does not allow variables to be mutable after declaration
Which of the following is true about variables in Rust? a) All variables are mutable by default b) Variables must be explicitly typed c) Variables are immutable by default unless declared mutable d) Rust has no concept of mutable variables
2. Basic Data Types: Integers, Floats, Booleans, and Characters
Which of the following is the correct way to declare a variable as an integer in Rust? a) let x = 10; b) let x: int = 10; c) let x: i32 = 10; d) let x = "10";
What is the default integer type in Rust? a) i32 b) i64 c) u32 d) usize
Which of the following is the correct representation of a floating-point number in Rust? a) let x = 3.14; b) let x: f64 = 3.14; c) let x = 3.14f32; d) Both a and b
What data type would you use in Rust to represent a boolean value? a) bool b) boolean c) int d) flag
How would you declare a character variable in Rust? a) let c = 'a'; b) let c = "a"; c) let c = 'char'; d) let c = 'A';
3. Constants
Which keyword is used to declare a constant in Rust? a) const b) let c) static d) var
Constants in Rust must be: a) Mutable b) Typed explicitly c) Non-static d) Dynamically allocated
Which of the following is the correct way to declare a constant in Rust? a) let PI = 3.14; b) const PI = 3.14; c) const PI: f64 = 3.14; d) Both b and c
What is the main difference between a constant and a variable in Rust? a) Constants can be changed at runtime b) Constants are mutable, but variables are not c) Constants cannot be changed once set, while variables can d) There is no difference
In Rust, where can constants be declared? a) Only inside functions b) Only at the top of the file c) Anywhere in the program d) Only inside loops
4. Type Inference and Explicit Typing
In Rust, what happens when a variable is declared without an explicit type? a) Rust infers the type automatically b) The program will fail to compile c) The variable will be treated as a dynamic type d) Rust assigns it a default i32 type
Which of the following is the correct way to explicitly type a variable in Rust? a) let x = 10i32; b) let x: i32 = 10; c) let x = 10: i32; d) let x: int = 10;
What type is inferred for the following Rust declaration: let x = 3.14;? a) f64 b) f32 c) i32 d) float
Which of the following statements is true about type inference in Rust? a) Rust never infers types and requires all types to be explicitly declared b) Rust always infers types as i32 c) Rust allows type inference but still requires types in certain contexts d) Type inference only works for basic data types
How would you declare a floating-point variable with explicit typing in Rust? a) let x = 3.14f32; b) let x: f32 = 3.14; c) let x = 3.14; d) Both a and b
5. Miscellaneous
Which of the following is NOT a valid data type in Rust? a) char b) int c) f64 d) bool
The size of an i64 variable in Rust is: a) 8 bytes b) 4 bytes c) 16 bytes d) 2 bytes
Which data type in Rust is used to represent unsigned integers? a) u32 b) i32 c) f32 d) char
In Rust, you cannot assign a value to a constant that is: a) Mutable b) A reference c) A string d) A boolean
How does Rust handle memory safety with regard to data types? a) By automatically managing memory allocation and deallocation b) By using garbage collection c) By using a borrow checker and ownership system d) By requiring all variables to be static
What does the usize type represent in Rust? a) A signed integer b) A type for indexing into arrays c) A 32-bit unsigned integer d) A fixed-size floating-point number
What is the primary reason to use explicit typing in Rust? a) To improve performance b) To make the code easier to read c) To avoid type inference errors d) To define custom types
The f32 type in Rust is: a) A 32-bit floating-point number b) A 32-bit integer c) A fixed-point number d) A character
What does the mut keyword do in Rust? a) It marks a variable as mutable b) It marks a constant as mutable c) It prevents a variable from being changed d) It automatically infers the type of a variable
In Rust, what is the purpose of type inference? a) To allow Rust to automatically detect the correct data type for a variable b) To speed up the compilation process c) To explicitly specify data types d) To allow for dynamic typing
Answer Key (Tabular Form)
Qno
Answer
1
b) Immutable
2
c) mut
3
d) The compiler gives an error
4
a) By using the mut keyword in the declaration
5
c) Variables are immutable by default unless declared mutable
6
c) let x: i32 = 10;
7
a) i32
8
d) Both a and b
9
a) bool
10
a) let c = 'a';
11
a) const
12
b) Typed explicitly
13
d) Both b and c
14
c) Constants cannot be changed once set, while variables can
15
c) Anywhere in the program
16
a) Rust infers the type automatically
17
b) let x: i32 = 10;
18
a) f64
19
c) Rust allows type inference but still requires types in certain contexts
20
d) Both a and b
21
b) int
22
a) 8 bytes
23
a) u32
24
a) Mutable
25
c) By using a borrow checker and ownership system
26
b) A type for indexing into arrays
27
c) To avoid type inference errors
28
a) A 32-bit floating-point number
29
a) It marks a variable as mutable
30
a) To allow Rust to automatically detect the correct data type for a variable