programming
beginner
10 sample questions
Rust MCQ Practice Test
A systems programming language focused on safety, speed, and concurrency.
Q1. Which Rust keyword is used to define a mutable reference to a value?
-
A. let
-
B. const
-
C. mut ✓
-
D. fn
Explanation: In Rust, the 'mut' keyword is used to define a mutable reference to a value, allowing it to be modified.
Q2. In Rust, what is the purpose of the `std::mem::size_of` function?
-
A. Returns the size of the type in bytes as a `u64` value.
-
B. Returns the alignment of the type in bytes as a `usize` value.
-
C. Returns the size of the type in bytes as a `usize` value. ✓
-
D. Returns the offset of the type in bytes as a `isize` value.
Explanation: The `std::mem::size_of` function returns the size of a type in bytes as a `usize` value. This can be used to determine the size of a type at compile-time.
Q3. In Rust, what is the purpose of the `std::mem::drop` function?
-
A. It is used to manually drop a value to free its resources ✓
-
B. It is used to clone a value
-
C. It is used to move a value into a new scope
-
D. It is used to increase the reference count of a value
Explanation: The `std::mem::drop` function is used to manually drop a value, which can be useful in certain situations where the value needs to be dropped immediately, such as when working with values that have a large memory footprint or when dealing with values that are not properly cleaned up by the Rust compiler.
Q4. What is the purpose of the 'match' keyword in Rust?
-
A. It is used for pattern matching in if statements
-
B. It is used for type checking and casting
-
C. It is used to handle different cases in a function ✓
-
D. It is used to create a new scope
Explanation: The 'match' keyword in Rust is used to handle different cases in a function, similar to switch statements in other languages.
Q5. What is the purpose of the `std::sync::mpsc` module in Rust?
-
A. It provides a way to create threads that can communicate with each other.
-
B. It offers a thread-safe way to manage shared resources.
-
C. It allows for concurrent access to data through a message passing interface. ✓
-
D. It provides a way to synchronize access to shared data between threads.
Explanation: The `std::sync::mpsc` module in Rust provides a thread-safe way for threads to communicate with each other, allowing for concurrent access to data through a message passing interface. This is achieved through the use of channels, which enable threads to send and receive messages in a safe and efficient manner.
Q6. What is the purpose of the `std::mem::drop` function in Rust?
-
A. It is used to manually drop a value and reclaim its memory. ✓
-
B. It is used to manually clone a value.
-
C. It is used to manually move a value into a new location.
-
D. It is used to manually take ownership of a value.
Explanation: The `std::mem::drop` function is used to manually drop a value and reclaim its memory, which can be useful in certain situations such as when working with smart pointers or when trying to optimize memory usage.
Q7. In Rust, what does calling std::mem::drop(x) do?
-
A. Moves x out of scope early and runs its Drop destructor ✓
-
B. Leaks memory permanently
-
C. Freezes the program
-
D. Converts x into a raw pointer
Explanation: drop(x) gives up ownership of x immediately so its destructor runs — used to release resources such as locks early.
Q8. What is the purpose of the `\s` pattern in Rust regular expressions?
-
A. A pattern that matches any character
-
B. A pattern that matches any whitespace character ✓
-
C. A pattern that matches the start of a string
-
D. A pattern that matches the end of a string
Explanation: In Rust regular expressions, the `\s` pattern is used to match any whitespace character, including spaces, tabs, and newlines.
Q9. In Rust, what is the purpose of the 'async' keyword when used with a function?
-
A. It indicates that the function will consume the caller's stack.
-
B. It allows the function to return a value that can be awaited by another task. ✓
-
C. It is used to specify the type of the function's return value.
-
D. It enables the function to be executed in parallel with other tasks.
Explanation: The 'async' keyword in Rust is used to mark a function as asynchronous, allowing it to return a value that can be awaited by another task. This enables the function to yield control back to the caller, allowing other tasks to run in the meantime.
Q10. What is the purpose of the \`match\` statement in Rust?
-
A. It is used to define a struct
-
B. It is used to define a function
-
C. It is used to handle different cases of a value ✓
-
D. It is used to define a trait
Explanation: The \`match\` statement in Rust is used to handle different cases of a value, such as different variants of an enum or different values of a primitive type. It allows you to specify a value to match against and a set of possible values, and execute different code blocks based on the match.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free