




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which keyword makes a variable mutable in Rust? A) const B) let mut C) var D) mutable Answer: B Explanation: In Rust, variables are immutable by default; adding mut after let (let mut) makes the binding mutable.
Question 2. What is the default integer type inferred by the compiler when you write let x = 42;? A) i B) u C) i D) isize Answer: C Explanation: When the compiler needs to infer an integer type and no suffix is provided, it defaults to i32.
Question 3. Which of the following is a compile‑time constant that can be used in any context, including pattern matches?
A) let B) static C) const D) mut Answer: C Explanation: const defines a constant value evaluated at compile time and can be used wherever a constant expression is required.
Question 4. Which of the following statements about the if let construct is true? A) It can only be used with Option types. B) It creates a new scope for the matched variable. C) It is equivalent to a match with a single arm and a wildcard. D) It always requires an else block. Answer: C Explanation: if let is syntactic sugar for a match that has one pattern arm and a wildcard _ arm that does nothing.
Question 5. In Rust, which loop construct guarantees that the body will execute at least once? A) while B) loop C) for
Answer: D Explanation: All three forms are syntactically correct; the first declares the signature, the second is a stub, and the third provides an implementation.
Question 8. In Rust, which of the following is an expression, not a statement? A) let x = 5; B) x + 1 C) fn foo() {} D) while true {} Answer: B Explanation: x + 1 evaluates to a value and is therefore an expression; the others are statements.
Question 9. Which tuple syntax creates a tuple containing a single i32 value? A) (5) B) (5,) C) [5] D) {5} Answer: B Explanation: A trailing comma distinguishes a single‑element tuple; (5,) is a tuple, while (5) is just the value 5 in parentheses.
Question 10. What is the size of an array [u8; 4] stored on the stack? A) 1 byte B) 4 bytes C) 8 bytes D) Size is determined at runtime Answer: B Explanation: The array holds four u8 values, each 1 byte, so the total size is 4 bytes and is known at compile time.
Question 11. Which method moves the ownership of a String into a Vec<String> without cloning? A) push(&s) B) push(s.clone()) C) push(s) D) push(&mut s) Answer: C Explanation: push(s) takes ownership of s and moves it into the vector; no cloning occurs.
C) Ownership can be shared simultaneously by multiple mutable references. D) Moving a value transfers its ownership to the new binding. Answer: C Explanation: Rust forbids having more than one mutable reference at a time; mutable borrowing is exclusive.
Question 15. What happens when you assign a String to another variable without using clone()? A) The data is copied byte‑by‑byte. B) The original variable becomes invalid because ownership is moved. C) Both variables point to the same heap allocation. D) The compiler raises an error. Answer: B Explanation: Assignment of a non‑Copy type like String moves ownership, rendering the original variable unusable.
Question 16. Which trait must a type implement to be implicitly copied by simple assignment? A) Clone B) Copy C) Drop D) Default
Answer: B Explanation: Types that implement the Copy trait are duplicated on assignment; Clone requires an explicit call.
Question 17. Which of the following types implements the Copy trait by default? A) String B) Vec<T> C) bool D) Box<T> Answer: C Explanation: Primitive scalar types like bool, integers, and floating‑point numbers implement Copy. Heap‑allocated types do not.
Question 18. What does the Drop trait allow you to do? A) Clone a value. B. Transfer ownership. C. Run custom code when a value goes out of scope. D. Prevent a value from being moved. Answer: C
Question 21. Which compile‑time error is produced when you try to create two mutable references to the same variable in the same scope? A) “cannot move out of borrowed content” B) “cannot borrow x as mutable more than once at a time” C) “type mismatch” D) “cannot assign to immutable variable” Answer: B Explanation: The borrow checker reports that a value cannot be borrowed as mutable more than once simultaneously.
Question 22. What does the 'static lifetime indicate about a reference? A) It lives for the duration of the current function. B) It lives for the entire duration of the program. C) It lives until the next await. D) It lives only inside a closure. Answer: B Explanation: 'static denotes that the referenced data is valid for the whole program execution, such as string literals.
Question 23. When is an explicit lifetime annotation required in a function signature? A) When the function returns a primitive type. B) When the compiler cannot infer the relationship between input and output lifetimes. C) When the function has no parameters. D) When the function uses unsafe. Answer: B Explanation: If the compiler cannot deduce how the lifetimes of arguments relate to the returned reference, you must annotate them explicitly.
Question 24. Which of the following function signatures correctly expresses that the returned &str lives at least as long as the input s? A) fn get_slice(s: &str) - > &str B) fn get_slice<'a>(s: &'a str) - > &'a str C) fn get_slice(s: &'static str) - > &'static str D) fn get_slice<'a>(s: &str) - > &'a str Answer: B Explanation: The explicit lifetime 'a ties the input and output lifetimes together, ensuring the returned slice does not outlive s.
Question 25. Which of the following struct definitions includes a lifetime parameter?
D) fn consume(self: &Self) {} Answer: A Explanation: Declaring the parameter as self moves the instance into the method, taking ownership.
Question 28. Which of the following is a correct associated (static) function for a struct Foo? A) fn new() - > Self { Foo { … } } inside impl Foo B) fn new(&self) - > Self { … } inside impl Foo C) fn new(self) - > Self { … } inside impl Foo D) fn new(&mut self) - > Self { … } inside impl Foo Answer: A Explanation: An associated function does not take self; it is called as Foo::new().
Question 29. Which enum variant is used to represent the absence of a value in Rust’s standard library? A) None B) Nil C) Empty D) Zero Answer: A
Explanation: Option<T> has two variants: Some(T) and None, where None denotes no value.
Question 30. When matching on a Result<T, E>, which pattern matches both success and error cases? A) Ok(v) | Err(e) B) _ C) Result::Ok(v) D) Result::Err(e) Answer: B Explanation: The wildcard pattern _ matches any value, regardless of whether it is Ok or Err.
Question 31. Which of the following match arms includes a guard that only matches even numbers? A) n if n % 2 == 0 => … B) n @ even => … C) even => … D) n | 0 => … Answer: A Explanation: The if clause after a pattern is a match guard; n if n % 2 == 0 matches only when the condition holds.
Question 34. In a while let loop, what happens when the pattern no longer matches? A) The loop panics. B) The loop breaks silently. C) The loop continues forever. D) The compiler reports an error. Answer: B Explanation: while let continues while the pattern matches; once it fails, the loop exits.
Question 35. Which syntax correctly binds a value to a name while also matching a range? A) 0..=10 => … B) n @ 0..=10 => … C) 0..=10 @ n => … D) range(0,10) => … Answer: B Explanation: The @ operator lets you bind the matched value (n) while also testing it against a pattern (0..=10).
Question 36. Which of the following is a recoverable error handling idiom in Rust? A) panic!() B) Result<T, E>
C) assert!() D) unreachable!() Answer: B Explanation: Result encodes success (Ok) or recoverable failure (Err) that can be handled by the caller.
Question 37. What does the ? operator do when applied to a Result inside a function that returns Result? A. Converts Ok to Err. B. Propagates the Err early, returning it from the function. C. Converts Err to Ok. D. Panics on Err. Answer: B Explanation: The ? operator returns the Ok value, but if the expression is Err, it returns that error from the enclosing function.
Question 38. Which trait allows you to convert one error type into another using the ? operator? A) Into B) From C) Try
Answer: B Explanation: To satisfy Error, you must provide implementations for Display (for description) and optionally source; there is no built‑in derive.
Question 41. Which keyword is used to declare a trait in Rust? A) interface B) trait C) typeclass D) impl Answer: B Explanation: The trait keyword defines a set of required methods that types can implement.
Question 42. How do you implement a trait Drawable for a struct Circle? A) impl Drawable for Circle { … } B) impl Circle for Drawable { … } C) trait Circle: Drawable { … } D) impl Drawable(Circle) { … } Answer: A Explanation: The syntax impl Trait for Type { … } provides the required method definitions.
Question 43. Which of the following bounds restricts a generic type T to types that can be printed with {}? A) T: Debug B) T: Display C) T: Clone D) T: Copy Answer: B Explanation: The Display trait enables formatting with {}; Debug uses {:?}.
Question 44. What is a trait object in Rust? A) A generic type parameter. B) A concrete type that implements a trait. C) A value of type dyn Trait that uses dynamic dispatch. D) An enum variant. Answer: C Explanation: dyn Trait creates a trait object, enabling runtime polymorphism via a vtable.
Question 45. Which marker traits indicate that a type can be safely transferred across thread boundaries?