menu

Python Functions

Python Functions - Important Points


21.

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

A. 5

B. 2

C. 3

D. 1

Discuss Work Space

Answer: option a

Explanation:

The my_function function takes two arguments x and y, and returns their sum. In this case, the arguments are passed by keyword, so their order doesn't matter. The function is called with x=3 and y=2, so the output is 5.

22.

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

A. 10

B. 24

C. 8

D. 1

Discuss Work Space

Answer: option b

Explanation:

The my_function function takes two arguments x and y, and returns their product. In the main code, a list of numbers is defined, and the product of all the numbers in the list is calculated using a loop and the my_function function. The initial value of the result variable is 1, so the final product is 1 * 2 * 3 * 4, which is 24.

23.

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

A. 3

B. 5

C. 8

D. Error

Discuss Work Space

Answer: option b

Explanation:

The my_function function declares the variable x as global, and assigns the value 3 to it. In the main code, a variable x is defined with the value 5, and then the my_function function is called. The function modifies the global variable x, and assigns it the value 5. When the print statement is executed, the value of x is 5.

24.

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

A. [1, 4, 9, 16]

B. [2, 4, 6, 8]

C. [0, 1, 4, 9

D. Error

Discuss Work Space

Answer: option a

Explanation:

The my_function function takes a single argument x, and returns the square of x. In the main code, a list of numbers is defined, and the map function is used to apply the my_function function to each number in the list. The result of the map function is a map object, which needs to be converted to a list using the list function. The resulting list contains the square of each number in the original list, which are 1, 4, 9, and 16.


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