menu

Python Functions

Python Functions - Important Points


16.

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)

A. 1

B. 4

C. 6

D. 3

Discuss Work Space

Answer: option c

Explanation:

The my_function function accepts a variable number of keyword arguments using the ** kwargs syntax. The function returns the sum of all the values of the keyword arguments passed to it (1 + 2 + 3 = 6).

17. What is recursion in Python?

A. A function that returns a value based on a given condition

B. A function that calls itself

C. A function that performs a loop

D. A function that accepts a variable number of arguments

Discuss Work Space

Answer: option b

Explanation:

Recursion is a technique in which a function calls itself one or more times in order to solve a problem.

18.

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

A. 5

B. 6

C. TypeError

D. None

Discuss Work Space

Answer: option b

Explanation:

The global keyword inside the my_function function indicates that the variable x refers to the global variable with the same name. The function increments the global variable x by 1, so its new value is 6.

19.

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

A. 5

B. 8

C. 6

D. 1

Discuss Work Space

Answer: option b

Explanation:

The ** operator is used to calculate the power of a number. The my_function function takes two arguments x and y, and returns x raised to the power of y. In this case, 2 raised to the power of 3 is 8.

20.

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)

A. 2 8

B. 4 8

C. 4 6

D. 2 6

Discuss Work Space

Answer: option b

Explanation:

The my_function function takes two arguments x and y, with a default value of 2 for y. If only one argument is passed, y takes the default value. The function returns x raised to the power of y. In this case, result1 is 2 raised to the power of 2, which is 4, and result2 is 2 raised to the power of 3, which is 8.


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