Rust Generic Quiz

Rust
0 Passed
0% acceptance

40 comprehensive questions on Rust's generic programming system, covering generic functions, structs, enums, impl blocks, and monomorphization — with 11 code examples demonstrating practical generic usage patterns.

40 Questions
~80 minutes
1

Question 1

What are generics in Rust?

A
A way to write code that works with multiple types while maintaining type safety
B
A way to create generic containers
C
A way to avoid type checking
D
A way to create dynamic types
2

Question 2

How do you define a generic function?

A
fn function_name<T>(param: T) -> T { ... }
B
fn function_name(param: Generic) -> Generic { ... }
C
fn function_name(param: T) -> T { ... }
D
Generic functions cannot be defined
3

Question 3

What will this generic function do?

rust
fn largest<T>(list: &[T]) -> &T {
    let mut largest = &list[0];
    for item in list {
        if item > largest {
            largest = item;
        }
    }
    largest
}

fn main() {
    let numbers = vec![34, 50, 25, 100, 65];
    let result = largest(&numbers);
    println!("The largest number is {}", result);
}
A
Prints 'The largest number is 100'
B
Compilation error because T doesn't implement comparison
C
Prints 'The largest number is 34'
D
Runtime error
4

Question 4

How do you add trait bounds to generic functions?

A
fn function<T: Trait>(param: T) or fn function<T>(param: T) where T: Trait
B
fn function(param: T) requires Trait
C
Trait bounds cannot be added to functions
D
Use implements keyword
5

Question 5

What is monomorphization?

A
The process where the compiler creates specific versions of generic code for each concrete type used
B
The process of making code polymorphic
C
The process of optimizing generic code
D
The process of erasing type information
6

Question 6

What are the benefits of monomorphization?

A
Zero runtime cost, static dispatch, optimal performance
B
Smaller binary size
C
Dynamic type checking
D
Runtime polymorphism
7

Question 7

How do you define a generic struct?

A
struct StructName<T> { field: T }
B
struct StructName { field: Generic }
C
struct StructName<T> { field: Generic }
D
Generic structs cannot be defined
8

Question 8

What will this generic struct instantiation create?

rust
struct Point<T> {
    x: T,
    y: T,
}

fn main() {
    let integer_point = Point { x: 5, y: 10 };
    let float_point = Point { x: 1.0, y: 4.0 };
}
A
Two Point instances with different concrete types
B
Compilation error
C
One Point instance
D
Runtime error
9

Question 9

How do you implement methods for generic structs?

A
impl<T> StructName<T> { fn method(&self) { ... } }
B
impl StructName { fn method(&self) { ... } }
C
impl StructName<T> { fn method(&self) { ... } }
D
Methods cannot be implemented for generic structs
10

Question 10

What will this generic method implementation do?

rust
struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}

fn main() {
    let p = Point { x: 5, y: 10 };
    println!("p.x = {}", p.x());
}
A
Prints 'p.x = 5'
B
Compilation error
C
Prints 'p.x = 10'
D
Runtime error
11

Question 11

How do you implement methods only for specific types in generic structs?

A
Use concrete type in impl block: impl Point<f32> { ... }
B
Add type constraints to the struct
C
Use where clauses
D
This is not possible
12

Question 12

What are generic enums?

A
Enums that can hold different types of data in their variants
B
Enums with type parameters
C
Both A and B
D
Enums that cannot be generic
13

Question 13

How is Option<T> defined?

A
enum Option<T> { Some(T), None }
B
enum Option { Some(T), None }
C
struct Option<T> { value: T }
D
Option is not generic
14

Question 14

How is Result<T, E> defined?

A
enum Result<T, E> { Ok(T), Err(E) }
B
enum Result<T> { Ok(T), Err(E) }
C
struct Result<T, E> { ok: T, err: E }
D
Result is not generic
15

Question 15

What is the difference between Option<T> and Result<T, E>?

A
Option represents optional values, Result represents operations that can fail
B
Option has two variants, Result has one
C
Result is simpler than Option
D
There is no difference
16

Question 16

How do you use multiple generic type parameters?

A
fn function<T, U>(param1: T, param2: U)
B
fn function<T U>(param1: T, param2: U)
C
fn function(param1: T, param2: U)
D
Multiple parameters are not allowed
17

Question 17

What are const generics?

A
Generic parameters that are constant values known at compile time
B
Generic parameters that are constant at runtime
C
Generic parameters for constants
D
Const generics don't exist
18

Question 18

How do you define a const generic parameter?

A
fn function<const N: usize>(arr: [i32; N])
B
fn function<N: usize>(arr: [i32; N])
C
fn function(arr: [i32; N])
D
Const generics are not supported
19

Question 19

What is the where clause used for?

