What will be the output of the following Python code?def foo(fname, val): print(fname(val))foo(max, [1, 2, 3])foo(min, [1, 2, 3]) 3 11 3errornone of the mentioned
Question
What will be the output of the following Python code?def foo(fname, val): print(fname(val))foo(max, [1, 2, 3])foo(min, [1, 2, 3]) 3 11 3errornone of the mentioned
Solution
The output of the given Python code will be:
3 1
Here's the step by step explanation:
-
The function
foois defined to take two arguments:fnameandval. Inside the function, it prints the result of callingfname(val). -
The function
foois first called withmaxasfnameand[1, 2, 3]asval. Themaxfunction returns the maximum value in the list, which is 3. So,3is printed. -
The function
foois then called withminasfnameand[1, 2, 3]asval. Theminfunction returns the minimum value in the list, which is 1. So,1is printed.
Similar Questions
What will be the output of the following Python code?def printMax(a, b): if a > b: print(a, 'is maximum') elif a == b: print(a, 'is equal to', b) else: print(b, 'is maximum')printMax(3, 4) None of the mentioned344 is maximum
What will be the output of the following Python code?1. def foo():2. try:3. print(1)4. finally:5. print(2)6. 7. foo()
What will be the output of the following Python code?def maximum(x, y): if x > y: return x elif x == y: return 'The numbers are equal' else: return yprint(maximum(2, 3))
What will be the output of the following Python code?def foo(k): k = [1]q = [0]foo(q)print(q) [0][1][1, 0][0, 1]
What will be the output of the following code?arr=[1,4,8,10,18,6,9]print(max(arr), len(arr), min(arr), arr.remove(4), arr.pop())
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.