Exercise: Functions

Questions for: Functions

How do you pass a variable number of arguments to a function?
A:
Using the *args syntax
B:
Using the **kwargs syntax
C:
Using the varargs() function
D:
Using the arg_count parameter
Answer: A
The *args syntax allows a function to accept a variable number of positional arguments.
What will be the result of the following code snippet?
x = 10

def multiply_by_two():
    global x
    x *= 2

multiply_by_two()
print(x)
A:
5
B:
20
C:
10
D:
Error
Answer: B
The global keyword inside the function indicates that the variable x should be treated as a global variable, and its value is multiplied by 2.
What is the purpose of the global keyword inside a function?
A:
Declares a global variable
B:
Specifies the scope of a function
C:
Converts a local variable to a global variable
D:
Prints a variable to the console
Answer: C
The global keyword is used to indicate that a local variable should be treated as a global variable.
What does the term "function signature" refer to?
A:
The name of the function
B:
The entire function body
C:
The list of parameters and their types
D:
The return statement of the function
Answer: C
The function signature includes the name of the function and the list of parameters along with their types.
What is the purpose of the return statement in a function?
A:
Prints a value to the console
B:
Terminates the program
C:
Exits the function and returns a value
D:
Declares a variable
Answer: C
The return statement is used to exit a function and return a value to the caller.
Ad Slot (Above Pagination)
Quiz