MCQs on Variables and Data Types | Rust

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

  1. In Rust, which of the following is the default behavior for variables?
    a) Mutable
    b) Immutable
    c) Constant
    d) Dynamically typed
  2. To make a variable mutable in Rust, which keyword is used?
    a) let
    b) mutable
    c) mut
    d) var
  3. 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
  4. 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
  5. 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

  1. 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";
  2. What is the default integer type in Rust?
    a) i32
    b) i64
    c) u32
    d) usize
  3. 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
  4. What data type would you use in Rust to represent a boolean value?
    a) bool
    b) boolean
    c) int
    d) flag
  5. 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

  1. Which keyword is used to declare a constant in Rust?
    a) const
    b) let
    c) static
    d) var
  2. Constants in Rust must be:
    a) Mutable
    b) Typed explicitly
    c) Non-static
    d) Dynamically allocated
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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;
  3. What type is inferred for the following Rust declaration: let x = 3.14;?
    a) f64
    b) f32
    c) i32
    d) float
  4. 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
  5. 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

  1. Which of the following is NOT a valid data type in Rust?
    a) char
    b) int
    c) f64
    d) bool
  2. The size of an i64 variable in Rust is:
    a) 8 bytes
    b) 4 bytes
    c) 16 bytes
    d) 2 bytes
  3. Which data type in Rust is used to represent unsigned integers?
    a) u32
    b) i32
    c) f32
    d) char
  4. In Rust, you cannot assign a value to a constant that is:
    a) Mutable
    b) A reference
    c) A string
    d) A boolean
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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)

QnoAnswer
1b) Immutable
2c) mut
3d) The compiler gives an error
4a) By using the mut keyword in the declaration
5c) Variables are immutable by default unless declared mutable
6c) let x: i32 = 10;
7a) i32
8d) Both a and b
9a) bool
10a) let c = 'a';
11a) const
12b) Typed explicitly
13d) Both b and c
14c) Constants cannot be changed once set, while variables can
15c) Anywhere in the program
16a) Rust infers the type automatically
17b) let x: i32 = 10;
18a) f64
19c) Rust allows type inference but still requires types in certain contexts
20d) Both a and b
21b) int
22a) 8 bytes
23a) u32
24a) Mutable
25c) By using a borrow checker and ownership system
26b) A type for indexing into arrays
27c) To avoid type inference errors
28a) A 32-bit floating-point number
29a) It marks a variable as mutable
30a) To allow Rust to automatically detect the correct data type for a variable

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top