30 Rust Quiz Questions and Answers

Rust is a modern, systems-level programming language known for its safety, performance, and concurrent programming features. It was developed by Mozilla Research and first released in 2010. Rust is designed to address common issues in software development, such as memory safety, data races, and null pointer dereferences, which can lead to bugs and security vulnerabilities. Here is an overview of Rust and its key features:

Memory Safety: Rust’s most notable feature is its emphasis on memory safety. The language enforces strict ownership rules and a borrow checker to prevent common programming errors like null pointer dereferences and data races, which are major sources of bugs and security vulnerabilities in other programming languages.

Ownership and Borrowing: In Rust, each value has a single owner, and the ownership can be transferred or borrowed using references. The borrow checker ensures that there are no data races or dangling pointers during compile-time, leading to safer concurrent programming.

Zero-cost Abstractions: Rust aims to provide high-level abstractions without compromising performance. It guarantees “zero-cost abstractions,” meaning that abstractions like generics and traits come with no runtime overhead.

Concurrency Support: Rust offers powerful concurrency primitives like threads and channels for building concurrent applications safely. Its ownership system helps prevent common threading bugs like data races.

Just to let you know

Sign up for a free OnlineExamMaker account to create an interactive online quiz in minutes – automatic grading & mobile friendly.

In this article

Part 1: 30 Rust quiz questions & answers

1. What is the primary feature that distinguishes Rust from many other programming languages?
a) Object-oriented programming
b) Memory safety
c) Dynamic typing
d) Interpreted execution
Answer: b) Memory safety

2. Which of the following is NOT a key goal of Rust?
a) High performance
b) Conciseness
c) Concurrency
d) Memory safety
Answer: b) Conciseness

3. In Rust, what is the purpose of the borrow checker?
a) To manage memory allocation for variables
b) To prevent data races and null pointer dereferences
c) To facilitate pattern matching
d) To enforce strict typing rules
Answer: b) To prevent data races and null pointer dereferences

4. Which term is used in Rust to describe the scope in which a borrowed reference is valid?
a) Lifespan
b) Ownership
c) Dangling
d) Mutation
Answer: a) Lifespan

