TUTORIALS

Rust Multiple Choice Questions (MCQs)

1 .
Topic:  Introduction
Question:
Who designed Rust?
A
Doug Cutting
B
JetBrains
C
Graydon Hoare
D
Robert Griesemer
Answer: C
Explanation: Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and others
2 .
Topic:  Introduction
Question:
Rust is syntatically similar to which programming language?
A
C++
B
Python
C
C Sharp
D
Java
Answer: A
Explanation: The basic syntax of Rust is similar to C and C++, having curly brackets and keywords for control flow.
3 .
Topic:  Introduction
Question:
Which among the following is permitted by rust?
A
Null Pointers
B
let keyword
C
Data Races
D
Automated Garbage Collectors
Answer: B
Explanation: Rust ensures memory safety, hence it does not permit null pointers, dangling pointers, or data races
4 .
Topic:  Keywords
Question:
Which among the following is not a valid keyword in Rust?
A
mut
B
impl
C
let
D
var
Answer: D
Explanation: Rust uses let for immutable variables and mut for mutable variables and impl defines implementation on types
5 .
Topic:  Functions
Question:
Which keyword is used to write a function in rust?
A
fn
B
function
C
func
D
f
Answer: A
Explanation: fn keyword is used to define a function: fn main() { println!("Hello world!!!"); }
6 .
Topic:  Functions
Question:
What does ::new indicates?
A
static method
B
abstract method
C
final method
D
main method
Answer: A
Explanation: ::new is a static method
7 .
Topic:  Functions
Question:
What are the result types in Rust?
A
structure
B
enumerations
C
class
D
union
Answer: B
Explanation: Result types in Rust are enums
8 .
Topic:  Functions
Question:
Which of the following are Result variants in Rust?
A
success and err
B
Success and Error
C
Ok and Error
D
Ok and Err
Answer: D
Explanation: Ok and Err are the result variants in Rust
9 .
Topic:  Functions
Question:
Which method is defined on Result types?
A
main
B
expect
C
handle
D
init
Answer: B
Explanation: expect method is defined on Result types
10 .
Topic:  Functions
Question:
Which brackets are used as placeholder in rust?
A
{ }
B
( )
C
[ ]
D
< >
Answer: A
Explanation: As a placeholder we can use curly braces { }
11 .
Topic:  Variables and Mutability
Question:
Constants cant be defined in which scope
A
Global
B
Local
C
Method
D
None
Answer: D
Explanation: Constants can be defined in global,local and method scope
12 .
Topic:  Data Types
Question:
Rust is
A
statically typed
B
dynamically typed
C
strongly typed
D
untyped
Answer: A
Explanation: Rust is statically-typed as the type of a variable is known at compile-time instead of at run-time
13 .
Topic:  Data Types
Question:
Choose the correct scalar types in Rust
A
integers, signed numbers, Booleans, and String
B
integers, floating-point numbers, Booleans, and characters
C
integers, floating-point numbers and characters
D
integers, floating-point numbers and Booleans
Answer: B
Explanation: integers, floating-point numbers, Booleans, and characters are the scalar types used in Rust
14 .
Topic:  Data Types
Question:
Which keyword is used to specify Boolean type?
A
boolean
B
Boolean
C
bool
D
bl
Answer: C
Explanation: bool is used for boolean type in Rust which uses one byte of memory.The bool type is a datatype which can be either true or false. It is used in comparisons and bitwise operations like &, |, and ! fn main() { let a = true; let b: bool = false; if a { println!("a is true"); } }
15 .
Topic:  Data Types
Question:
Which are the two primitive compound types supported by Rust?
A
tuples and arrays
B
tuples and lists
C
lists and arrays
D
maps and arrays
Answer: A
Explanation: Tuple is a collection of values of different types with a comma separated values enclosed within parenthesis ( ) Eg: let tuple1 = (1, "world", 50.5, true); Array is a collection of objects of the same type T, stored in contiguous memory which are created using brackets [ ] Eg: let array1: [i32; 5] = [1, 2, 3, 4, 5];
16 .
Topic:  Data Types
Question:
Which of the following is false with respect to tuples?
A
tup is the keyword used to declare tuples
B
Tuples have a fixed length
C
tuple element can be accessed by using dot (.)
D
every element of a tuple must have the same type
Answer: D
Explanation: A tuple is a collection of values of different types let tuple1 = (8, "example", 101.67, false);
17 .
Topic:  Data Types
Question:
Which is not a valid declaration of an array?
A
let a: [i32; 5] = [1, 2, 3, 4, 5];
B
let a = [3; 5];
C
let a: = [3; 5];
D
let a = [1, 2, 3, 4, 5];
Answer: C
Explanation: Array can be declared in various forms let a: [i32; 5] = [1, 2, 3, 4, 5]; let a = [3; 5]; let a = [1, 2, 3, 4, 5];
18 .
Topic:  Data Types
Question:
Which symbol is used to declare the functions with return values?
A
::
B
:
C
< >
D
->
Answer: D
Explanation: Functions can return value to calling function and the return values are not explicity named instead we can declare their type after an arrow (->) fn method() -> i32 { 7 }
19 .
Topic:  Control Flow
Question:
What is the output of the following program snippet:

fn main() {
    let three = 3;

    if three {
        println!("number was three");
    }
}

A
number was three
B
unrecognised keyword println!
C
error mismatched types
D
3
Answer: C
Explanation: Here the variable three holds 3 and if expects bool value , hence the error occurs because of mismatched types
20 .
Topic:  Control Flow
Question:
Which among the following loop is not supported by Rust?
A
loop
B
while
C
for
D
do while
Answer: D
Explanation: loop in the keyword used in Rust to execute a block of code over and over again until it is explicitly specified to stop. Loop can be used as an retry operation to check if the thread has completed its job. Rus supports while and for and doesnot use do while
21 .
Topic:  Ownership
Question:
Which among the following is false with respect to ownership rules?
A
Each value in Rust has a variable which is the owner.
B
There can only be one owner at a time.
C
When the owner goes out of scope, the value will be dropped.
D
owner never goes out of scope
Answer: D
Explanation: The core feature of Rust is ownership. There are few rules for ownership.owner wil go out of scope, when it does the value of the variable is dropped
22 .
Topic:  Ownership
Question:
Which operator allows to use namespace?
A
::
B
:
C
.
D
!
Answer: A
Explanation: Double colon (::) operator is used for namespace eg: String::from("example");
23 .
Topic:  Ownership
Question:
What does the String data type hold in Rust?
A
a pointer to the memory where String resides and its capacity
B
a pointer to the memory where String resides, a length, and a capacity
C
a string value and its length
D
a string value, a length, and a capacity
Answer: B
Explanation: String data type holds a pointer to the memory where String resides, a length, and a capacity
24 .
Topic:  Ownership
Question:
What does the below statements infer?

let s1 = String::from("example");
let s2 = s1;

