Make a difficult, comprehensive exam on the latest version of the Rust programming…
- User
Make a difficult, comprehensive exam on the latest version of the Rust programming language. The theoretical questions should be MCQs (with answers) and there should be some self-contained small programming exercises (with answers) as applied, non-multiple-choice questions.
- Assistant
- Multiple Choice Questions: a) In Rust, what is the purpose of the ownership system?
- It ensures data safety and prevents data races.
- It allows for efficient memory management.
- It enables automatic garbage collection.
- It simplifies code readability.
Answer: 1. It ensures data safety and prevents data races.
b) Which of the following statements about borrowing in Rust is incorrect?
- Borrowing allows multiple immutable references to access data simultaneously.
- Borrowing prevents mutable references to the same data from being used simultaneously.
- Borrowing enables sharing mutable references to data across multiple threads.
- Borrowing ensures the integrity of data by enforcing strong ownership rules.
Answer: 3. Borrowing enables sharing mutable references to data across multiple threads.
c) What does the 'match' keyword do in Rust?
- It performs pattern matching for control flow operations.
- It defines a macro for generating the boilerplate code.
- It dynamically binds a variable to its runtime type.
- It defines a function for pattern matching purposes.
Answer: 1. It performs pattern matching for control flow operations.
- Programming Exercises:
a) Implement a Rust function called 'reverse_string' which accepts a string as an argument and returns the reverse of the input string.
fn reverse_string(input: &str) -> String { input.chars().rev().collect::<String>() }b) Write a Rust program that defines a struct called 'Rectangle' with 'width' and 'height' fields. Implement a method 'calculate_area' that returns the area of the rectangle. Additionally, implement a function 'create_square' that accepts a single parameter (side length) and creates a square rectangle (where width = height).
struct Rectangle { width: f64, height: f64, } impl Rectangle { fn calculate_area(&self) -> f64 { self.width * self.height } } fn create_square(side_length: f64) -> Rectangle { Rectangle { width: side_length, height: side_length, } }Note: These sample programming exercises may have multiple valid solutions.
Remember to provide suitable explanations, error handling, or additional implementation details where required.
- User
Re-do it with every question being designed such that there is only one correct answer.
- Assistant
- Multiple Choice Questions: a) In Rust, what is the purpose of the ownership system?
- It ensures data safety and prevents data races.
- It allows for efficient memory management.
- It enables automatic garbage collection.
- It simplifies code readability.
Answer: 1. It ensures data safety and prevents data races.
b) Which of the following statements about borrowing in Rust is correct?
- Borrowing allows multiple immutable references to access data simultaneously.
- Borrowing allows mutable references to the same data from being used simultaneously.
- Borrowing enables sharing mutable references to data across multiple threads.
- Borrowing ensures the integrity of data by enforcing strong ownership rules.
Answer: 1. Borrowing allows multiple immutable references to access data simultaneously.
c) What does the 'match' keyword do in Rust?
- It performs pattern matching for control flow operations.
- It defines a macro for generating the boilerplate code.
- It dynamically binds a variable to its runtime type.
- It defines a function for pattern matching purposes.
Answer: 1. It performs pattern matching for control flow operations.
- Programming Exercises: a) Implement a Rust function called 'reverse_string' which accepts a string as an argument and returns the reverse of the input string.
fn reverse_string(input: &str) -> String { input.chars().rev().collect::<String>() }b) Write a Rust program that defines a struct called 'Rectangle' with 'width' and 'height' fields. Implement a method 'calculate_area' that returns the area of the rectangle. Additionally, implement a function 'create_square' that accepts a single parameter (side length) and creates a square rectangle (where width = height).
struct Rectangle { width: f64, height: f64, } impl Rectangle { fn calculate_area(&self) -> f64 { self.width * self.height } } fn create_square(side_length: f64) -> Rectangle { Rectangle { width: side_length, height: side_length, } }Note: These sample programming exercises may have multiple valid solutions.
Remember to provide suitable explanations, error handling, or additional implementation details where required.