Python Programming - Tricky Questions - Discussion
Discussion Forum : Tricky Questions - General Questions (Q.No. 8)
8.
What will be the output of the following Python code?
def power(x, n=2):
return x ** n
result1 = power(2)
result2 = power(2, 3)
print(result1 + result2)
Answer: Option
Explanation:
The code defines a function called power that calculates the power of a number using the **
operator. In the code, result1
is the result of calling power(2)
, which calculates 2 raised to the power of 2, resulting in 4.
Similarly, result2
is the result of calling power(2, 3)
, which calculates 2 raised to the power of 3, resulting in 8. The expression result1 + result2
calculates the sum of result1 and result2, resulting in 4 + 8 = 12.
Finally, 12 is printed to the console as the output of the code.
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers