Sample questions
Question 1Easy
What happens when this code is compiled?
let a = String::from("hi");
let b = a;
println!("{}", a);
- Compile error:
a was moved to b and used afterwards - It prints hi
- It prints hi twice
- It compiles but panics at run time
Answer: A. Compile error: a was moved to b and used afterwards
Explanation
Assigning a String moves ownership, so a can no longer be used. The borrow checker reports this at compile time.
Step-by-step solution
let b = a moves the heap String.a is now invalid.println! uses a, so compilation fails.
Why the other options are wrong
- B. Ownership of the String moved to
b, so a is unusable. - C. Printing
a is rejected before anything runs. - D. Nothing is printed because
a is never valid again.
Question 2Easy
Which is one of Rust's ownership rules?
- Only heap values have owners; stack values are shared freely by reference counting
- Each value has exactly one owner at a time
- Values are freed by a garbage collector when unreachable
- Every value may have any number of owners that all free it
Answer: B. Each value has exactly one owner at a time
Explanation
One owner per value; when the owner goes out of scope, the value is dropped.
Step-by-step solution
- Rust's ownership rules say each value has exactly one owner at a time.
- When that owner goes out of scope, the value is dropped.
- So option B is the correct answer.
Why the other options are wrong
- A. Reference counting is an opt-in type such as Rc, not a rule for stack values.
- C. Rust has no garbage collector.
- D. Values do not have many owners that each free them.
Tip: One owner per value; move it or borrow it, and it is dropped once.
Question 3Easy
Why does this fail to compile?
let s1 = String::from("hi");
let s2 = s1;
println!("{}", s1);
- s1 was moved into s2, so s1 can no longer be used
- Strings are immutable, so assigning one to another is not allowed
- String cannot be printed with {}
- s2 is never used
Answer: A. s1 was moved into s2, so s1 can no longer be used
Explanation
Assigning a String moves ownership; using s1 afterwards is 'borrow of moved value'.
Step-by-step solution
- let s2 = s1 moves the String.
- s1 is no longer valid.
- Printing s1 uses a moved value, so compile error.
Why the other options are wrong
- B. Strings can be assigned; it moves.
- C. String implements Display.
- D. Unused variables only give warnings.
Question 4Easy
Which keyword makes a variable binding mutable in Rust?
- let!
- mut
- mutable
- var
Answer: B. mut
Explanation
let mut count = 0; allows reassignment and mutation.
Step-by-step solution
- Bindings are immutable by default.
mut makes them mutable.``rust
let mut x = 5;
``
Why the other options are wrong
- A. let! is not valid Rust.
- C. mutable is not a keyword.
- D. var is not a Rust keyword.
Question 5Medium
Passing a String to a function fn take(s: String) means:
- The String is copied byte by byte
- The caller keeps ownership, and the function gets a temporary copy that is freed on return
- Ownership moves into the function; the caller cannot use it afterwards
- The function receives a shared reference
Answer: C. Ownership moves into the function; the caller cannot use it afterwards
Explanation
Taking ownership by value moves it; pass &String or &str to only borrow.
Step-by-step solution
- Fn take(s: String) takes the String by value.
- Passing the String moves ownership into the function.
- The caller cannot use s afterwards, so option C is the correct answer.
Why the other options are wrong
- A. No byte-by-byte copy happens; ownership moves.
- B. Passing by value gives ownership away; the caller does not keep it.
- D. A shared reference is written as &String or &str, not String.
Tip: Pass &String or &str to only borrow a value; pass String to give it away.
Question 6Medium
What does this print?
fn take(s: String) {}
fn main() {
let s = String::from("ok");
take(s.clone());
println!("{}", s);
}
- Nothing, it fails to compile
- ok
- ok ok
- It panics
Answer: B. ok
Explanation
clone() makes an independent copy that is moved into take. The original s stays valid and prints ok.
Step-by-step solution
s.clone() creates a new String.- The clone moves into
take and is dropped. s is untouched and prints.
Why the other options are wrong
- A. Only the clone was moved into
take; s is still owned by main. - C. The clone is dropped inside
take, nothing is printed twice. - D. There is no runtime failure here.
Question 7Medium
How can a function give ownership of a value back to its caller?
- By marking the parameter mut
- By returning the value
- By calling drop on it before returning so the caller can recreate it
- By using a static variable
Answer: B. By returning the value
Explanation
Returning moves ownership out of the function to the caller.
Step-by-step solution
- A function that receives an owned value owns it.
- It hands ownership back by returning the value.
- So returning the value, option B, is the correct answer.
Why the other options are wrong
- A. Mut only allows changes to the parameter; it does not return ownership.
- C. Drop destroys the value, so the caller cannot get it back this way.
- D. A static variable is not a way to pass ownership back.
Tip: Return the owned value from a function to hand ownership back to the caller.
Question 8Medium
Which statement about the Drop trait is correct?
- It runs custom cleanup when a value goes out of scope
- You must call
drop() manually on every value - It is only for heap memory
- It runs when the garbage collector decides, so the timing cannot be predicted
Answer: A. It runs custom cleanup when a value goes out of scope
Explanation
Implement Drop for custom cleanup; std::mem::drop(x) just moves x into a function so it is dropped early.
Step-by-step solution
- The Drop trait lets a type define its own cleanup code.
- Rust runs that code when the value goes out of scope.
- So option A is the correct answer.
Why the other options are wrong
- B. Drop runs automatically; you do not call drop on every value.
- C. Drop is for any resource, not only heap memory.
- D. Rust has no garbage collector, so the timing is deterministic.
Tip: Implement Drop for custom cleanup; std::mem::drop moves a value in to drop it early.
Question 9Hard
Why is let s2 = s1.clone(); different from let s2 = s1; for a String?
- They are identical
- clone is a shallow copy that shares the heap buffer
- Assignment copies the data and clone moves it, the opposite of most languages
- clone makes a deep copy, so both stay valid; assignment moves
Answer: D. clone makes a deep copy, so both stay valid; assignment moves
Explanation
clone explicitly duplicates the heap data; a plain assignment transfers ownership without copying.
Step-by-step solution
- let s2 = s1 moves ownership; s1 becomes invalid.
- clone() copies the heap data so both are valid.
- So clone is a deep copy and assignment is a move.
Why the other options are wrong
- A. They behave differently.
- B. clone is deep, not shared.
- C. It is reversed: assignment moves, clone copies.
Question 10Hard
What does Rust's ownership system prevent at compile time?
- Deadlocks between threads that lock mutexes in different orders
- Integer overflow in every build mode
- All logic errors
- Double frees and use-after-free
Answer: D. Double frees and use-after-free
Explanation
Single ownership ensures exactly one drop; borrowing rules forbid references that outlive their data. Deadlocks are still possible.
Step-by-step solution
- Ownership rules allow exactly one drop per value.
- Borrowing rules stop references from outliving their data.
- So double frees and use-after-free are prevented at compile time; option D.
Why the other options are wrong
- A. Deadlocks are runtime locking problems that ownership does not prevent.
- B. Integer overflow is not prevented by ownership rules.
- C. Ownership does not catch all logic errors.
Tip: Ownership gives memory safety; it does not prevent every bug.
Practise all 16 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 →