Sample questions
Question 1Easy
Which of these is a macro call rather than a function call?
- vec![1, 2]
- Vec::new()
- String::from("a")
- [1, 2]
Answer: A. vec![1, 2]
Explanation
A macro call ends its name with !, as in vec![...]. vec! is a macro that builds a Vec from the list you give it. The other options are a function call, an associated function and an array literal.
Step-by-step solution
- Macro calls have a! after the name, such as name!(...) or name![...].
- vec![1, 2] has vec! in that form, so it is a macro call.
- Vec::new() and String::from are functions, and [1, 2] is an array literal.
Why the other options are wrong
- B. Vec::new() is an associated function with no !.
- C. String::from is a function; there is no! in the call.
- D. [1, 2] is an array literal, not a call.
Tip: If the name ends in !, it is a macro.
Question 2Easy
What does the ! in println! indicate?
- It is a macro invocation, not a normal function call
- It negates the output being printed
- It marks the call as an unsafe operation
- It forces a panic whenever an error occurs
Answer: A. It is a macro invocation, not a normal function call
Explanation
Macros are invoked with !. They expand at compile time, which is why println! can take a variable number of arguments.
Step-by-step solution
- println! has a bang.
- Bang marks macros.
- They expand at compile time.
Why the other options are wrong
- B. The! is not boolean negation here.
- C. unsafe is a separate keyword.
- D. Panics are unrelated.
Question 3Easy
What does #[derive(Debug)] add to a struct?
- Makes every field public so the struct can be printed
- An automatic Copy implementation, so every assignment duplicates the value
- A Debug implementation that lets {:?} format the struct
- A runtime check that stops integer overflow in the struct's fields
Answer: C. A Debug implementation that lets {:?} format the struct
Explanation
derive asks the compiler to write the Debug trait for the type. That lets the value be printed with {:?} or {:#?}.
Step-by-step solution
- derive(Debug) asks the compiler to generate an implementation of the Debug trait.
- The generated code describes each field so the value can be printed.
- So println!("{:?}", value) works for that struct.
Why the other options are wrong
- A. derive(Debug) does not change field visibility.
- B. Copy is a separate trait; it needs derive(Copy) or a manual impl.
- D. Overflow checks are compiler behaviour; derive(Debug) adds nothing to the fields.
Tip: derive writes the boilerplate for a trait; Debug is what {:?} uses.
Question 4Medium
Which operation requires an unsafe block?
- Calling a method on a Vec of integers
- Using an ordinary shared reference
- Matching on an enum with a match
- Dereferencing a raw pointer
Answer: D. Dereferencing a raw pointer
Explanation
Raw pointers aren't tracked by the borrow checker, so dereferencing one is unsafe. The programmer must uphold the safety rules.
Step-by-step solution
- Raw pointers skip checks.
- Compiler cannot verify.
- unsafe block is needed.
Why the other options are wrong
- A. Vec methods are safe.
- B. References are checked by the borrow checker.
- C. Pattern matching is safe.
Question 5Medium
What is printed?
macro_rules! double {
($x:expr) => {
$x * 2
};
}
fn main() {
println!("{}", double!(1 + 2));
}
- 6
- 5
- 3
- Compile error
Answer: A. 6
Explanation
An expr fragment keeps (1 + 2) as one unit, so the macro expands to (1 + 2) * 2, which is 6. Plain text substitution would give 1 + 2 * 2, which is 5.
Step-by-step solution
- double!(1 + 2) matches $x:expr with $x = (1 + 2).
- The expansion is $x * 2, and $x stays a single expression.
- So the value is (1 + 2) * 2 = 6.
Why the other options are wrong
- B. That is what plain text substitution would give: 1 + 2 * 2 ignores the grouping.
- C. 3 is only the $x part, without the multiplication.
- D. The macro is valid Rust and compiles.
Tip: Macro fragments of type expr keep their grouping; they are not pasted as raw text.
Question 6Medium
What is printed?
fn main() {
let mut n = 10;
let p = &mut n as *mut i32;
unsafe {
*p += 5;
}
println!("{}", n);
}
- 10
- 5
- Compile error
- 15
Answer: D. 15
Explanation
Casting a mutable reference to a raw pointer is safe. Writing through the raw pointer needs an unsafe block, and that write changes n to 15.
Step-by-step solution
- p is a raw pointer to n, created with a cast.
- *p += 5 writes through the pointer, so n goes from 10 to 15.
- println! then prints 15.
Why the other options are wrong
- A. The write through the pointer changes n, so the old value is not printed.
- B. 5 is the amount added, not the final value of n.
- C. Creating a raw pointer and writing to it inside unsafe compiles.
Tip: A raw pointer points at the same memory, so writing through it changes the original variable.
Question 7Medium
What does panic! do in a single-threaded program by default?
- Returns an Err value to the caller to handle
- Prints a message and unwinds the stack, ending the program
- Ignores the error and continues with the next line
- Retries the last statement until it succeeds
Answer: B. Prints a message and unwinds the stack, ending the program
Explanation
A panic unwinds (or aborts, depending on settings), running destructors on the way, and terminates the thread; in the main thread the program exits with a non-zero code.
Step-by-step solution
- panic! signals an unrecoverable error.
- Stack unwinds.
- Program ends.
Why the other options are wrong
- A. panic! does not return a Result.
- C. Execution does not continue.
- D. There is no retry.
Question 8Medium
Which of these compiles in safe Rust, without an unsafe block?
- Dereferencing a raw pointer with *p
- Calling an extern "C" function such as abs
- Creating a raw pointer with &x as *const i32
- Reading a static mut variable
Answer: C. Creating a raw pointer with &x as *const i32
Explanation
Turning a reference into a raw pointer is safe; the pointer is only a value. Dereferencing a raw pointer, calling a foreign function and reading a static mut all need unsafe.
Step-by-step solution
- Creating a raw pointer only stores an address, so no unsafe is needed.
- Dereferencing a raw pointer may read invalid memory, so it needs unsafe.
- Foreign functions and static mut access are also unsafe operations.
Why the other options are wrong
- A. Dereferencing a raw pointer is one of the operations unsafe is for.
- B. Calling foreign code is unsafe because the compiler cannot check it.
- D. Reading a static mut can race or alias, so it needs unsafe.
Tip: Making a raw pointer is safe; using it (dereferencing) is what needs unsafe.
Practise all 8 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 →