nerdexam
Python_Institute

PCEP-30-02 · Question #214

``python def func(x, y=2): num = 1 for i in range(y): num = num * x return num print(func(4)) print(func(4, 4)) `` What is the expected output of the following code?

The correct answer is D. 1 | 16 2 | 256. Option D is correct because func computes x to the power of y - num starts at 1 and is multiplied by x exactly y times through the loop. Calling func(4) uses the default y=2, giving 1 × 4 × 4 = 16; calling func(4, 4) runs the loop 4 times, giving 1 × 4 × 4 × 4 × 4 = 256. Option…

Question

def func(x, y=2):
 num = 1
 for i in range(y):
 num = num * x
 return num

print(func(4))
print(func(4, 4))
What is the expected output of the following code?

Options

  • A1 | 128 2 | 512
  • B1 | 8 2 | 16
  • C1 | 32 2 | 1024
  • D1 | 16 2 | 256

How the community answered

(36 responses)
  • A
    6% (2)
  • B
    3% (1)
  • C
    14% (5)
  • D
    78% (28)

Explanation

Option D is correct because func computes x to the power of y - num starts at 1 and is multiplied by x exactly y times through the loop. Calling func(4) uses the default y=2, giving 1 × 4 × 4 = 16; calling func(4, 4) runs the loop 4 times, giving 1 × 4 × 4 × 4 × 4 = 256.

Option B (8, 16) likely tricks students who think range(y) produces y-1 iterations, or who misread the default as y=1 - they end up one multiplication short. Options A and C (128/512 and 32/1024) introduce values that would require extra loop iterations or a different starting value for num, neither of which the code supports. The distractors all stem from the same trap: miscounting how many times the loop body executes.

Memory tip: range(y) always produces exactly y values (0 through y-1), so a loop for i in range(y) runs precisely y times - think "range y = y iterations," never y-1 or y+1.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice