Rust Online Quiz Test

Welcome to our Rust Online Quiz Test! This quiz includes 25 multiple-choice questions that will test your knowledge and skills in Rust, a modern programming language known for its safety, concurrency, and performance. The questions range from the basics of Rust syntax and data types to more complex topics like ownership rules, lifetimes, and advanced concurrency features. This test is perfect for both beginners wanting to solidify their fundamentals and experienced developers looking to deepen their understanding of Rust's unique features. Ready to challenge yourself and see how well you know Rust? Let's get started!

1. What is the default integer type in Rust?

a) i32
b) i64
c) u32
d) usize

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

a) let v: Vec<i32> = Vec::new();
b) let v = Vec::<i32>::new();
c) let v = new Vec<i32>();
d) let v = vec![i32];

3. How do you define an enum in Rust?

a) enum Direction { Up, Down, Left, Right }
b) type Direction = { Up, Down, Left, Right }
c) struct Direction { Up, Down, Left, Right }
d) class Direction { Up, Down, Left, Right }

4. What does the match statement do in Rust?

a) Iterates over each element in a collection
b) Provides a mechanism for pattern matching
c) Compares two values for equality
d) None of the above

5. How do you handle errors in Rust using the Result type?

a) fn get_number() -> Result<i32, &'static str> { Ok(42) }
b) fn get_number() -> Error<i32> { Fail(42) }
c) fn get_number() -> Result<i32> { Ok(42) }
d) fn get_number() -> Result { Ok(42) }

6. What is the purpose of the & symbol in Rust?

a) It denotes a mutable variable
b) It creates a new thread
c) It is used to reference a value without taking ownership
d) It unpacks a tuple

7. How do you declare a mutable variable in Rust?

a) let mut x = 5;
b) var x = 5;
c) let x = 5;
d) mutable x = 5;

8. How do you implement a trait for a type in Rust?

a) impl CanMove for Point { fn move_up(&mut self) { self.y += 1; } }
b) trait CanMove for Point { fn move_up(&mut self) { self.y += 1; } }
c) Point implements CanMove { fn move_up(&mut self) { self.y += 1; } }
d) extend Point with CanMove { fn move_up(&mut self) { self.y += 1; } }

9. What is the output of the following Rust code?

let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
a) hello
b) Compile-time error
c) Run-time error
d) Undefined behavior

10. How do you update a value in a mutable HashMap?

use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("key", 10);
map.entry("key").or_insert(0) += 10;
a) map["key"] = 20;
b) map.update("key", 20);
c) map.entry("key").or_insert(0) += 10;
d) map.set("key", 20);

11. How are lifetimes specified in Rust?

a) Using the life keyword before a variable declaration
b) By annotating variable references with lifetime specifiers
c) By declaring the lifespan of variables at the start of a program
d) Rust does not have lifetimes

12. What does the following code snippet do?

fn takes_ownership(some_string: String) {
    println!("{}", some_string);
}
let s = String::from("hello");
takes_ownership(s);
println!("{}", s);
a) Prints "hello" twice
b) Prints "hello" once, then causes a compile-time error
c) Prints nothing due to an error
d) Prints "hello", then an empty string

13. What is the trait in Rust used for outputting debug information?

a) Display
b) Debug
c) ToString
d) Serialize

14. How do you create a new thread in Rust?

a) thread::new(|| {...});
b) thread::start(|| {...});
c) thread::run(|| {...});
d) thread::spawn(|| {...});

15. What does unwrap() do when called on a Result type?

a) It returns the value if Result is Ok, otherwise it does nothing
b) It returns the value if Result is Ok, otherwise it panics
c) It ignores the value and continues execution
d) It always returns a default value

16. How do you declare a constant in Rust?

a) let const MAX_POINTS: u32 = 100_000;
b) const MAX_POINTS: u32 = 100_000;
c) var MAX_POINTS: u32 = 100_000;
d) define MAX_POINTS: u32 = 100_000;

17. How do you import a module in Rust?

a) use crate::module_name;
b) import module_name;
c) require module_name;
d) include module_name;

18. What is the purpose of the Cargo.toml file in a Rust project?

a) It contains settings for the compiler.
b) It lists the dependencies of the project.
c) It defines environment variables.
d) It stores the compiled output.

19. How do you specify that a function returns an error in Rust?

a) fn get_index() -> Error<usize>
b) fn get_index() -> Result<usize>
c) fn get_index() -> Result<usize, String>
d) fn get_index() -> Throw<usize>

20. What is the output of the following Rust code?

let mut s = String::from("hello");
s.push_str(", world");
println!("{}", s);
a) hello, world
b) hello
c) , world
d) None of the above

21. How do you define a struct in Rust?

a) struct Point { x: i32, y: i32 }
b) class Point { x: i32, y: i32 }
c) type Point = { x: i32, y: i32 }
d) data Point = { x: i32, y: i32 }

22. How do you update a mutable reference to a value in Rust?

let mut x = 10;
let y = &mut x;
*y += 1;
a) *y += 1;
b) y++ = 1;
c) y += 1;
d) y.update(1);

23. How do you implement the Drop trait for a custom struct?

a) impl Drop for Custom { fn drop(&mut self) { println!("Dropping Custom!"); } }
b) trait Drop for Custom { fn drop(&mut self) { println!("Dropping Custom!"); } }
c) Custom::drop(fn) { println!("Dropping Custom!"); }
d) drop Custom { println!("Dropping Custom!"); }

24. What does the ? operator do in Rust functions that return Result or Option types?

a) It unwraps the value safely, returning the value if Ok or Some, and continues execution if Err or None
b) It panics if the value is Err or None
c) It returns the error or None immediately if applicable, otherwise continues with the unwrapped value
d) It converts Err or None into Ok or Some

25. How do you create a loop in Rust that continues until a condition is met?

a) while counter < 10 { counter += 1; }
b) for i in 0..10 { counter = i; }
c) loop { if counter >= 10 { break; } counter += 1; }
d) until counter == 10 { counter += 1; }

Comments