A
Specifying complex trait bounds that are clearer than inline bounds
B
Defining where functions are implemented
C
Specifying where generics can be used
D
Where clauses are not used in Rust
20

Question 20

What will this where clause example do?

rust
fn some_function<T, U>(t: T, u: U) -> i32
    where T: Display + Clone,
          U: Clone + Debug
{
    // function body
}
A
Requires T to implement Display and Clone, U to implement Clone and Debug
B
Compilation error
C
Requires T and U to implement the same traits
D
Runtime error
21

Question 21

What is generic lifetime in relation to generics?

A
Lifetime parameters can be generic just like type parameters
B
Generic lifetimes don't exist
C
Lifetimes are not generic
D
Generic lifetimes are the same as type generics
22

Question 22

How do you declare generic lifetimes?

A
fn function<'a, T>(param: &'a T)
B
fn function<T, 'a>(param: &'a T)
C
fn function(param: &'a T)
D
Generic lifetimes cannot be declared
23

Question 23

What is the relationship between generics and performance?

A
Generics provide zero-cost abstractions through monomorphization
B
Generics have runtime overhead
C
Generics are slower than concrete types
D
Generics increase binary size significantly
24

Question 24

What happens to binary size with generics?

A
May increase due to monomorphization creating multiple versions
B
Always decreases
C
Remains the same
D
Dramatically increases
25

Question 25

How do you create a generic type alias?

A
type Result<T> = std::result::Result<T, std::io::Error>;
B
alias Result<T> = std::result::Result<T, std::io::Error>;
C
typedef Result<T> = std::result::Result<T, std::io::Error>;
D
Generic type aliases are not supported
26

Question 26

What is generic associated type?

A
An associated type that is itself generic
B
A type associated with generics
C
Generic types in associated positions
D
Generic associated types don't exist
27

Question 27

How do you define a generic associated type?

A
trait Trait { type Associated<T>; }
B
trait Trait { type Associated: Generic; }
C
trait Trait { generic type Associated; }
D
Generic associated types are not supported
28

Question 28

What is higher-kinded types in relation to generics?

A
Types that take other types as parameters, like Vec<T> taking T
B
Types that are higher-order
C
Types that cannot be generic
D
Higher-kinded types are not supported in Rust
29

Question 29

What is the turbofish operator (::<>)?

A
Explicit type annotation syntax for generic function calls
B
Operator for fast operations
C
Operator for type casting
D
The turbofish operator doesn't exist
30

Question 30

When would you use the turbofish operator?

A
When the compiler cannot infer the generic type parameters
B
For all generic function calls
C
To improve performance
D
Turbofish is never needed
31

Question 31

What will this turbofish example do?

rust
let numbers: Vec<i32> = vec![];
let result = numbers.into_iter().collect::<Vec<_>>();
A
Explicitly specifies Vec<_> as the collect target type
B
Compilation error
C
Uses default collection type
D
Runtime error
32

Question 32

What is generic covariance and contravariance?

A
How generic types relate to their type parameters' subtyping relationships
B
How generics vary in performance
C
How generics handle variance
D
Variance is not relevant to generics
33

Question 33

What is phantom data in generic contexts?

A
Zero-sized type parameters used to enforce type safety or carry metadata
B
Data that is invisible at runtime
C
Data that is not generic
D
Phantom data doesn't exist
34

Question 34

How do you use PhantomData?

A
struct MyStruct<T> { data: PhantomData<T> }
B
struct MyStruct { phantom: T }
C
struct MyStruct<T> { phantom: PhantomData }
D
PhantomData is not used in structs
35

Question 35

What is generic specialization?

A
Providing specific implementations for certain generic types
B
Making generics more specific
C
Specializing generic code for performance
D
Generic specialization is not supported
36

Question 36

What are the limitations of generics in Rust?

A
Cannot perform dynamic dispatch, monomorphization can increase binary size
B
Cannot use with all types
C
Cannot be nested deeply
D
Generics have no limitations
37

Question 37

How do generics interact with trait objects?

A
Generics use static dispatch, trait objects use dynamic dispatch
B
They cannot be used together
C
Generics replace trait objects
D
Trait objects replace generics
38

Question 38

What is generic inference?

A
The compiler's ability to automatically determine generic type parameters
B
Manual specification of generic types
C
Runtime type determination
D
Generic inference doesn't exist
39

Question 39

When does generic inference fail?

A
When there are multiple valid interpretations or insufficient context
B
When types are complex
C
When using turbofish
D
Inference never fails
40

Question 40

In a scenario where you're building a generic data structure that needs to work with any comparable type, how would you design the generic constraints?

A
Use trait bounds like <T: Ord> to ensure types can be compared and ordered
B
Use runtime type checking
C
Avoid generics and use concrete types
D
Use trait objects for all operations

QUIZZES IN Rust