MCQs on Structs and Enums | Rust

Rust’s powerful type system includes the use of structs and enums, allowing developers to define complex data structures and control program flow through pattern matching. This set of 30 multiple-choice questions explores Rust’s structs, enums, and the use of pattern matching, focusing on practical aspects like struct methods, associated functions, and enums like Option and Result.


MCQs on Structs and Enums in Rust

Defining Structs

  1. In Rust, which keyword is used to define a struct?
    • A) structure
    • B) struct
    • C) class
    • D) object
  2. Which of the following defines a basic struct in Rust?
    • A) struct Point { x: i32, y: i32 }
    • B) struct Point(x: i32, y: i32)
    • C) struct { x, y }
    • D) struct Point => { x, y }
  3. How do you create an instance of a struct in Rust?
    • A) let p = Point.new(10, 20);
    • B) let p = Point { x: 10, y: 20 };
    • C) let p = new Point(10, 20);
    • D) let p = Point(10, 20);
  4. What must each field in a struct be separated by?
    • A) Colon :
    • B) Comma ,
    • C) Semi-colon ;
    • D) Period .
  5. What is the purpose of the #[derive] attribute in structs?
    • A) To define the struct’s methods
    • B) To enable automatic implementation of certain traits like Debug or Clone
    • C) To enforce a specific data type
    • D) To create an instance of a struct
  6. Which of the following is a correct definition of a struct with multiple types in Rust?
    • A) struct Info { name: String, age: u32, is_active: bool }
    • B) struct Info(name, age, is_active)
    • C) struct Info { name: String, age: u32, bool is_active }
    • D) struct Info(String, u32, bool)
  7. Can a struct in Rust have methods?
    • A) Yes, methods can be defined inside the struct
    • B) No, structs cannot have methods
    • C) Yes, but only static methods
    • D) Yes, methods are defined outside the struct
  8. What is the default visibility of a field in a Rust struct?
    • A) Private
    • B) Public
    • C) Protected
    • D) Internal
  9. In a struct, how do you make a field public so that it can be accessed from outside the module?
    • A) pub name: String
    • B) private name: String
    • C) let name: String
    • D) name: String with #[allow(public)]
  10. Which of the following is true about tuples in comparison to structs in Rust?
    • A) Tuples do not have named fields, while structs do
    • B) Tuples require explicit types for each element
    • C) Structs cannot store multiple data types
    • D) Tuples are a subtype of structs in Rust

Struct Methods and Associated Functions

  1. What is an associated function in Rust?
  • A) A function that is tied to a specific struct type but does not operate on its data
  • B) A function that can only be called on instances of a struct
  • C) A function that belongs to a module
  • D) A function that is executed only once during the program’s lifecycle
  1. Which keyword is used to define methods inside a impl block in Rust?
  • A) def
  • B) function
  • C) impl
  • D) struct
  1. How do you define a method that takes ownership of self in a Rust struct?
  • A) fn method(self) { ... }
  • B) fn method(&self) { ... }
  • C) fn method(self: &mut self) { ... }
  • D) fn method(self: Box<Self>) { ... }
  1. In Rust, methods defined on a struct can access the struct’s data through which of the following?
  • A) self
  • B) this
  • C) super
  • D) parent
  1. What is the purpose of the new() method commonly defined in Rust structs?
  • A) To set default values for struct fields
  • B) To create an instance of the struct
  • C) To print struct data
  • D) To destructure the struct into individual elements
  1. Which of the following is a valid syntax for calling a method on a struct instance?
  • A) instance.method()
  • B) method(instance)
  • C) method->instance()
  • D) instance::method()
  1. What does the &self parameter signify when used in a method of a struct?
  • A) The method borrows self immutably
  • B) The method borrows self mutably
  • C) The method takes ownership of self
  • D) The method does not use self
  1. How do you define a method that modifies a field of a struct in Rust?
  • A) Use &mut self as the method parameter
  • B) Use self to take ownership
  • C) Use &self and return a new value
  • D) Methods cannot modify fields of a struct
  1. Can you define static methods in Rust structs?
  • A) Yes, by using the static keyword
  • B) Yes, by defining methods inside an impl block
  • C) No, Rust only supports instance methods
  • D) No, static methods are not allowed in Rust
  1. What does the Box keyword indicate when used in a method definition?
  • A) The method takes ownership of a boxed value
  • B) The method returns a boxed value
  • C) The method borrows a boxed value immutably
  • D) The method borrows a boxed value mutably

