Python Programming - Functions

Exercise : Functions - General Questions
  • Functions - General Questions
6.
What is the purpose of the global keyword inside a function?
Declares a global variable
Specifies the scope of a function
Converts a local variable to a global variable
Prints a variable to the console
Answer: Option
Explanation:
The global keyword is used to indicate that a local variable should be treated as a global variable.

7.
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)
5
20
10
Error
Answer: Option
Explanation:
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.

8.
How do you pass a variable number of arguments to a function?
Using the *args syntax
Using the **kwargs syntax
Using the varargs() function
Using the arg_count parameter
Answer: Option
Explanation:
The *args syntax allows a function to accept a variable number of positional arguments.

9.
What is the purpose of the lambda keyword?
Declares a variable
Defines a class
Creates an anonymous function
Specifies the return type of a function
Answer: Option
Explanation:
The lambda keyword is used to create anonymous functions, also known as lambda functions.

10.
What does the term "recursion" refer to in the context of programming?
Declaring a variable again
A function calling itself
Reassigning the value of a variable
Using a loop construct
Answer: Option
Explanation:
Recursion is a programming concept where a function calls itself in its own definition.