Sample questions
Question 1Easy
- Storing a value on the heap with a single owner
- Sharing ownership among many owners
- Making any value Copy so it can be duplicated freely
- Thread-safe shared state
Answer: A. Storing a value on the heap with a single owner
Explanation
Box gives heap allocation and a fixed-size pointer; useful for recursive types and trait objects.
Step-by-step solution
- Box<T> allocates T on the heap.
- The Box owns it, so there is a single owner.
- The heap value is freed when the Box is dropped.
Why the other options are wrong
- B. Shared ownership is Rc<T> or Arc<T>.
- C. Box does not make anything Copy.
- D. Thread-safe sharing needs Arc plus Mutex or similar.
Question 2Easy
What does this print?
use std::rc::Rc;
let a = Rc::new(5);
let b = Rc::clone(&a);
println!("{}", Rc::strong_count(&a));
- 1
- 2
- 0
- 5
Answer: B. 2
Explanation
Rc::clone copies the pointer and increments the strong count, so two owners exist.
Step-by-step solution
- Rc::new gives count 1.
- Rc::clone adds one.
- Count is 2.
Why the other options are wrong
- A. Cloning the Rc raises the count to 2.
- C. The count never drops to 0 while a lives.
- D. 5 is the stored value.
Question 3Easy
What does the Deref trait let smart pointers do?
- Copy values
- Behave like references, so *b and method calls work
- Implement Display automatically for the pointed-to value
- Free memory
Answer: B. Behave like references, so *b and method calls work
Explanation
Deref coercion lets &Box<String> be used where &str is expected.
Step-by-step solution
- The Deref trait lets a smart pointer be used like a reference.
- So *b reads the inner value and method calls reach the inner type.
- So option B is the correct answer.
Why the other options are wrong
- A. Deref does not copy values.
- C. Display is a separate trait, and Deref does not give it.
- D. Freeing memory is the job of Drop, not Deref.
Tip: Deref coercion lets a Box of String be passed where a str reference is expected.
Question 4Easy
What does Box::new(5) do?
- Creates a reference-counted pointer shared by many owners
- Allocates the value 5 on the heap and returns an owning pointer
- Creates a pointer that can be shared safely across threads
- Stores 5 in a static variable
Answer: B. Allocates the value 5 on the heap and returns an owning pointer
Explanation
Box<T> owns a heap allocation and frees it when dropped. It has a single owner.
Step-by-step solution
- Box = single-owner heap pointer.
- The 5 goes to the heap.
- Dropping the Box frees it.
Why the other options are wrong
- A. That is Rc.
- C. That is Arc.
- D. Box uses the heap, not statics.
Question 5Medium
- Heap allocation with exactly one owner and no counting overhead
- Mutable access to shared data
- Shared ownership via reference counting, single-threaded
- Thread-safe shared ownership
Answer: C. Shared ownership via reference counting, single-threaded
Explanation
Rc::clone increments the count; the value drops when the last Rc goes.
Step-by-step solution
- Rc stands for reference counted.
- It lets several owners share a value, and it counts them.
- Rc is for single-threaded code, so option C is the correct answer.
Why the other options are wrong
- A. Rc allows many owners, not exactly one.
- B. Rc gives shared access, not mutable access on its own.
- D. Rc is not thread-safe; Arc is the thread-safe version.
Tip: Use Rc for shared ownership in one thread, and Arc across threads.
Question 6Medium
What is Weak<T> used for?
- A thread-safe pointer that locks on every access
- A mutable reference
- A non-owning reference to Rc data that does not keep it alive
- A faster Box
Answer: C. A non-owning reference to Rc data that does not keep it alive
Explanation
upgrade() returns Option<Rc<T>>, which is None if the value has been dropped.
Step-by-step solution
- Weak is a handle that does not count as an owner.
- Upgrade returns None if the value has already been dropped.
- So option C, a non-owning reference, is the correct answer.
Why the other options are wrong
- A. Weak does not lock; a Mutex does the locking.
- B. Weak does not give mutable access.
- D. Weak is not a faster Box; it is a non-owning handle.
Tip: Use Weak to refer back to an Rc value without keeping it alive.
Question 7Medium
Why can Rc<T> not be used to share data across threads?
- It cannot hold structs or any user-defined type
- It always leaks memory when cloned
- Its reference count is not atomic, so Rc is not Send or Sync
- It forbids shared access
Answer: C. Its reference count is not atomic, so Rc is not Send or Sync
Explanation
Rc updates its count non-atomically, which would race between threads. Use Arc, which uses atomic operations.
Step-by-step solution
- Counts must be exact.
- Rc counts non-atomically.
- Compiler blocks it; use Arc.
Why the other options are wrong
- A. Rc holds any type.
- B. Leaks need cycles, not threads.
- D. Rc is shared ownership by design.
Question 8Medium
Which combination is typical for shared, mutable data within one thread?
- Rc<RefCell<T>>
- &'static mut T, which is the idiomatic safe choice
- Arc<T> alone
- Box<Box<T>>
Answer: A. Rc<RefCell<T>>
Explanation
Rc gives shared ownership; RefCell allows mutation through it.
Step-by-step solution
- Rc gives shared ownership to several handles.
- RefCell allows mutation through a shared handle, checked at runtime.
- So Rc with RefCell inside, option A, is the usual pattern within one thread.
Why the other options are wrong
- B. A static mutable reference is not a shared pattern and is not the idiomatic choice.
- C. Arc alone shares data across threads but gives no mutation.
- D. Box of Box adds heap layers; it does not share or mutate data.
Tip: Rc with RefCell for one thread; Arc with Mutex across threads.
Question 9Hard
Calling borrow_mut() on a RefCell while a borrow() is still active results in:
- A compile error
- A deadlock that blocks the thread forever
- Silent data corruption
- A runtime panic (already borrowed)
Answer: D. A runtime panic (already borrowed)
Explanation
RefCell enforces the same rules as the compiler, but at runtime.
Step-by-step solution
- Borrow returns a shared borrow, and it is still active.
- Borrow_mut asks for an exclusive borrow at the same time.
- RefCell detects the conflict at runtime and panics, so option D is the correct answer.
Why the other options are wrong
- A. The conflict is only found at runtime, so the compiler does not reject it.
- B. RefCell does not use locks, so the thread does not deadlock.
- C. RefCell stops the conflict with a panic; it does not corrupt data.
Tip: Keep RefCell borrows short so that a borrow_mut does not overlap a borrow.
Question 10Hard
What does RefCell<T> provide?
- Reference counting for shared ownership across threads, using atomic operations on the count
- Interior mutability with borrow rules checked at runtime
- Compile-time borrow checking only
- Thread-safe locking
Answer: B. Interior mutability with borrow rules checked at runtime
Explanation
borrow() and borrow_mut() track borrows at runtime and panic on a violation.
Step-by-step solution
- RefCell lets you change data through a shared reference.
- It checks the borrow rules at runtime, not at compile time.
- So option B is the correct answer.
Why the other options are wrong
- A. Reference counting across threads is Arc; RefCell does not count references.
- C. RefCell checks at runtime, not only at compile time.
- D. RefCell is not a lock and is not thread-safe; Mutex does that.
Tip: RefCell moves borrow checks to runtime; a bad borrow panics instead of failing to compile.
Practise all 15 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 →