Enums and Pattern Matching with Enums

  1. In Rust, what keyword is used to define an enum?
  • A) enum
  • B) enumtype
  • C) type
  • D) data
  1. Which of the following defines an enum with two variants, Success and Failure?
  • A) enum Result { Success, Failure }
  • B) enum Result(Success, Failure)
  • C) enum Result { Success => i32, Failure => i32 }
  • D) enum Result {Success: i32, Failure: i32}
  1. How can you match against specific variants in an enum using pattern matching?
  • A) match enum { Result::Success => { ... }, Result::Failure => { ... } }
  • B) enum match { Result::Success => { ... }, Result::Failure => { ... } }
  • C) match { Result => { ... } }
  • D) case enum { Success, Failure }
  1. What does the match keyword allow you to do in Rust?
  • A) Compare variables with other types
  • B) Match patterns against specific enum variants
  • C) Perform arithmetic operations
  • D) Automatically convert types
  1. How do you define an enum with data attached to each variant in Rust?
  • A) enum Option { Some(i32), None }
  • B) enum Option { Some: i32, None: i32 }
  • C) enum Option { i32 => Some, None }
  • D) enum Option { Some { value: i32 }, None }
  1. What is the pattern matching syntax for accessing data within an enum variant?
  • A) match enum { Option::Some(value) => { ... } }
  • B) match { Option::Some => value }
  • C) match Option { Some => { value } }
  • D) match Option::Some(value)
  1. How do you handle cases where an enum variant might not match any pattern?
  • A) Use the else keyword
  • B) Use the _ wildcard in a match statement
  • C) Ignore unmatched patterns
  • D) Use continue for unmatched patterns
  1. Which enum in Rust is commonly used to represent the presence or absence of a value?
  • A) Option
  • B) Result
  • C) Enum
  • D) Null
  1. Which of the following is a typical use case for the Result enum in Rust?
  • A) Handling success and error in functions
  • B) Storing multiple data types
  • C) Representing an optional value
  • D) Representing a collection of values
  1. How do you handle the None variant in the Option enum when pattern matching?
  • A) match Option { Some(x) => { ... }, None => { ... } }
  • B) match None { Option::Some => { ... }, None => { ... } }
  • C) match { Option => { None } }
  • D) case { None }

Answers

QnoAnswer
1B) struct
2A) struct Point { x: i32, y: i32 }
3B) let p = Point { x: 10, y: 20 };
4B) Comma ,
5B) To enable automatic implementation of certain traits like Debug or Clone
6A) struct Info { name: String, age: u32, is_active: bool }
7A) Yes, methods can be defined inside the struct
8A) Private
9A) pub name: String
10A) Tuples do not have named fields, while structs do
11A) A function that is tied to a specific struct type but does not operate on its data
12C) impl
13A) fn method(self) { ... }
14A) self
15B) To create an instance of the struct
16A) instance.method()
17A) The method borrows self immutably
18A) Use &mut self as the method parameter
19B) Yes, by defining methods inside an impl block
20A) The method takes ownership of a boxed value
21A) enum
22A) enum Result { Success, Failure }
23A) match enum { Result::Success => { ... }, Result::Failure => { ... } }
24B) Match patterns against specific enum variants
25A) enum Option { Some(i32), None }
26A) match enum { Option::Some(value) => { ... } }
27B) Use the _ wildcard in a match statement
28A) Option
29A) Handling success and error in functions
30A) match Option { Some(x) => { ... }, None => { ... } }

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