A
a copy of the value in s1 is binded to s2
B
copy the data on the heap that the pointer in string refers to
C
copy the pointer, the length, and the capacity that are on the stack.
D
copy the data on the string pool that the pointer in string refers to
Answer: C
Explanation: When s1 is assigned to s2, the entire string data on the stack including pointer, length and capacity are copied.
25 .
Topic:  Ownership
Question:
Which function assists in cleaning up the heap memory in Rust?
A
drop
B
deallocate
C
flush
D
clean
Answer: A
Explanation: drop function cleans up the heap memory
26 .
Topic:  Ownership
Question:
Automatic copying in rust ______________
A
inexpensive in terms of runtime performance
B
create “deep” copies of your data
C
hinders runtime performance
D
All of the above
Answer: A
Explanation: Whenever variable goes out of scope, it will free the memory and rust doesn’t have “deep” copies of the data which improves runtime performance
27 .
Topic:  Ownership
Question:
Identify the types which are not Copy in Rust?
A
char
B
bool
C
(i32, i32)
D
(i32, String)
Answer: D
Explanation: Rust has a special annotation called the Copy trait. Types that are Copy can be moved without owning the value.If a type has the Copy trait, an older variable is still usable after assignment
28 .
Topic:  References and Borrowing
Question:
What does & in the function signature of Rust indicate?
A
type of the parameter
B
address
C
pointer
D
reference
Answer: A
Explanation: let len = function(&s1); here & indicates the type of the parameter and s is a reference
29 .
Topic:  References and Borrowing
Question:
When does the data race occur in Rust?
A
Two or more pointers access the same data at the same time.
B
At least one of the pointers is being used to write to the data.
C
There’s no mechanism being used to synchronize access to the data
D
All of the above
Answer: D
Explanation: Rust can prevent data races at compile time.
30 .
Topic:  References and Borrowing
Question:
Which among the following is not a valid rule for references in Rust?
A
References must always be valid
B
one mutable reference
C
the compiler guarantees that references will never be dangling references
D
immutable references are not to be used
Answer: D
Explanation: Immutable references can be used but at any point either immutable references should be used or mutable references
31 .
Topic:  Slice Type
Question:
What is the proper notation of Rust's range syntax?
A
let slice = &s[0..2];
B
let slice = &s[0-2];
C
let slice = s[0-2];
D
let slice = &s[0:2];
Answer: A
Explanation: .. Is used to specify the ranges in Rust
32 .
Topic:  Slice Type
Question:
String literals are
A
not slices
B
static
C
mutable
D
immutable
Answer: D
Explanation: String literals are immutable
33 .
Topic:  Structs
Question:
Which is not true with respect to structs in Rust?
A
flexible than tuples
B
order of the data is important to access the values
C
dot notation is used to specify the value from a struct
D
key: value pairs to be specified inside { }
Answer: B
Explanation: in Structs, the order of the data to be accessed is not important
34 .
Topic:  Structs
Question:
In Tuple structs
A
only type of the fields is specified
B
names associated with their fields to be specified
C
start with the tuple keyword
D
cant destructure them into their individual pieces
Answer: A
Explanation: Structs defined to look similar to tuples are called tuple structs. To define a tuple struct, start with the struct keyword and the struct name followed by the types in the tuple
35 .
Topic:  Structs
Question:
What is the use of unit-like structs?
A
Useful for generics.
B
If you don't want to store in the type itself.
C
behave similar to ().
D
All of the above
Answer: D
Explanation: Unit-like structs can be useful in situations in which you need to implement a trait on some type but don’t have any data that you want to store in the type itself.
36 .
Topic:  Structs
Question:
Which keyword is used to create a method?
A
impl
B
method
C
fn
D
no keyword
Answer: A
Explanation: Methods are similar to functions. Functions are declared with the fn keyword and their name, they can have parameters and a return value, and they contain some code that is run when they’re called from somewhere else where as methods are defined within the context of a structand self should be its first parameter. impl keyword is used to create a method in Rust
37 .
Topic:  Structs
Question:
What are associated functions?
A
defining functions within methods
B
associated with structs
C
return a new instance of the struct
D
cant be used for constructors
Answer: C
Explanation: Associated functions are the functions defined within impl block without the self parameter. They are not methods as they donot have any instance of the struct to work with. They are used for constructors
38 .
Topic:  Structs
Question:
Which among the following is true with respect to Multiple impl blocks?
A
Each struct is allowed to have multiple impl blocks
B
each method in its own impl block
C
first parameter is always self
D
All of the above
Answer: D
Explanation: Each struct is allowed to have multiple impl blocks. Each impl block have its own function defined
39 .
Topic:  Enums and Pattern Matching
Question:
Choose the valid variant of enum in Rust?
A
can have no data associated with it at all
B
includes an anonymous struct inside it
C
includes a single String
D
All of the above
Answer: D
Explanation: Enum have some advantages over structs.Data can be attached to each variant of the enum directly where each variant can have different types and amounts of associated data.
40 .
Topic:  Enums and Pattern Matching
Question:
Which among the following is false with respect to Option Enum in Rust?
A
encode the concept of a value being present or absent
B
compile checks whether all the cases are handled
C
prevent bugs
D
need to bring it into scope explicitly
Answer: D
Explanation: Option is an enum defined by standard library. Some and None are the variants of Option
41 .
Topic:  Enums and Pattern Matching
Question:
Which is the control flow operator used for pattern matching in Rust?
A
option
B
match
C
control
D
self
Answer: B
Explanation: match is a powerful control flow operator based on pattern matching. match can be used to run code conditionally.match allows us to compare a value against a series of patterns and then execute code based on which pattern matches.
42 .
Topic:  Enums and Pattern Matching
Question:
Which operator separates the pattern and the code to be executed?
A
=>
B
->
C
::
D
_
Answer: A
Explanation: => operator separates the pattern and the code to run
43 .
Topic:  Enums and Pattern Matching
Question:
Which operator is used as placeholder in Rust for pattern matching?
A
:
B
::
C
->
D
_
Answer: D
Explanation: underscore(_) acts a wildcard in pattern matching. The _ pattern will match any value
44 .
Topic:  Enums and Pattern Matching
Question:
Which among the following is not true regarding if let in Rust?
A
else can be included within if let
B
if let takes a pattern and an expression separated by an = sign
C
if let takes a pattern and an expression separated by an => sign
D
matches one pattern and ignoring the rest
Answer: C
Explanation: The if let syntax combines if and let in such a way that it handle values that match one pattern and ignores the rest. match or if let can be used to extract and use the values, depending on how many cases are needed to handle
45 .
Topic:  Enums and Pattern Matching
Question:
Which standard library type helps to use the type system in order to prevent errors in Rust?
A
Option<T>
B
Cargo
C
Crate
D
Vec<T>
Answer: A
Explanation: Option<T> is Rust's standard library helps to use the type system to prevent errors
46 .
Topic:  Packages, Crates and Modules
Question:
Which Cargo feature helps to build, test, and share crates?
A
match
B
Packages
C
Option<T>
D
Modules
Answer: B
Explanation: A crate is a binary or library similar to the packages which are used in other langugaes. It compiles individually. src/main.rs is the crate root/ entry point for a binary crate and src/lib.rs is the entry point for a library crate. A crate is a package, which can be shared via crates.io
47 .
Topic:  Packages, Crates and Modules
Question:
Which among the following has a tree of modules that produces a library or executable?
A
Modules
B
Crates
C
Packages
D
Paths
Answer: B
Explanation: A crate is a binary or library similar to the packages which are used in other langugaes. It compiles individually. src/main.rs is the crate root/ entry point for a binary crate and src/lib.rs is the entry point for a library crate. A crate is a package, which can be shared via crates.io
48 .
Topic:  Packages, Crates and Modules
Question:
Which helps in controlling the organization, scope and Paths privacy?
A
Paths
B
Crates
C
Modules
D
Packages
Answer: C
Explanation: Modules helps in organizing the code within a crate into groups for readability and easy reusability. It controls the privacy of items telling which should be private and public
49 .
Topic:  Packages, Crates and Modules
Question:
What is a way of naming an item, such as a struct, function, or module called?
A
Crates
B
Packages
C
Modules
D
Paths
Answer: D
Explanation: Paths that allows us to name items and find an item in a module tree. Paths can be absolute or relative
50 .
Topic:  Packages, Crates and Modules
Question:
From which source file does the Rust compiler start?
A
crate root
B
cargo root
C
root
D
use root
Answer: A
Explanation: src/main.rs is the crate root/ entry point for a binary crate and src/lib.rs is the entry point for a library crate