5. What is the symbol used to denote a comment in Rust?
a) //
b) —
c) /*
d) #
Answer: a) //

6. Which keyword is used to define a new variable in Rust?
a) let
b) var
c) define
d) const
Answer: a) let

7. In Rust, how is a function declared?
a) def function_name():
b) fn function_name()
c) void function_name()
d) func function_name()
Answer: b) fn function_name()

8. What is the default visibility of variables and functions in Rust?
a) Public
b) Private
c) Protected
d) Package-private
Answer: b) Private

9. What is the macro system used in Rust called?
a) define
b) macro_rules!
c) def
d) macro()
Answer: b) macro_rules!

10. In Rust, how do you define an immutable variable?
a) let x = 5;
b) const x = 5;
c) let mut x = 5;
d) immutable x = 5;
Answer: a) let x = 5;

11. What does the “mut” keyword indicate when defining a variable in Rust?
a) The variable cannot be changed after initialization.
b) The variable can be changed, and its value is mutable.
c) The variable is a constant and cannot be changed.
d) The variable is a reference to another variable.
Answer: b) The variable can be changed, and its value is mutable.

12. What is the output of the following code snippet?
“`rust
let x = 5;
let y = x + 3;
println!(“The value of y is: {}”, y);
“`
a) The value of y is: 5
b) The value of y is: 8
c) The value of y is: x + 3
d) The code will not compile due to a syntax error.
Answer: b) The value of y is: 8

13. In Rust, which data type is used for floating-point numbers with double precision?
a) float
b) double
c) f64
d) real
Answer: c) f64

14. Which of the following is NOT a valid numeric literal in Rust?
a) 42
b) 3.14
c) 0b1010
d) “Hello”
Answer: d) “Hello”

15. How do you declare a tuple in Rust?
a) (1, 2, 3)
b) tuple(1, 2, 3)
c) new_tuple!(1, 2, 3)
d) (1; 2; 3)
Answer: a) (1, 2, 3)

Part 2: Download Rust questions & answers for free

Download questions & answers for free

16. In Rust, what does the “match” keyword do?
a) It declares a new function.
b) It starts a loop.
c) It compares two variables for equality.
d) It performs pattern matching.
Answer: d) It performs pattern matching.

17. What does the “if let” syntax in Rust do?
a) It allows pattern matching in if statements.
b) It checks if a variable is defined.
c) It defines a new variable within an if statement.
d) It converts a value to a boolean.
Answer: a) It allows pattern matching in if statements.

18. How do you create a new vector in Rust?
a) new_vec!(1, 2, 3)
b) [1, 2, 3]
c) vec[1, 2, 3]
d) vec![1, 2, 3]
Answer: d) vec![1, 2, 3]

19. What is the Rust keyword used to exit a loop prematurely?
a) end
b) done
c) break
d) exit
Answer: c) break

20. In Rust, how do you create a new string slice from an existing string?
a) “Hello”.to_string()
b) &”Hello”
c) “Hello”[..]
d) “Hello”.slice()
Answer: b) &”Hello”

21. What is the purpose of the “unwrap()” method on Option and Result types in Rust?
a) To extract the value from the Option or Result if it exists, or panic if it is None or an Err.
b) To convert an Option or Result to a boolean value.
c) To wrap a value in an Option or Result.
d) To convert an Option or Result to a string.
Answer: a) To extract the value from the Option or Result if it exists, or panic if it is None or an Err.

22. How do you handle errors returned by a function that returns a Result type in Rust?
a) Use the “if let” syntax.
b) Use the “match” statement.
c) Use the “unwrap()” method.
d) Use the “?” operator.
Answer: d) Use the “?” operator.

23. Which module is used to manage threads in Rust?
a) std::thread
b) std::sync
c) std::process
d) std::concurrent
Answer: a) std::thread

24. What is the purpose of the “Arc” type in Rust?
a) To create a reference-counted atomic pointer.
b) To create an asynchronous task.
c) To perform atomic operations on integers.
d) To define an asynchronous closure.
Answer: a) To create a reference-counted atomic pointer.

25. In Rust, what is the primary mechanism for handling asynchronous programming?
a) Promises
b) Callbacks
c) Futures
d) Threads
Answer: c) Futures

You might like to know

Create an auto-grading quiz/assessment without any coding – try OnlineExamMaker today!

26. What is the name for Rust’s official package manager?
a) build
b) manage
c) rustpkg
d) Cargo
Answer: d) Cargo

27. How do you create a new Rust project using Cargo?
a) cargo init
b) cargo new
c) rust new
d) create project
Answer: b) cargo new

28. What is the purpose of the “mod” keyword in Rust?
a) To declare a new variable.
b) To define a new function.
c) To include a module from an external crate.
d) To declare a new module.
Answer: d) To declare a new module.

29. What is the output of the following code snippet?
“`rust
fn main() {
let mut vec = vec![1, 2, 3];
for i in &vec {
vec.push(*i);
}
println!(“{:?}”, vec);
}
“`
a) [1, 2, 3]
b) [1, 2, 3, 1, 2, 3]
c) The code will not compile due to an error.
d) The output cannot be determined.
Answer: c) The code will not compile due to an error.

30. Which of the following statements is true about Rust’s “unsafe” keyword?
a) It allows writing code that is guaranteed to be safe.
b) It indicates that the code is unsafe and could lead to undefined behavior.
c) It is a keyword used to define global constants.
d) It is used to perform type casting in Rust.
Answer: b) It indicates that the code is unsafe and could lead to undefined behavior.

Part 3: Best online quiz making platform – OnlineExamMaker

OnlineExamMaker gives you everything you need to create interactive online quizzes to assess students and staffs, and engage potential customers. The online quiz platform offers options for adding multimedia elements, such as images and videos, to enhance the quiz-taking experience.

Create Your Next Quiz/Exam with OnlineExamMaker

SAAS, free forever
100% data ownership