menu

Python Functions


1. 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


2.

What is the keyword used to define a function in Python?

define

function

def

create


3. What is a lambda function in Python?

A function that is defined within another function

A function that can be called recursively

A function that is defined using the lambda keyword and has no name

A function that is defined using the def keyword and has no name


4.

What is the output of the following code?
def my_function(**kwargs):return sum(kwargs.values())
result = my_function(a=1, b=2, c=3)
print(result)

1

4

6

3


5.

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


6.

What is the output of the following code?
def my_function(x, y=2):return x ** y
result1 = my_function(2)
result2 = my_function(2, 3)
print(result1, result2)

2 8

4 8

4 6

2 6


7.

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

5

6

TypeError

None


8.

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


9.

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

[1, 4, 9, 16]

[2, 4, 6, 8]

[0, 1, 4, 9

Error


10. 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