Sample questions
Question 1Easy
Which trait controls how a type is printed with {} in println!?
- Debug
- Display
- Default
- ToOwned
Answer: B. Display
Explanation
Debug is used by {:?}; Display must be implemented by hand for {}.
Step-by-step solution
- The {} placeholder in println uses the Display trait.
- The {:?} placeholder uses the Debug trait.
- So option B, Display, is the correct answer.
Why the other options are wrong
- A. Debug controls {:?}, not {}.
- C. Default gives a default value, not text.
- D. ToOwned converts a borrowed value into an owned one.
Tip: Implement Display by hand for {}; derive Debug for {:?}.
Question 2Easy
Which trait lets a type be compared with ==?
- Clone
- PartialEq
- Iterator
- Display
Answer: B. PartialEq
Explanation
#[derive(PartialEq)] generates field-by-field equality.
Step-by-step solution
- The == operator uses the PartialEq trait.
- The derive(PartialEq) attribute generates equality comparing each field.
- So option B is the correct answer.
Why the other options are wrong
- A. Clone makes a copy of a value; it does not compare values.
- C. Iterator is for stepping through items, not comparing.
- D. Display is for printing, not for ==.
Tip: Derive PartialEq (and Eq when it applies) to compare your types with ==.
Question 3Easy
- A set of methods a type can implement, like an interface
- A macro that copies code from one type into another at compile time
- A memory allocator
- A struct field
Answer: A. A set of methods a type can implement, like an interface
Explanation
Traits define shared behaviour; types opt in with impl Trait for Type.
Step-by-step solution
- A trait lists methods that types can implement.
- A type opts in with impl Trait for Type, much like an interface.
- So option A is the correct answer.
Why the other options are wrong
- B. Copying code between types is not what a trait does; that is not a macro concept.
- C. A trait is not a memory allocator.
- D. A struct field stores data; a trait defines behaviour.
Tip: Think of a trait as a named set of behaviour that types promise to provide.
Question 4Easy
What does #[derive(Debug)] provide?
- Runtime logging of every function call
- An automatic Debug implementation, enabling {:?} formatting
- A debugger
- A Display implementation that prints the struct with {} formatting
Answer: B. An automatic Debug implementation, enabling {:?} formatting
Explanation
Derive macros generate standard trait impls such as Debug, Clone, PartialEq.
Step-by-step solution
- The derive(Debug) attribute asks the compiler to generate a Debug implementation.
- That implementation lets the type be printed with {:?}.
- So option B is the correct answer.
Why the other options are wrong
- A. Derive Debug does not log function calls at runtime.
- C. A debugger is a separate tool, not a derive.
- D. Display is a different trait, and Debug does not give it.
Tip: Derive Debug on most structs so they can be printed while developing.
Question 5Medium
What are default methods in a trait?
- Methods that cannot be overridden
- Methods every type gets automatically
- Method bodies in the trait that implementors can use or override
- Static methods that are called once when the program starts
Answer: C. Method bodies in the trait that implementors can use or override
Explanation
Implementors only need to provide the required methods.
Step-by-step solution
- A trait can give a method a default body.
- Implementors use that body or replace it with their own.
- So option C is the correct answer.
Why the other options are wrong
- A. Default methods can be overridden by implementors.
- B. A default method is only given to types that implement the trait.
- D. Default methods are ordinary methods, not one-time static code.
Tip: Implementors need only write the required methods; the rest can use defaults.
Question 6Medium
What does returning impl Iterator<Item = u32> from a function mean?
- It returns a boxed trait object
- It returns a Vec
- It returns some concrete iterator type without naming it
- It returns a different iterator type on each call, chosen at runtime
Answer: C. It returns some concrete iterator type without naming it
Explanation
The concrete type is hidden but fixed, keeping static dispatch.
Step-by-step solution
- Impl Iterator Item = u32 as a return type hides the concrete iterator type.
- The type is still one fixed type chosen by the function body.
- So option C is the correct answer.
Why the other options are wrong
- A. This is dyn Iterator in a Box, not impl Trait.
- B. The function returns an iterator, not a Vec.
- D. The concrete type is fixed at compile time, not chosen at runtime.
Tip: Use impl Trait to return an iterator without naming its type.
Question 7Medium
What does fn print_all<T: Display>(items: &[T]) mean?
- items must be strings
- T must be exactly the Display type
- A generic function for any T that implements Display
- The function is only compiled once and uses dynamic dispatch for every call
Answer: C. A generic function for any T that implements Display
Explanation
The trait bound restricts T to types that can be formatted with {}.
Step-by-step solution
- T is a type parameter, and Display is a trait bound on it.
- The bound means any type T that implements Display can be used.
- So option C is the correct answer.
Why the other options are wrong
- A. The items do not have to be strings; any Display type works.
- B. T is not fixed to one type; it can be any type that implements Display.
- D. Generics are compiled for each concrete type, not dispatched dynamically.
Tip: A trait bound restricts a generic to types with that behaviour.
Question 8Medium
Why is Box<dyn Shape> used instead of dyn Shape when storing mixed shapes in a Vec?
- dyn Shape is unsized, so it needs a pointer
- Box makes the method calls faster
- dyn types cannot have methods
- Vec only stores heap types
Answer: A. dyn Shape is unsized, so it needs a pointer
Explanation
Different implementors have different sizes, so a trait object must be behind a pointer such as Box or &.
Step-by-step solution
- Implementors differ in size.
- Vec needs sized elements.
- Box provides a fixed-size pointer.
Why the other options are wrong
- B. dyn calls go through a vtable, which is slower, not faster.
- C. Trait objects do have methods.
- D. Vec can store any sized type.
Question 9Hard
What is an associated type, such as type Item in Iterator?
- A type placeholder each implementation fixes once
- A type alias for i32
- A generic parameter chosen by the caller each time
- A field that every struct implementing the trait must contain
Answer: A. A type placeholder each implementation fixes once
Explanation
Each Iterator implementation names one Item type, avoiding repeated generic parameters.
Step-by-step solution
- An associated type, such as type Item in Iterator, is a placeholder type.
- Each implementation of the trait sets it once.
- So option A is the correct answer.
Why the other options are wrong
- B. An associated type is chosen by each implementation, not fixed to i32.
- C. The caller does not choose it each time; the implementation does.
- D. It is a type in the trait, not a struct field.
Tip: Use an associated type when each implementor should fix exactly one type.
Question 10Hard
dyn Trait (e.g. Box<dyn Shape>) vs generics:
- dyn Trait is resolved at compile time
- They are identical, except that dyn Trait can only be used with structs and never with enums
- Generics need a Box
- dyn Trait uses dynamic dispatch through a vtable; generics use static dispatch
Answer: D. dyn Trait uses dynamic dispatch through a vtable; generics use static dispatch
Explanation
Trait objects allow mixed types in one collection at a small runtime cost.
Step-by-step solution
- dyn Trait uses a vtable at runtime.
- Generics are monomorphised at compile time.
- So dyn is dynamic dispatch, generics static.
Why the other options are wrong
- A. dyn is resolved at runtime.
- B. dyn Trait works for any type implementing the trait.
- C. Generics do not need Box.
Practise all 18 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →