menu

Python Functions

Python Functions - Important Points


11.

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))

A. [1, 4, 9, 16, 25]

B. [1, 2, 3, 4, 5]

C. [2, 4, 6, 8, 10]

D. [3, 6, 9, 12, 15]

Discuss Work Space

Answer: option a

Explanation:

The map function applies the square function to each element of the numbers list and returns a new iterable. The list function is used to convert the iterable to a list, which is then printed.

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

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

B. It filters an iterable based on a given function

C. It converts an iterable to a dictionary

D. It sorts an iterable based on a given function

Discuss Work Space

Answer: option b

Explanation:

The filter function filters an iterable based on a given function and returns a new iterable.

13.

What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
def is_even(x):return x % 2 == 0
even_numbers = filter(is_even, numbers)
print(list(even_numbers))

A. [1, 3, 5]

B. [2, 4]

C. [1, 2, 3, 4, 5]

D. []

Discuss Work Space

Answer: option b

Explanation:

The filter function applies the is_even function to each element of the numbers list and filters out the elements that are not even. The list function is used to convert the iterable to a list, which is then printed.

14.

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)

A. 3

B. 6

C. 10

D. 5

Discuss Work Space

Answer: option c

Explanation:

The my_function function accepts four arguments, with the last two having default values of 0. The function returns the sum of all four arguments (1 + 2 + 3 + 4 = 10).

15.

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

A. 1

B. 4

C. 10

D. 3

Discuss Work Space

Answer: option c

Explanation:

The my_function function accepts a variable number of positional arguments using the *args syntax. The function returns the sum of all the arguments passed to it (1 + 2 + 3 + 4 = 10).


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now