MCQs on Memory Management and Smart Pointers | Rust

Rust’s memory management system ensures safety and performance without a garbage collector. It uses smart pointers like Box, Rc, Arc, and RefCell to manage heap and stack memory effectively, preventing memory leaks.

Topics Overview:

  • The heap vs stack memory model
  • Box, Rc, and Arc smart pointers
  • RefCell and interior mutability
  • Memory leaks and Rust’s memory safety

MCQs on Memory Management and Smart Pointers in Rust

1. The Heap vs Stack Memory Model

  1. Which of the following is stored on the stack in Rust?
    • A) Dynamically allocated memory
    • B) Fixed-size variables
    • C) Data in collections like Vec
    • D) Strings in a String type
  2. The heap memory in Rust is primarily used for:
    • A) Storing small, fixed-size data
    • B) Storing dynamically sized data that is allocated at runtime
    • C) Storing function calls
    • D) Handling type safety at compile time
  3. What is the key difference between stack and heap memory?
    • A) Stack memory is faster but less flexible
    • B) Heap memory is used for local variables, stack for function calls
    • C) Heap memory is automatically deallocated, stack memory is manually managed
    • D) Stack memory is used for complex data types, heap for primitive types
  4. In Rust, the stack is typically used for:
    • A) Data that needs to persist across function calls
    • B) Fixed-size, known-at-compile-time data
    • C) Dynamically allocated memory
    • D) Structs and enums
  5. When does Rust deallocate memory from the stack?
    • A) When the program ends
    • B) When the memory is no longer in use and the scope ends
    • C) When explicitly instructed by the user
    • D) Never, the stack is fixed-size

2. Box, Rc, and Arc Smart Pointers

  1. What does Box in Rust do?
    • A) It provides an immutable reference to a value
    • B) It is used to allocate data on the heap
    • C) It allows shared ownership of data
    • D) It enables safe concurrent access to data
  2. Which of the following is true about Rc in Rust?
    • A) It is a smart pointer used for exclusive ownership
    • B) It allows multiple ownership of heap-allocated data
    • C) It is used for mutable references only
    • D) It works across threads
  3. What is the primary difference between Rc and Arc in Rust?
    • A) Rc is for single-threaded usage, while Arc is for multi-threaded usage
    • B) Arc is a faster alternative to Rc
    • C) Rc allows mutable references, while Arc does not
    • D) Arc is for mutable heap allocations, while Rc is for immutable
  4. Which of the following smart pointers provides shared ownership of data across multiple threads?
    • A) Box
    • B) Rc
    • C) Arc
    • D) RefCell
  5. When should you use Box over Rc or Arc in Rust?
    • A) When you need to share ownership of data across threads
    • B) When you need to store a value on the heap and don’t need to share ownership
    • C) When you need to mutate data at runtime
    • D) When you need to work with interior mutability

3. RefCell and Interior Mutability

  1. What is the primary purpose of RefCell in Rust?
  • A) To allow safe mutable access to data through borrowing
  • B) To provide immutable references to data
  • C) To enable interior mutability at runtime
  • D) To prevent any mutable references at all
  1. Which of the following allows interior mutability in Rust?
  • A) RefCell
  • B) Box
  • C) Arc
  • D) Rc
  1. How does RefCell ensure memory safety in Rust?
  • A) By using compile-time borrow checking
  • B) By using runtime borrow checking for mutable and immutable references
  • C) By only allowing immutable references
  • D) By preventing concurrent access to data
  1. What happens if you try to borrow a RefCell mutably while it is already borrowed immutably?
  • A) The code compiles successfully
  • B) The borrow checker throws an error at runtime
  • C) The mutable borrow is allowed to overwrite the immutable borrow
  • D) The program will panic at runtime
  1. In what scenarios would you use RefCell in Rust?
  • A) When you need to share ownership of data between threads
  • B) When you need interior mutability but still want to comply with Rust’s borrowing rules
  • C) When you need to store data on the stack
  • D) When you need to store immutable data on the heap

4. Memory Leaks and Rust’s Memory Safety

  1. Rust prevents memory leaks by:
  • A) Automatically garbage collecting unused memory
  • B) Releasing memory when the owner variable goes out of scope
  • C) Allowing developers to manually deallocate memory
  • D) Using reference counting for all variables
  1. What is a memory leak in the context of Rust?
  • A) Memory that is freed prematurely
  • B) Memory that is never properly deallocated after use
  • C) Memory that is shared across threads without synchronization
  • D) Memory that is allocated to the stack
  1. Which of the following can cause a memory leak in Rust?
  • A) Incorrect usage of Box or Rc
  • B) Properly managing memory using the ownership system
  • C) Using RefCell for interior mutability
  • D) Moving variables between threads
  1. How does Rust’s ownership system help prevent memory leaks?
  • A) By enforcing garbage collection
  • B) By automatically freeing memory when a variable goes out of scope
  • C) By using reference counting
  • D) By allowing developers to manage memory manually
  1. Which of the following is a potential cause of a memory leak when using Rc in Rust?
  • A) Circular references between Rc pointers
  • B) Shared ownership across threads
  • C) Borrowing a reference multiple times
  • D) Using RefCell for mutable references

Answer Key:

QnoAnswer (Option with Text)
1B) Fixed-size variables
2B) Storing dynamically sized data that is allocated at runtime
3A) Stack memory is faster but less flexible
4B) Fixed-size, known-at-compile-time data
5B) When the memory is no longer in use and the scope ends
6B) It is used to allocate data on the heap
7B) It allows multiple ownership of heap-allocated data
8A) Rc is for single-threaded usage, while Arc is for multi-threaded usage
9C) Arc
10B) When you need to store a value on the heap and don’t need to share ownership
11C) To enable interior mutability at runtime
12A) RefCell
13B) By using runtime borrow checking for mutable and immutable references
14B) The borrow checker throws an error at runtime
15B) When you need interior mutability but still want to comply with Rust’s borrowing rules
16B) Releasing memory when the owner variable goes out of scope
17B) Memory that is never properly deallocated after use
18A) Incorrect usage of Box or Rc
19B) By automatically freeing memory when a variable goes out of scope
20A) Circular references between Rc pointers

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top