Rust Quiz - MCQ Questions and Answers

Welcome to our Rust Quiz! This quiz has 30 multiple-choice questions to check your understanding of Rust, a programming language known for being safe and fast. The questions cover everything from basic stuff like how to write and organize Rust code to more complex features like how Rust handles memory safety without needing a garbage collector. Whether you're new to Rust or already know some of it, this quiz is a great way to see how much you know and learn more. Ready to get started? Let’s test your Rust knowledge!

1. What is the primary purpose of the cargo tool in Rust?

a) To compile programs
b) To manage project dependencies
c) To format code
d) To create new user accounts

2. Which keyword is used to define a variable that cannot be modified?

a) mut
b) let
c) const
d) var

3. What symbol is used to denote ownership transfer in Rust?

a) &
b) ->
c) *
d) =

4. How do you define a function in Rust?

a) func
b) def
c) function
d) fn

5. What is the output type of the println! macro when it is used correctly?

a) int
b) bool
c) ()
d) string

6. Which type of loop will continue until a specific condition becomes false?

a) loop
b) for
c) while
d) repeat

7. How can you access the third element of a vector v?

a) v(2)
b) v[2]
c) v.get(2)
d) Both b and c are correct

8. What does the match statement do?

a) Executes code based on the value of a variable
b) Iterates over each item in a collection
c) Checks the type of a variable
d) None of the above

9. Which keyword is used to bring a module into scope?

a) import
b) require
c) use
d) include

10. What does the ? operator do in Rust?

a) Error checking
b) Multiplication
c) Unwrapping a value or propagating an error
d) Optional type marking

11. What is the term for variables stored on the heap?

a) Static
b) Dynamic
c) Block
d) Inline

12. How do you create a new vector in Rust?

a) Vec::new()
b) Vector::new()
c) vec![]
d) Both a and c are correct

13. Which trait must be in scope to use the ? operator for error handling?

a) Debug
b) Display
c) Try
d) Error

14. What does the ownership model in Rust help prevent?

a) Slow performance
b) Syntax errors
c) Data races
d) Overflows

15. What is the default return type of a function in Rust that does not explicitly return a value?

a) int
b) bool
c) ()
d) None

16. What does the following code snippet return?

let x = 5;
let y = {
    let x = 3;
    x + 1
a) 3
b) 4
c) 5
d) None of the above

17. Analyze the purpose of the unwrap() function in this code:

let result = Some(10).unwrap();
a) Causes the program to panic if None
b) Multiplies the inner value by 2
c) Converts Some to None
d) None of the above

18. What will be the output of this Rust code?

fn main() {
    let s1 = String::from("Hello, ");
    let s2 = String::from("world!");
    let s3 = s1 + &s2;
    println!("{}", s3);
a) Hello, world!
b) Hello,
c) world!
d) Compilation error

19. What does the following code demonstrate?

fn change(val: &mut i32) {
    *val += 1;
fn main() {
    let mut num = 0;
    change(&mut num);
a) Function overloading
b) Mutable borrowing
c) Immutable borrowing
d) Ownership transfer

20. Identify the error in this Rust code:

fn main() {
    let nums = vec![1, 2, 3];
    let first = &nums[0];
    println!("{}", first);
a) Syntax error
b) Mutable and immutable borrow conflict
c) Out of bounds access
d) No error

21. What is the purpose of match in this snippet?

let option = Some(5);
match option {
    Some(x) => println!("Value: {}", x),
    None => (),
a) Prints a value if not None
b) Checks for syntax errors
c) Loops through option
d) None of the above

22. What is the outcome of the following loop?

let mut counter = 0;
while counter < 3 {
    println!("counter = {}", counter);
    counter += 1;
a) Prints "counter = 0" three times
b) Prints "counter = 0", "counter = 1", and "counter = 2"
c) Results in an infinite loop
d) No output

23. How is error handling implemented in this function?

a) Using panics
b) Through Result type
c) By callbacks
d) None of the above

24. What feature does this Rust code demonstrate?

struct Point {
    x: i32,
    y: i32,
fn main() {
    let pt = Point { x: 1, y: 2 };
    println!("{:?}", pt);
a) Error propagation
b) Struct definition and Debug trait
c) Function overloading
d) Generic programming

25. What kind of memory safety issue is prevented by the following code?

let mut data = vec![1, 2, 3];
let element = &data[0];
println!("First element: {}", element);
a) Use after free
b) Data race
c) Buffer overflow
d) None, the code will cause a compile-time error

26. What does &str represent in Rust?

let greeting: &str = "Hello, world!";
a) A string literal stored in the stack
b) A mutable string reference
c) A heap-allocated string
d) An immutable string slice

27. How does Rust handle memory management?

let x = Box::new(5);
a) Automatic garbage collection
b) Manual memory management with pointers
c) Ownership system with automatic memory deallocation
d) Reference counting

28. What is the result of the following code?

fn main() {
    let x = 5;
    let y = &x;
    let z = &y;
    println!("z points to {}", **z);
a) 5
b) Compilation error
c) 0
d) None of the above

29. Which trait is required to use the ? operator for error handling in custom types?

struct MyError;
impl std::error::Error for MyError {}
a) Debug
b) Display
c) Try
d) Error

30. Analyze the role of mut in this code snippet:

let mut number = 3;
number = 4;
a) It makes number a constant
b) It allows number to be changed
c) It makes number an immutable reference
d) It indicates that number is a type of integer