menu

Python Functions


1.

What is the output of the following code?
def add_numbers(a, b):return a + b
result = add_numbers(5, 10)
print(result)

5

10

15

50


2.

What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
def square(x):return x * x
squared_numbers = map(square, numbers)
print(list(squared_numbers))

[1, 4, 9, 16, 25]

[1, 2, 3, 4, 5]

[2, 4, 6, 8, 10]

[3, 6, 9, 12, 15]


3. What is the purpose of the map function in Python?

It applies a function to each element of an iterable and returns a new iterable

It filters an iterable based on a given function

It converts an iterable to a dictionary

It sorts an iterable based on a given function


4. What is the purpose of the filter function in Python?

It applies a function to each element of an iterable and returns a new iterable

It filters an iterable based on a given function

It converts an iterable to a dictionary

It sorts an iterable based on a given function


5.

What is the output of the following code?
def my_function():global x
x = 3
x = 5
my_function()
print(x)

3

5

8

Error


6.

What is the output of the following code?
def my_function(a, b, c=0, d=0):return a + b + c + d
result = my_function(1, 2, 3, 4)
print(result)

3

6

10

5


7. What is the difference between function parameters and arguments in Python?

There is no difference

Parameters are values passed to a function, whereas arguments are variables defined within a function

Arguments are values passed to a function, whereas parameters are variables defined within a function

Parameters and arguments are both variables defined within a function


8. What is the purpose of the **kwargs parameter in a function definition?

It allows the function to accept a variable number of positional arguments

It allows the function to accept a variable number of keyword arguments

It defines the default arguments for a function

It defines the required arguments for a function


9. What is the purpose of the *args parameter in a function definition?

It allows the function to accept a variable number of keyword arguments

It allows the function to accept a variable number of positional arguments

It defines the default arguments for a function

It defines the required arguments for a function


10.

What is the output of the following code?
def my_function(*args):return sum(args)
result = my_function(1, 2, 3, 4)
print(result)

1

4

10

3