nerdexam
Python_Institute

PCEP-30-02 · Question #104

What is the result of the following code? `` 1 x = 7 2 y = x % 2 3 y += 1 4 print(y) ``

The correct answer is B. 2. Option B is correct because 7 % 2 evaluates to 1 (the remainder when 7 is divided by 2), and then y += 1 increments that result by 1, giving a final value of 2. Option A (1) is the most common trap - it reflects stopping after line 2 and forgetting the += 1 on line 3. Option D…

Question

What is the result of the following code?
1 x = 7
2 y = x % 2
3 y += 1
4 print(y)

Options

  • A1
  • B2
  • C5
  • D3

How the community answered

(37 responses)
  • A
    8% (3)
  • B
    70% (26)
  • C
    16% (6)
  • D
    5% (2)

Explanation

Option B is correct because 7 % 2 evaluates to 1 (the remainder when 7 is divided by 2), and then y += 1 increments that result by 1, giving a final value of 2. Option A (1) is the most common trap - it reflects stopping after line 2 and forgetting the += 1 on line 3. Option D (3) confuses the modulo operator with integer division: 7 // 2 would be 3, but % gives the remainder, not the quotient. Option C (5) has no mathematical path from this code and likely results from misreading the operators entirely.

Memory tip: Think of % as "what's left over after dividing" - for any odd number modulo 2, the leftover is always 1, then just watch for any subsequent operations that modify the result before print().

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice