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
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() {}
What keyword is used to define a function in Rust?
a) def
b) func
c) fn
d) function
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 {}
What is the default return type of a function in Rust if not specified?
a) ()
b) int
c) i32
d) void
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
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
What type is used for passing a reference to a string in Rust?
a) &String
b) &str
c) String&
d) str&
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) {}
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
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)
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
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
Which Rust feature can simulate function overloading?
a) Structs
b) Generics
c) Macros
d) Traits
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) {}
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
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
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) {}
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| {}
Which keyword is used to specify that a closure takes ownership of a captured variable?
a) move
b) capture
c) take
d) store
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
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
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 {}
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)
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
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
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
In Rust, which closure trait is used when the closure only borrows its parameters immutably?
a) Fn
b) FnMut
c) FnOnce
d) Borrow
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)
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 = (|{})
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
Qno
Answer (Option with Text)
1
c) fn my_func() {}
2
c) fn
3
b) fn my_func() -> i32 {}
4
a) ()
5
b) Using an arrow -> after the parameters
6
a) Using & before the parameter name
7
b) &str
8
a) fn my_func() -> (i32, String) {}
9
b) By returning a tuple
10
a) &T
11
b) No, Rust does not support function overloading
12
a) By using generics
13
b) Generics
14
a) fn my_func<T>(x: T) {}
15
b) They allow functions to work with multiple types
16
a) A function that captures the environment
17
a) let closure =
18
c)
19
a) move
20
a) Closures can capture variables from their environment
21
b) It captures variables by value
22
a) let closure = move
23
a) Fn(i32) -> i32
24
b) Closures can capture variables from the environment
25
a) To process items in iterators
26
b) They can be passed as arguments to other functions
27
a) Fn
28
a) closure(5)
29
a) let closure =
30
c) Closures can either borrow or take ownership of variables