Rust Traits Quiz

Rust
0 Passed
0% acceptance

40 comprehensive questions on Rust's trait system, covering trait definitions, implementations, bounds, associated types, and trait objects — with 11 code examples demonstrating practical trait usage patterns.

40 Questions
~80 minutes
1

Question 1

What is a trait in Rust?

A
A way to define shared behavior that types can implement
B
A type of struct
C
A collection of functions
D
A way to define constants
2

Question 2

How do you define a trait in Rust?

A
trait TraitName { fn method(&self); }
B
struct TraitName { fn method(&self); }
C
class TraitName { fn method(&self); }
D
interface TraitName { fn method(&self); }
3

Question 3

How do you implement a trait for a type?

A
impl TraitName for TypeName { fn method(&self) { ... } }
B
trait TraitName for TypeName { fn method(&self) { ... } }
C
TypeName implements TraitName { fn method(&self) { ... } }
D
fn TraitName for TypeName { fn method(&self) { ... } }
4

Question 4

What will this trait implementation do?

rust
trait Drawable {
    fn draw(&self);
}

struct Circle;

impl Drawable for Circle {
    fn draw(&self) {
        println!("Drawing a circle");
    }
}

fn main() {
    let c = Circle;
    c.draw();
}
A
Prints 'Drawing a circle'
B
Compilation error
C
Prints nothing
D
Runtime error
5

Question 5

What are trait bounds in generic functions?

A
Constraints that specify what traits generic types must implement
B
Limits on the size of generic types
C
Bounds on trait method implementations
D
Memory bounds for trait objects
6

Question 6

How do you specify trait bounds in function signatures?

A
fn function<T: Trait>(param: T)
B
fn function<T>(param: T) where T: Trait
C
Both A and B are valid
D
fn function(param: Trait)
7

Question 7

What will this generic function with trait bounds do?

rust
trait Summary {
    fn summarize(&self) -> String;
}

fn notify<T: Summary>(item: &T) {
    println!("Breaking news! {}", item.summarize());
}

struct NewsArticle {
    headline: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("Headline: {}", self.headline)
    }
}

fn main() {
    let article = NewsArticle { headline: String::from("Rust is great!") };
    notify(&article);
}
A
Prints 'Breaking news! Headline: Rust is great!'
B
Compilation error
C
Prints 'Breaking news!'
D
Runtime error
8

Question 8

What are associated types in traits?

A
Types that are part of a trait definition but specified by implementations
B
Types that are automatically inferred
C
Types that are shared across all implementations
D
Types that cannot be changed
9

Question 9

How do you define an associated type in a trait?

A
trait TraitName { type AssociatedType; }
B
trait TraitName { associated type AssociatedType; }
C
trait TraitName<T> { ... }
D
trait TraitName { fn type AssociatedType; }
10

Question 10

How do you specify associated types in trait implementations?

A
impl TraitName for Type { type AssociatedType = ConcreteType; }
B
impl TraitName for Type<ConcreteType> { ... }
C
impl TraitName<ConcreteType> for Type { ... }
D
Associated types are automatically inferred
11

Question 11

What is the difference between generics and associated types?

A
Generics allow multiple implementations per type, associated types allow only one
B
Associated types are more flexible than generics
C
Generics are only for functions, associated types for traits
D
There is no functional difference
12

Question 12

What are trait objects?

A
References to types that implement a trait, enabling dynamic dispatch
B
Objects that contain trait definitions
C
Compiled trait implementations
D
Trait metadata
13

Question 13

How do you create a trait object?

A
&dyn Trait or Box<dyn Trait>
B
Trait::new()
C
dyn Trait {}
D
Trait {}
14

Question 14

What is the difference between static and dynamic dispatch?

A
Static dispatch is resolved at compile time, dynamic dispatch at runtime
B
Dynamic dispatch is faster
C
Static dispatch uses more memory
D
They are identical
15

Question 15

What will this trait object code do?

rust
trait Animal {
    fn make_sound(&self);
}

struct Dog;
struct Cat;

impl Animal for Dog {
    fn make_sound(&self) { println!("Woof!"); }
}

impl Animal for Cat {
    fn make_sound(&self) { println!("Meow!"); }
}

fn main() {
    let animals: Vec<Box<dyn Animal>> = vec![Box::new(Dog), Box::new(Cat)];
    for animal in animals {
        animal.make_sound();
    }
}
A
Prints 'Woof!' then 'Meow!'
B
Compilation error
C
Prints 'Woof!' twice
D
Runtime error
16

Question 16

What are default implementations in traits?

A
Method implementations provided in the trait definition that can be overridden
B
Automatic implementations generated by the compiler
C
Default parameter values for trait methods
D
Fallback implementations when custom ones fail
17

Question 17

How do you provide a default implementation in a trait?

A
trait Trait { fn method(&self) { /* default impl */ } }
B
trait Trait { default fn method(&self); }
C
trait Trait { fn method(&self) = default; }
D
Default implementations are not allowed
18

