Rust Functions Quiz

Rust
0 Passed
0% acceptance

40 questions covering function parameters, return values, expressions, diverging functions, and function scope in Rust.

40 Questions
~80 minutes
1

Question 1

What is the syntax for defining a function in Rust?

A
fn function_name() { ... }
B
function function_name() { ... }
C
def function_name() { ... }
D
func function_name() { ... }
2

Question 2

How do you specify parameters in a Rust function?

A
fn add(x: i32, y: i32) -> i32 { x + y }
B
fn add(x, y: i32) -> i32 { x + y }
C
fn add(x: i32, y) -> i32 { x + y }
D
fn add(x y: i32) -> i32 { x + y }
3

Question 3

What does the -> syntax indicate in a function signature?

A
The return type of the function
B
A reference parameter
C
An arrow function
D
A closure
4

Question 4

What happens if you don't specify a return type for a function?

A
It returns () (unit type)
B
Compilation error
C
It returns the last expression
D
It returns i32 by default
5

Question 5

How do you explicitly return a value from a function?

A
Use the return keyword or omit semicolon on last expression
B
Only use the return keyword
C
Only omit semicolon on last expression
D
Use the yield keyword
6

Question 6

What is a statement in Rust?

A
An expression that returns a value
B
An instruction that performs an action but doesn't return a value
C
A function call
D
A variable declaration
7

Question 7

What is an expression in Rust?

A
An instruction that performs an action
B
Code that evaluates to a value
C
A function definition
D
A variable assignment
8

Question 8

Can function bodies contain both statements and expressions?

A
Yes, statements perform actions, expressions return values
B
No, only expressions are allowed
C
No, only statements are allowed
D
Only in certain contexts
9

Question 9

What will this function return?

rust
fn example() -> i32 {
    let x = 5;
    x + 1
}
A
6
B
5
C
()
D
Compilation error
10

Question 10

What will this function return?

rust
fn example() -> i32 {
    let x = 5;
    x + 1;
}
A
6
B
5
C
()
D
Compilation error
11

Question 11

What are diverging functions in Rust?

A
Functions that never return to the caller
B
Functions that return multiple values
C
Functions that take variable arguments
D
Functions that modify their parameters
12

Question 12

What is the return type of a diverging function?

A
!
B
()
C
Option<T>
D
Result<T, E>
13

Question 13

Which of these is an example of a diverging function?

A
fn panic_function() -> ! { panic!(); }
B
fn loop_forever() -> ! { loop {} }
C
fn exit_program() -> ! { std::process::exit(0); }
D
All of the above
14

Question 14

How does function scope affect variable lifetime?

A
Variables live until the end of their scope
B
Variables live for the entire program
C
Variables are garbage collected
D
Variables live until explicitly dropped
15

Question 15

What happens to variables when a function returns?

A
They are dropped in reverse order of creation
B
They remain in memory
C
They are moved to the caller
D
They become invalid
16

Question 16

Can functions be defined inside other functions?

A
Yes, as nested functions
B
No, functions can only be defined at module level
C
Only in certain contexts
D
Only anonymous functions can be nested
17

Question 17

What is the scope of a variable declared inside a function?

A
From its declaration until the end of the function
B
Global scope
C
Until the next function call
D
Until explicitly freed
18

Question 18

How do you pass ownership of a value to a function?

A
By passing the value directly
B
By using &mut
C
By using &
D
By cloning
19

Question 19

How do you allow a function to borrow a value without taking ownership?

A
Use & in the parameter type
B
Use mut in the parameter
C
Pass by value
D
Use Box<T>
20

Question 20

What happens to borrowed values when a function returns?

A
They remain borrowed by the caller
B
They are moved to the caller
C
They are dropped
D
They become owned by the caller
21

Question 21

Can a function modify a borrowed value?

A
Only if it's &mut
B
Yes, always
C
No, never
D
Only for primitive types
22

Question 22

What is the difference between String and &str parameters?

A
String takes ownership, &str borrows
B
They are the same
C
&str takes ownership, String borrows
D
String is for literals, &str for owned strings
23

Question 23

When should you use &str instead of String in function parameters?

A
When you only need to read the string
B
When you need to modify the string
C
When you need to return the string
D
When the string is very long
24

Question 24

What will this function do?

rust
fn take_ownership(s: String) {
    println!("{}", s);
} // s is dropped here
A
Print the string and drop it
B
Print the string and return it
C
Move the string to the caller
D
Borrow the string temporarily
25

Question 25

What will this function do?

rust
fn borrow_string(s: &String) {
    println!("{}", s);
} // s remains borrowed
A
Print the string and drop it
B
Print the string and keep it borrowed
C
Take ownership of the string
D
Modify the string
26

Question 26

How do you return a value from a function while maintaining ownership?

A
Return the value directly
B
Use references
C
Use Rc<T>
D
Use Box<T>
27

Question 27

What happens if you try to return a reference to a local variable?

A
Compilation error - cannot return references to locals
B
The reference is automatically extended
C
The local variable is moved
D
Runtime error
28

Question 28

How can you return a string from a function?

A
fn get_string() -> String { String::from("hello") }
B
fn get_string() -> &str { "hello" }
C
fn get_string() -> String { &String::from("hello") }
D
Both A and B work
29

Question 29

What is function overloading in Rust?

A
Defining multiple functions with the same name but different parameters
B
Defining functions with variable arguments
C
Not supported in Rust
D
Using generics for different types
30

Question 30

How does Rust handle functions with the same name?

A
Compilation error
B
Last definition wins
C
They are overloaded
D
They must be in different modules
31

Question 31

What are generic functions in Rust?

A
Functions that work with multiple types
B
Functions that take variable arguments
C
Functions that can be overloaded
D
Functions that return any type
32

Question 32

What is the syntax for generic functions?

A
fn generic<T>(value: T) { ... }
B
fn generic(value: T) { ... }
C
fn generic(value) { ... }
D
fn generic(value: any) { ... }
33

Question 33

Can functions have multiple generic parameters?

A
Yes, fn func<T, U>(t: T, u: U)
B
No, only one generic parameter allowed
C
Only for structs
D
Only for traits
34

Question 34

What are trait bounds in generic functions?

A
Constraints on what types can be used
B
Limits on function size
C
Memory bounds
D
Performance bounds
35

Question 35

What is the syntax for trait bounds?

A
fn func<T: Display>(value: T)
B
fn func<T where T: Display>(value: T)
C
Both A and B
D
fn func(value: T where T: Display)
36

Question 36

What happens if you call a diverging function?

A
The program terminates or loops forever
B
The function returns a value
C
The caller continues execution
D
Memory is leaked
37

Question 37

How can diverging functions be useful?

A
For error handling and program termination
B
For returning multiple values
C
For performance optimization
D
For memory management
38

Question 38

What is the scope hierarchy in Rust?

A
Function > Block > Module > Crate
B
Module > Function > Block > Crate
C
Crate > Module > Function > Block
D
Block > Function > Module > Crate
39

Question 39

Can inner scopes access variables from outer scopes?

A
Yes, but they cannot modify them unless mutable
B
No, scopes are completely isolated
C
Only for global variables
D
Only for constants
40

Question 40

What happens to variables shadowed in inner scopes?

A
The outer variable is temporarily inaccessible
B
The outer variable is dropped
C
Compilation error
D
The inner variable replaces the outer one permanently

QUIZZES IN Rust