Rust Memory Management Quiz

Rust
0 Passed
0% acceptance

Deep dive into Rust's ownership system, borrowing rules, and advanced memory management patterns for safe and efficient code

40 Questions
~80 minutes
1

Question 1

What are the three ownership rules in Rust?

A
Each value has one owner, ownership can be transferred, when owner goes out of scope value is dropped
B
Values can have multiple owners, borrowing is automatic, garbage collection handles cleanup
C
Ownership is shared, references are weak, manual memory management
D
No ownership rules exist
2

Question 2

What does this ownership example show?

rust
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
A
Compilation error - ownership transferred to s2, s1 invalid
B
Prints 'hello' twice
C
Runtime error
D
s1 and s2 share ownership
3

Question 3

What is the difference between stack and heap allocation?

A
Stack is fast, fixed-size, automatic cleanup; heap is slower, dynamic-size, manual cleanup
B
Stack is slow, heap is fast
C
They are identical
D
Rust doesn't use heap
4

Question 4

What does Copy trait allow?

A
Types that can be duplicated by copying bits, both copies remain valid
B
Types that can be moved only
C
Types that can be cloned only
D
Copy doesn't exist
5

Question 5

What does Clone trait allow?

A
Explicit deep copying of values
B
Automatic copying
C
Moving values
D
Clone doesn't exist
6

Question 6

What are borrowing rules?

A
One mutable reference OR multiple immutable references, references must be valid
B
Multiple mutable references allowed
C
References can be invalid
D
No borrowing rules
7

Question 7

What does this borrowing example show?

rust
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
let r3 = &mut s;
A
Compilation error - cannot borrow mutable when immutable borrows exist
B
Succeeds
C
Runtime error
D
Creates multiple mutable references
8

Question 8

What is a lifetime?

A
Scope for which reference is valid
B
How long a value lives
C
Execution time
D
Lifetimes don't exist
9

Question 9

What does this lifetime example show?

rust
fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str {
    if s1.len() > s2.len() { s1 } else { s2 }
}
A
Function returns reference valid for lifetime 'a
B
Compilation error
C
Returns owned string
D
Lifetime annotation unnecessary
10

Question 10

What is lifetime elision?

A
Compiler automatically infers lifetime annotations
B
Manual lifetime annotation
C
Lifetime removal
D
Lifetime elision doesn't exist
11

Question 11

What is a dangling reference?

A
Reference to data that has been deallocated
B
Valid reference
C
Null pointer
D
Dangling references don't exist in Rust
12

Question 12

What does this dangling reference example show?

rust
fn dangle() -> &String {
    let s = String::from("hello");
    &s
}
A
Compilation error - would return reference to dropped value
B
Succeeds
C
Returns valid reference
D
Runtime error
13

Question 13

What is RAII?

A
Resource Acquisition Is Initialization - resources managed by object lifetime
B
Random Access Integer Array
C
Resource Allocation Interface
D
RAII doesn't exist
14

Question 14

What does Drop trait do?

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

Question 15

What does this custom Drop example show?

rust
struct CustomDrop {
    data: String,
}

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

Question 16

What is the difference between & and &mut?

A
& is immutable borrow, &mut is mutable borrow
B
They are identical
C
&mut allows multiple borrows
D
No difference
17

Question 17

What is interior mutability?

A
Mutating data through immutable reference using types like RefCell
B
Normal mutability
C
Immutable data
D
Interior mutability doesn't exist
18

Question 18

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

A
Cell copies values, RefCell borrows; Cell for Copy types, RefCell for any type
B
They are identical
C
Cell is thread-safe
D
RefCell doesn't exist
19

Question 19

What is a raw pointer?

A
Unchecked pointer (*const T, *mut T) that can be null or dangling
B
Smart pointer
C
Reference
D
Raw pointers don't exist
20

Question 20

What does unsafe keyword allow?

A
Bypassing Rust's safety guarantees for necessary low-level operations
B
Making code safe
C
Automatic safety
D
unsafe doesn't exist
21

Question 21

What is memory layout in Rust?

A
How data is arranged in memory - alignment, padding, size
B
Code organization
C
File structure
D
Memory layout doesn't matter
22

Question 22

What does #[repr(C)] do?

A
Uses C-compatible memory layout for struct fields
B
Makes struct copyable
C
Adds methods
D
#[repr(C)] doesn't exist
23

Question 23

What is ownership transfer (move)?

A
Transferring ownership from one variable to another, invalidating source
B
Copying data
C
Borrowing
D
Ownership transfer doesn't exist
24

Question 24

What is partial move?

A
Moving some fields of struct while leaving others
B
Moving entire struct
C
Copying struct
D
Partial move doesn't exist
25

Question 25

What does this partial move example show?

rust
struct Person {
    name: String,
    age: u32,
}

let p = Person { name: String::from("Alice"), age: 30 };
let name = p.name;
// p.age is still accessible, p.name is moved
A
Moves name field, age remains accessible
B
Compilation error
C
Moves entire struct
D
Runtime error
26

Question 26

What is the difference between String and &str?

A
String owns heap data, &str borrows existing string slice
B
They are identical
C
&str owns data
D
String doesn't exist
27

Question 27

What is the difference between Vec<T> and &[T]?

A
Vec owns heap data, &[T] borrows existing slice
B
They are identical
C
&[T] owns data
D
Vec doesn't exist
28

Question 28

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
29

Question 29

What does this deref coercion example show?

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

Question 30

What is the difference between Sized and ?Sized?

A
Sized has compile-time known size, ?Sized may have dynamic size
B
They are identical
C
?Sized is smaller
D
?Sized doesn't exist
31

Question 31

What is DST (Dynamically Sized Type)?

A
Types with unknown size at compile time like [T] and str
B
Fixed-size types
C
Generic types
D
DST doesn't exist
32

Question 32

What is memory alignment?

A
Address requirement for data types (power of 2)
B
Memory size
C
Memory location
D
Memory alignment doesn't matter
33

Question 33

What is padding in structs?

A
Unused bytes inserted for alignment requirements
B
Data bytes
C
Code bytes
D
Padding doesn't exist
34

Question 34

What is the difference between fat and thin pointers?

A
Thin pointers are single usize, fat pointers contain address + metadata
B
They are identical
C
Fat pointers are smaller
D
No difference
35

Question 35

What is stack allocation vs heap allocation performance?

A
Stack is faster (no allocation overhead), heap slower but flexible
B
Heap is faster
C
They are identical
D
Performance doesn't matter
36

Question 36

What is the borrow checker?

A
Compile-time analysis ensuring borrowing rules
B
Runtime checking
C
Manual checking
D
Borrow checker doesn't exist
37

Question 37

What is NLL (Non-Lexical Lifetimes)?

A
Lifetimes end when variable last used, not scope end
B
Lexical lifetimes
C
Manual lifetime management
D
NLL doesn't exist
38

Question 38

What is the difference between lifetime bounds and trait bounds?

A
Lifetime bounds constrain reference validity, trait bounds constrain behavior
B
They are identical
C
Lifetime bounds are stricter
D
No difference
39

Question 39

What is memory safety?

A
Prevention of undefined behavior from memory operations
B
Fast memory access
C
Large memory usage
D
Memory safety doesn't matter
40

Question 40

In a complex data structure with trees of objects that reference each other, shared configuration data accessed by multiple threads, and temporary buffers with varying lifetimes, which memory management patterns would you choose and why?

A
Rc<RefCell<Node>> for tree with cycles, Arc<Config> for shared config, scoped allocation for buffers
B
Raw pointers everywhere
C
Global variables
D
Manual malloc/free

QUIZZES IN Rust