MCQs on Functions | Rust

In Rust, functions are fundamental building blocks for creating reusable and modular code. This topic covers defining functions, handling parameters and return values, function overloading with types, and working with closures. Mastering these concepts enhances your ability to write efficient and flexible code in Rust.


MCQs on Functions in Rust Programming

Defining Functions

  1. Which of the following is the correct syntax to define a function in Rust?
    • a) function my_func() {}
    • b) def my_func() {}
    • c) fn my_func() {}
    • d) function my_func() {}
  2. What keyword is used to define a function in Rust?
    • a) def
    • b) func
    • c) fn
    • d) function
  3. In Rust, how do you define a function that returns an integer?
    • a) fn my_func() -> int {}
    • b) fn my_func() -> i32 {}
    • c) fn my_func() -> Integer {}
    • d) fn my_func() -> Integer32 {}
  4. What is the default return type of a function in Rust if not specified?
    • a) ()
    • b) int
    • c) i32
    • d) void
  5. How do you specify the return type of a function in Rust?
    • a) Using a colon : after the function name
    • b) Using an arrow -> after the parameters
    • c) Using an equal sign =
    • d) By not specifying anything

Function Parameters and Return Values

  1. How do you pass parameters by reference in Rust functions?
    • a) Using & before the parameter name
    • b) Using * before the parameter name
    • c) Using ref before the parameter name
    • d) Using const before the parameter name
  2. What type is used for passing a reference to a string in Rust?
    • a) &String
    • b) &str
    • c) String&
    • d) str&
  3. Which of the following is the correct way to define a function that returns a tuple in Rust?
    • a) fn my_func() -> (i32, String) {}
    • b) fn my_func() -> Tuple(i32, String) {}
    • c) fn my_func() -> (i32, 'String') {}
    • d) fn my_func() -> (Integer, String) {}
  4. How do you return multiple values from a function in Rust?
    • a) By returning an array
    • b) By returning a tuple
    • c) By returning a vector
    • d) By using a reference
  5. In Rust, how do you return a reference to a value from a function?
  • a) &T
  • b) T&
  • c) T*
  • d) &mut T

Function Overloading (with Types)

  1. Does Rust support function overloading based on different parameter types?
  • a) Yes, it allows overloading with different parameter types
  • b) No, Rust does not support function overloading
  • c) Yes, but only with the same parameter types
  • d) Yes, but only with different return types
  1. How can you handle multiple versions of a function in Rust?
  • a) By using generics
  • b) By using function overloading
  • c) By using macros
  • d) By using traits
  1. Which Rust feature can simulate function overloading?
  • a) Structs
  • b) Generics
  • c) Macros
  • d) Traits
  1. How would you define a generic function in Rust?
  • a) fn my_func<T>(x: T) {}
  • b) fn my_func(x: T) -> T {}
  • c) fn my_func(x: T) {}
  • d) fn my_func<T: Type>(x: T) {}
  1. What is the main advantage of using generics in Rust?
  • a) They allow for runtime polymorphism
  • b) They allow functions to work with multiple types
  • c) They ensure type safety
  • d) They provide function overloading

Closures and Their Syntax

  1. What is a closure in Rust?
  • a) A function that captures the environment
  • b) A function with no parameters
  • c) A function that returns a closure
  • d) A function that is only callable inside another function
  1. How do you define a closure in Rust?
  • a) let closure = |param| {}
  • b) let closure = (param) => {}
  • c) let closure = fn(param) {}
  • d) let closure = function(param) {}
  1. Which of the following is the correct way to capture values by reference in a Rust closure?
  • a) |x| {}
  • b) |&x| {}
  • c) |x: &T| {}
  • d) |&x: &T| {}
  1. Which keyword is used to specify that a closure takes ownership of a captured variable?
  • a) move
  • b) capture
  • c) take
  • d) store
  1. How does a closure differ from a regular function in Rust?
  • a) Closures can capture variables from their environment
  • b) Closures cannot accept parameters
  • c) Closures must return a value
  • d) Closures are not type-safe
  1. What does the move keyword in a closure do?
  • a) It captures variables by reference
  • b) It captures variables by value
  • c) It returns the closure
  • d) It makes the closure mutable
  1. Which of the following is the correct closure syntax for taking ownership of a variable?
  • a) let closure = move |x| {}
  • b) let closure = |x| move {}
  • c) let closure = |move x| {}
  • d) let closure = |x: T| move {}
  1. What is the type of the following closure in Rust: |x: i32| x + 1?
  • a) Fn(i32) -> i32
  • b) FnMut(i32) -> i32
  • c) FnOnce(i32) -> i32
  • d) Fn(i32)
  1. How do closures in Rust differ from functions regarding their parameters?
  • a) Closures can take parameters of any type
  • b) Closures can capture variables from the environment
  • c) Functions can take parameters, but closures cannot
  • d) Closures must have a return type
  1. How are closures used in Rust’s standard library?
  • a) To process items in iterators
  • b) To handle data serialization
  • c) To define recursive functions
  • d) To create macros
  1. Which of the following is true about closures in Rust?
  • a) They are always faster than functions
  • b) They can be passed as arguments to other functions
  • c) They are restricted to non-capturing environments
  • d) They cannot return values
  1. In Rust, which closure trait is used when the closure only borrows its parameters immutably?
  • a) Fn
  • b) FnMut
  • c) FnOnce
  • d) Borrow
  1. Which of the following is the correct way to call a closure in Rust?
  • a) closure(5)
  • b) closure{5}
  • c) call closure(5)
  • d) call(closure, 5)
  1. How would you define a closure that takes no parameters in Rust?
  • a) let closure = || {}
  • b) let closure = fn() {}
  • c) let closure = |{}|
  • d) let closure = (|{})
  1. Which of the following is true about the ownership rules in Rust for closures?
  • a) Closures do not require ownership tracking
  • b) Closures always borrow variables
  • c) Closures can either borrow or take ownership of variables
  • d) Closures only borrow variables

Answers

QnoAnswer (Option with Text)
1c) fn my_func() {}
2c) fn
3b) fn my_func() -> i32 {}
4a) ()
5b) Using an arrow -> after the parameters
6a) Using & before the parameter name
7b) &str
8a) fn my_func() -> (i32, String) {}
9b) By returning a tuple
10a) &T
11b) No, Rust does not support function overloading
12a) By using generics
13b) Generics
14a) fn my_func<T>(x: T) {}
15b) They allow functions to work with multiple types
16a) A function that captures the environment
17a) let closure =
18c)
19a) move
20a) Closures can capture variables from their environment
21b) It captures variables by value
22a) let closure = move
23a) Fn(i32) -> i32
24b) Closures can capture variables from the environment
25a) To process items in iterators
26b) They can be passed as arguments to other functions
27a) Fn
28a) closure(5)
29a) let closure =
30c) Closures can either borrow or take ownership of variables

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