Rust is a systems programming language designed for performance and safety. One of its key features is support for traits and generics, enabling flexible, reusable, and type-safe code. This set of 30 multiple-choice questions (MCQs) covers essential topics such as defining traits, using generics in functions and structs, and understanding trait bounds and lifetimes.
1. Defining and Implementing Traits
In Rust, a trait is used to: a) Define the structure of an object b) Define shared behavior for types c) Manage memory safety d) Provide function signatures
Which of the following syntax is used to define a trait in Rust? a) trait MyTrait {} b) trait MyTrait() {} c) fn MyTrait {} d) impl MyTrait {}
What is required to implement a trait for a type in Rust? a) The type must implement all methods defined in the trait b) The trait must be mutable c) The type must be explicitly declared d) The type must be a struct
Which keyword is used to implement a trait for a specific type? a) define b) impl c) trait d) type
What does the following code represent in Rust? trait Speak { fn speak(&self); } a) A struct with a method b) A trait with a method definition c) A function definition d) A type declaration
2. Generics in Functions and Structs
What is the main advantage of using generics in Rust? a) To make code more specific b) To improve performance by reducing memory usage c) To allow a function or struct to work with multiple data types d) To make functions more complex
Which of the following is an example of a generic function in Rust? a) fn print<T>(value: T) { println!("{}", value); } b) fn print(value: T) { println!("{}", value); } c) fn print(value) { println!("{}", value); } d) fn print<T>(value: String) { println!("{}", value); }
How would you define a struct that uses a generic type in Rust? a) struct Point<T> { x: T, y: T } b) struct Point<T>(x: T, y: T) c) struct Point(x: T, y: T) d) struct Point { T: x, T: y }
Which of the following is true about generics in Rust? a) Generics only work with primitive types b) Generics can be used with functions and structs c) Generics cannot be used with traits d) Generics are not type-safe in Rust
In Rust, what does the T in a generic function or struct represent? a) The type parameter b) A specific value c) A struct type d) A trait bound
3. Trait Bounds and Constraints
In Rust, trait bounds are used to: a) Limit the types that can be used with generics b) Define default values for generics c) Ensure types can perform certain actions d) All of the above
Which syntax is used to specify a trait bound in a generic function in Rust? a) fn func<T: Trait>(x: T) b) fn func<T::Trait>(x: T) c) fn func(T: Trait) d) fn func<T>(x: T where T: Trait)
What is the purpose of the Copy trait in Rust? a) To allow a type to be passed by value b) To prevent copying of a value c) To define the cloning behavior of a type d) To provide mutable access to data
How do you specify multiple trait bounds for a generic type in Rust? a) fn func<T: Trait1, Trait2>(x: T) b) fn func<T: Trait1 + Trait2>(x: T) c) fn func<T: (Trait1, Trait2)>(x: T) d) fn func<T: (Trait1; Trait2)>(x: T)
When can a trait bound be omitted for a generic type in Rust? a) When the type implements all necessary traits b) When the type is a struct c) When the type is specified as a reference d) Never, trait bounds must always be defined
4. Lifetimes and Generic Parameters
Lifetimes in Rust are used to: a) Ensure types are properly allocated in memory b) Prevent memory leaks and dangling references c) Provide default values for generic parameters d) Control the execution flow of a program
What does the following syntax represent? fn first_word<'a>(s: &'a str) -> &'a str a) A function with a lifetime annotation b) A reference to a string c) A struct with a lifetime d) A mutable reference to a string
In Rust, a lifetime parameter 'a is used to: a) Specify the lifetime of a reference in a function or struct b) Define the type of a reference c) Mark a function as returning a reference d) Set the lifetime of a type parameter
What is the purpose of the 'static lifetime in Rust? a) It denotes the shortest possible lifetime b) It is the lifetime of a reference that is valid for the entire program c) It is used for temporary variables d) It represents a lifetime in a local scope
Which of the following is the correct way to specify a lifetime for a struct in Rust? a) struct Foo<'a> { value: &'a str } b) struct Foo<'a> { value: String } c) struct Foo { value: &'a str } d) struct Foo<'a> { value: str }
5. Miscellaneous
What does the Copy trait allow in Rust? a) The type can be duplicated with a simple assignment b) The type must implement a custom copy function c) The type can only be cloned d) The type can be copied by reference
Which of the following is true about generic types in Rust? a) They must always be explicitly defined b) They allow a function to operate on different data types without code duplication c) They cannot be used in structs d) They are slower than non-generic types
What is the default trait bound for a generic type parameter in Rust? a) Clone b) Debug c) Copy d) There is no default trait bound
In Rust, how do you specify that a function takes a reference to a generic type? a) fn foo<'a, T>(x: &'a T) b) fn foo<T: &T>(x: T) c) fn foo<T>(x: &T) d) fn foo<'a, T>(x: T)
Which of the following is used to specify that a function or struct works with types that implement multiple traits in Rust? a) where clause b) trait clause c) type clause d) default clause
What is a typical use case for lifetimes in Rust? a) To specify how long a reference should be valid b) To manage the visibility of a variable c) To control the memory allocation of a struct d) To pass ownership of a variable
Which of the following is a valid syntax for using lifetimes in a function signature? a) fn foo<'a>(x: &'a str) -> &'a str b) fn foo<'a>(&'a str) -> &'a str c) fn foo<'a>(&'a str) -> str d) fn foo<'a>(x: str) -> str
What does the + symbol indicate when used in trait bounds? a) Multiple traits are required for the type parameter b) The type parameter must be a reference c) The type parameter must implement a specific trait d) The type parameter is optional
What is the main purpose of the Option type in Rust? a) To represent an optional value, either Some or None b) To handle memory allocation c) To represent mutable values d) To manage function signatures
How would you define a trait with a method that can be implemented by any type that supports addition? a) trait Addable { fn add(self, other: Self) -> Self; } b) trait Addable<T> { fn add(self, other: Self) -> Self; } c) trait Addable { fn add<T>(self, other: T) -> T; } d) trait Addable { fn add(self, other: i32) -> i32; }
Answer Key (Tabular Form)
Qno
Answer
1
b) Define shared behavior for types
2
a) trait MyTrait {}
3
a) The type must implement all methods defined in the trait
4
b) impl
5
b) A trait with a method definition
6
c) To allow a function or struct to work with multiple data types
7
a) fn print<T>(value: T) { println!("{}", value); }
8
a) struct Point<T> { x: T, y: T }
9
b) Generics can be used with functions and structs
10
a) The type parameter
11
a) Limit the types that can be used with generics
12
a) fn func<T: Trait>(x: T)
13
a) To allow a type to be passed by value
14
b) fn func<T: Trait1 + Trait2>(x: T)
15
a) When the type implements all necessary traits
16
b) Prevent memory leaks and dangling references
17
a) A function with a lifetime annotation
18
a) Specify the lifetime of a reference in a function or struct
19
b) It is the lifetime of a reference that is valid for the entire program
20
a) struct Foo<'a> { value: &'a str }
21
a) The type can be duplicated with a simple assignment
22
b) They allow a function to operate on different data types without code duplication
23
d) There is no default trait bound
24
c) fn foo<T>(x: &T)
25
a) where clause
26
a) To specify how long a reference should be valid
27
a) fn foo<'a>(x: &'a str) -> &'a str
28
a) Multiple traits are required for the type parameter
29
a) To represent an optional value, either Some or None