Rust Smart Pointers Quiz

Rust
0 Passed
0% acceptance

Master Rust's smart pointer types for automatic memory management and safe heap allocation

40 Questions
~80 minutes
1

Question 1

What is a smart pointer in Rust?

A
A pointer type that implements Deref and Drop traits for automatic resource management
B
A raw pointer with manual memory management
C
A function pointer
D
Smart pointers don't exist in Rust
2

Question 2

What does Box<T> do?

A
Allocates value on heap and provides ownership
B
Shares ownership between multiple pointers
C
Provides interior mutability
D
Box doesn't exist
3

Question 3

What does this Box example do?

rust
let b = Box::new(5);
println!("b = {}", b);
A
Allocates i32 on heap, automatically dereferences for printing
B
Compilation error
C
Allocates on stack
D
Runtime error
4

Question 4

When would you use Box<T>?

A
For large values, recursive data structures, or trait objects
B
For shared ownership
C
For interior mutability
D
Never, it's obsolete
5

Question 5

What is Rc<T>?

A
Reference counted smart pointer for single-threaded shared ownership
B
Atomic reference counting for threads
C
Box with reference counting
D
Rc doesn't exist
6

Question 6

What does this Rc example show?

rust
use std::rc::Rc;
let a = Rc::new(vec![1, 2, 3]);
let b = Rc::clone(&a);
println!("Reference count: {}", Rc::strong_count(&a));
A
Creates shared ownership, reference count is 2
B
Compilation error
C
Reference count is 1
D
Runtime error
7

Question 7

What is the difference between Rc::clone() and regular clone()?

A
Rc::clone() increments reference count, doesn't clone data
B
They are identical
C
Rc::clone() clones the data
D
Rc::clone() doesn't exist
8

Question 8

What is Arc<T>?

A
Atomic reference counting for thread-safe shared ownership
B
Single-threaded reference counting
C
Box with atomic operations
D
Arc doesn't exist
9

Question 9

When would you choose Arc<T> over Rc<T>?

A
When data needs to be shared across threads
B
For single-threaded code
C
When performance is critical
D
Never, Rc is better
10

Question 10

What is RefCell<T>?

A
Provides interior mutability with runtime borrow checking
B
Provides compile-time borrow checking
C
Box with mutability
D
RefCell doesn't exist
11

Question 11

What does this RefCell example do?

rust
use std::cell::RefCell;
let c = RefCell::new(5);
*c.borrow_mut() = 10;
println!("Value: {}", *c.borrow());
A
Mutates value through shared reference using runtime checking
B
Compilation error
C
Compile-time borrow error
D
Runtime panic
12

Question 12

What happens if you violate borrowing rules with RefCell?

A
Runtime panic occurs
B
Compilation error
C
Silent data corruption
D
Nothing happens
13

Question 13

What is Weak<T>?

A
Weak reference that doesn't prevent deallocation
B
Strong reference
C
Mutable reference
D
Weak doesn't exist
14

Question 14

How do you create a Weak<T> from Rc<T>?

A
Use Rc::downgrade(&rc) to create Weak pointer
B
Use Rc::clone() with weak flag
C
Cast Rc to Weak
D
Cannot create Weak from Rc
15

Question 15

What does this Weak example show?

rust
use std::rc::{Rc, Weak};
let strong = Rc::new(5);
let weak = Rc::downgrade(&strong);
if let Some(value) = weak.upgrade() {
    println!("Value: {}", value);
}
A
Creates weak reference, upgrades to access value if it still exists
B
Compilation error
C
Always panics
D
Runtime error
16

Question 16

What is a reference cycle?

A
When Rc pointers reference each other, preventing deallocation
B
When Box pointers cycle
C
When Weak pointers cycle
D
Reference cycles don't exist
17

Question 17

How do you break reference cycles?

A
Use Weak<T> for one direction of the reference
B
Use Box<T>
C
Use RefCell<T>
D
Reference cycles cannot be broken
18

Question 18

What does Deref trait provide?

A
Automatic dereferencing with * and . operators
B
Manual dereferencing
C
Reference counting
D
Deref doesn't exist
19

Question 19

What does Drop trait do?

A
Defines cleanup logic when value goes out of scope
B
Defines how to drop references
C
Defines dereferencing
D
Drop doesn't exist
20

Question 20

What does this custom Drop example do?