Question 18

Can you override default implementations?

A
Yes, by providing a custom implementation in the impl block
B
No, default implementations cannot be changed
C
Only partially
D
Only in certain contexts
19

Question 19

What are supertraits?

A
Traits that a trait depends on, requiring implementors to also implement the supertraits
B
Traits that inherit from other traits
C
Parent traits in a hierarchy
D
Traits with higher priority
20

Question 20

How do you specify supertraits?

A
trait SubTrait: SuperTrait { ... }
B
trait SubTrait extends SuperTrait { ... }
C
trait SubTrait implements SuperTrait { ... }
D
Supertraits are automatically inferred
21

Question 21

What are orphan rules in trait implementations?

A
Rules that prevent implementing external traits for external types
B
Rules about trait inheritance
C
Rules about trait object creation
D
Rules about default implementations
22

Question 22

When can you implement a trait for a type?

A
When either the trait or the type is defined in your crate
B
Always, for any trait and type
C
Only for traits and types in the standard library
D
Only when both are defined locally
23

Question 23

What are marker traits?

A
Traits with no methods that convey semantic meaning to the compiler
B
Traits that mark code locations
C
Traits used for debugging
D
Traits that implement automatically
24

Question 24

What is the Copy trait?

A
A marker trait that indicates types can be copied by simply copying bits
B
A trait for copying files
C
A trait for copying strings
D
A trait that implements clone automatically
25

Question 25

What is the difference between Copy and Clone?

A
Copy is implicit and cheap, Clone is explicit and may be expensive
B
Clone is implicit, Copy is explicit
C
Copy works for all types, Clone for some
D
There is no difference
26

Question 26

How do you derive common traits automatically?

A
#[derive(Debug, Clone, Copy)] above struct/enum definitions
B
impl Debug for Struct automatically
C
use derive::Debug;
D
Automatic derivation is not possible
27

Question 27

What traits can typically be derived?

A
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash
B
All traits can be derived
C
Only Debug and Display
D
Only standard library traits
28

Question 28

What is trait coherence (also called orphan rules)?

A
Rules ensuring that trait implementations are unique and predictable
B
Rules about trait compilation order
C
Rules about trait performance
D
Rules about trait naming
29

Question 29

What are blanket implementations?

A
Trait implementations that apply to all types meeting certain bounds
B
Implementations that cover multiple traits
C
Implementations that are automatically generated
D
Implementations for built-in types
30

Question 30

What will this blanket implementation do?

rust
trait Display {
    fn display(&self) -> String;
}

impl<T: std::fmt::Display> Display for T {
    fn display(&self) -> String {
        format!("{}", self)
    }
}

fn main() {
    let num = 42;
    println!("{}", num.display());
}
A
Prints '42' using the blanket implementation
B
Compilation error
C
Prints nothing
D
Runtime error
31

Question 31

What are associated constants in traits?

A
Constants that are part of a trait definition
B
Constants that are automatically calculated
C
Constants shared across all implementations
D
Constants that cannot be overridden
32

Question 32

How do you define an associated constant?

A
trait Trait { const CONSTANT: Type = value; }
B
trait Trait { constant CONSTANT: Type; }
C
trait Trait { static CONSTANT: Type; }
D
Associated constants cannot be defined
33

Question 33

What is the Sized trait?

A
A marker trait indicating types with known size at compile time
B
A trait for resizing collections
C
A trait for sized types only
D
A trait that makes types larger
34

Question 34

What does ?Sized mean in trait bounds?

A
Allows both sized and unsized types (like slices)
B
Makes types unsized
C
Requires sized types only
D
Optimizes for sized types
35

Question 35

What are object safety rules for traits?

A
Rules that determine if a trait can be used as a trait object
B
Rules about trait object performance
C
Rules about trait compilation
D
Rules about trait naming
36

Question 36

What makes a trait object-safe?

A
No associated types, all methods have Self: Sized or use &self/&mut self
B
Having default implementations
C
Being implemented by many types
D
Using generics
37

Question 37

What is the Drop trait used for?

A
Custom cleanup logic when values go out of scope
B
Dropping trait objects
C
Dropping references
D
Automatic memory deallocation
38

Question 38

How do you implement the Drop trait?

A
impl Drop for Type { fn drop(&mut self) { ... } }
B
impl Drop for Type { fn cleanup(&mut self) { ... } }
C
Drop implementations are automatic
D
Use the drop keyword
39

Question 39

What is the Deref trait used for?

A
Smart pointer dereferencing with the * operator
B
Dereferencing raw pointers
C
Dereferencing references
D
Automatic dereferencing
40

Question 40

In a scenario where you're designing a drawing API that needs to work with different shapes that can be rendered to different backends, how would you structure the trait hierarchy?

A
Create a Drawable trait with associated types for different backends, and implement it for each shape type
B
Use inheritance with base Shape class
C
Use enums for different shape types
D
Use functions for each shape and backend combination

QUIZZES IN Rust