Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

for the same function func1, what will be printed when we run the follo…

Question

for the same function func1, what will be printed when we run the following code?
def func1(x, y, z = 2):
if x - y < z:
return (x + y) * z
else:
return (x + y) / z
a = 2
b = 4
result = func1(a,b,3)
result = func1(b,a)
print(result)

Explanation:

Step1: Analyze the first function call

When result = func1(a,b,3) is executed, x = 2, y = 4, z = 3. Calculate x - y = 2 - 4=-2. Since -2<3, return (x + y)z=(2 + 4)3 = 18. But then result = func1(b,a) is executed.

Step2: Analyze the second function call

When result = func1(b,a) is executed, x = 4, y = 2, z takes the default value 2. Calculate x - y=4 - 2 = 2. Since 2 is not less than 2 (the condition x - y < z is False), return (x + y)/z=(4 + 2)/2=3.

Answer:

B. 3.0