rust
struct CustomSmartPointer {
    data: String,
}

impl Drop for CustomSmartPointer {
    fn drop(&mut self) {
        println!("Dropping with data: {}", self.data);
    }
}
A
Prints message when CustomSmartPointer is deallocated
B
Compilation error
C
Prints immediately
D
Runtime error
21

Question 21

What is the performance cost of Rc<T>?

A
Reference count updates on clone/drop
B
Atomic operations
C
Heap allocation
D
No performance cost
22

Question 22

What is the performance cost of Arc<T>?

A
Atomic reference count updates, more expensive than Rc
B
Same as Rc
C
No atomic operations
D
Faster than Rc
23

Question 23

What is the performance cost of RefCell<T>?

A
Runtime borrow checking overhead
B
Compile-time checking
C
No overhead
D
Significant allocation cost
24

Question 24

What does Rc<RefCell<T>> allow?

A
Shared mutable data in single-threaded code
B
Thread-safe shared data
C
Immutable shared data
D
This combination doesn't work
25

Question 25

What does Arc<Mutex<T>> allow?

A
Thread-safe shared mutable data
B
Single-threaded shared data
C
Immutable shared data
D
This combination doesn't work
26

Question 26

What is interior mutability?

A
Mutating data through shared references
B
Mutating data through mutable references
C
Mutating data directly
D
Interior mutability doesn't exist
27

Question 27

What is the difference between Cell<T> and RefCell<T>?

A
Cell works with Copy types, RefCell works with any type
B
Cell is thread-safe, RefCell is not
C
They are identical
D
Cell doesn't exist
28

Question 28

What does this Cell example show?

rust
use std::cell::Cell;
let c = Cell::new(5);
c.set(10);
println!("Value: {}", c.get());
A
Interior mutability for Copy types without borrowing
B
Compilation error
C
Runtime borrow error
D
Runtime error
29

Question 29

What is a common use case for Box<T>?

A
Recursive data structures like trees or linked lists
B
Shared ownership
C
Thread-safe sharing
D
Interior mutability
30

Question 30

What is a common use case for Rc<T>?

A
Graph data structures with shared nodes
B
Thread-safe data sharing
C
Single ownership
D
Heap allocation only
31

Question 31

What is a common use case for Arc<T>?

A
Shared data accessed by multiple threads
B
Single-threaded sharing
C
Recursive structures
D
Interior mutability
32

Question 32

What is a common use case for RefCell<T>?

A
Mock objects or caches that mutate through shared references
B
Thread-safe mutation
C
Shared ownership
D
Heap allocation
33

Question 33

What is a common use case for Weak<T>?

A
Parent-child relationships where child references parent
B
Strong ownership
C
Thread-safe sharing
D
Interior mutability
34

Question 34

What happens when Rc strong count reaches zero?

A
Data is deallocated, Weak references become invalid
B
Data stays allocated
C
Program panics
D
Nothing happens
35

Question 35

What happens when Arc strong count reaches zero?

A
Same as Rc - data is deallocated
B
Data stays allocated due to atomic operations
C
Different from Rc
D
Program crashes
36

Question 36

Can you implement Deref for your own types?

A
Yes, to make your type behave like a smart pointer
B
No, only built-in types
C
Only for Box
D
Deref cannot be implemented
37

Question 37

What is Deref coercion?

A
Automatic conversion from &T to &U where T: Deref<Target = U>
B
Manual dereferencing
C
Type casting
D
Deref coercion doesn't exist
38

Question 38

What does this Deref coercion example show?

rust
fn takes_str(s: &str) { println!("{}", s); }
let s = Box::new(String::from("hello"));
takes_str(&s);
A
Deref coercion converts &Box<String> to &String to &str
B
Compilation error
C
Manual conversion needed
D
Runtime error
39

Question 39

What is the memory layout of Box<T>?

A
Small pointer on stack, data on heap
B
Data directly on stack
C
Data and pointer on heap
D
Box has no memory layout
40

Question 40

In a complex application with a tree structure where nodes need to reference their parent, shared configuration across threads, and mutable cached data, which smart pointer combination would you choose and why?

A
Weak<Rc<Node>> for parent refs, Arc<Config> for shared config, Rc<RefCell<Cache>> for mutable cache
B
Box for everything to keep it simple
C
Raw pointers for maximum performance
D
Avoid smart pointers and use global variables

QUIZZES IN Rust