nerdexam
Python_Institute

PCEP-30-02 · Question #32

What is the expected output of the following code? ``python data = ['Peter', 'Paul', 'Mary'] print(data[int(-1 / 2)]) ``

The correct answer is E. Peter. -1 / 2 in Python 3 performs true (float) division, yielding -0.5, and int(-0.5) truncates toward zero - giving 0, not -1. So data[0] returns 'Peter', making E correct. Why the distractors fail: A (Paul) - data[1] requires an index of 1, not 0. B (Mary) - data[-1] would give…

Question

What is the expected output of the following code?
data = ['Peter', 'Paul', 'Mary']
print(data[int(-1 / 2)])

Options

  • APaul
  • BMary
  • CThe code is erroneous.
  • DNone of the above.
  • EPeter

How the community answered

(57 responses)
  • A
    4% (2)
  • B
    12% (7)
  • C
    5% (3)
  • D
    2% (1)
  • E
    77% (44)

Explanation

-1 / 2 in Python 3 performs true (float) division, yielding -0.5, and int(-0.5) truncates toward zero - giving 0, not -1. So data[0] returns 'Peter', making E correct.

Why the distractors fail:

  • A (Paul) - data[1] requires an index of 1, not 0.
  • B (Mary) - data[-1] would give Mary, but that requires int(-0.5) to equal -1, which it doesn't.
  • C (erroneous) - the code runs without error; every operation is valid.
  • D (None of the above) - ruled out because E is a valid match.

Memory tip: Remember the difference between int() and //. Both truncate, but in opposite directions for negatives - int(-0.5) rounds toward zero0, while -1 // 2 rounds toward negative infinity-1. On exam questions involving negative index arithmetic, always apply int() first before assuming which list element is accessed.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice