Thursday , September 19 2024

What is the output of the following snippet? def fun(x, y, z): return x + 2 * y + 3 * z print(fun(0, z=1, y=3))

Questions BankCategory: Python Essentials 1What is the output of the following snippet? def fun(x, y, z): return x + 2 * y + 3 * z print(fun(0, z=1, y=3))
What is the output of the following snippet?

def fun(x, y, z):
    return x + 2 * y + 3 * z
 
 
print(fun(0, z=1, y=3))
  • 0
  • 3
  • the snippet is erroneous
  • 9

Explanation: Let’s analyze the code:

  • the fun function is invoked, and the arguments take these values: x = 0, y = 3, z = 1. Remember that positional arguments should be placed before keyword arguments,
  • the fun function returns the result of the following arithmetic operation: 0 + 2 * 3 + 3 * 1,
  • the products are carried out first: 0 + 6 + 3,
  • the addition is performed, and the result is 9,
  • the print function shows 9 in the console.

More Questions: Python Essentials 1 – Module 4 Test