PCEP-30-02 · Question #114
What is the expected output of the following code if the user enters 2 and 4? ``python x = float(input()) y = float(input()) print(y ** (1 / x)) ``
The correct answer is C. 4.0. There appears to be an error in this question's answer key. Let me walk through the actual computation: With x = 2.0 and y = 4.0, the expression evaluates as: `` y (1 / x) = 4.0 (1 / 2.0) = 4.0 0.5 = 2.0 ` 1 / x produces 0.5 (Python 3 true division), and raising 4 to the power…
Question
x = float(input())
y = float(input())
print(y ** (1 / x))
Options
- A1.0
- B2.0
- C4.0
- D0.0
How the community answered
(29 responses)- A10% (3)
- B3% (1)
- C83% (24)
- D3% (1)
Explanation
There appears to be an error in this question's answer key. Let me walk through the actual computation:
With x = 2.0 and y = 4.0, the expression evaluates as:
y ** (1 / x)
= 4.0 ** (1 / 2.0)
= 4.0 ** 0.5
= 2.0
1 / x produces 0.5 (Python 3 true division), and raising 4 to the power of 0.5 is equivalent to the square root of 4, which is 2.0 - option B, not C.
Why each option is wrong:
- A (1.0): Would result from
y ** 0, noty ** 0.5 - C (4.0): Would result if the exponent were
1(i.e.,x = 1), or if the code printedyunchanged - D (0.0): Not achievable with these inputs and this operation
The stated correct answer (C: 4.0) is incorrect. The actual output is 2.0 (option B).
Memory tip: y ** (1/x) means "the x-th root of y." With x=2, it's the square root. Remember: n ** 0.5 == sqrt(n) - a useful Python shorthand to know for exams.
Community Discussion
No community discussion yet for this question.