nerdexam
Python_Institute

PCEP-30-02 · Question #34

What is the expected output of the following code? ``python list = ['Peter', 'Paul', 'Mary'] def list(data): del data[1] data[1] = 'Jane' return data print(list(list)) ``

The correct answer is A. The code is erroneous. Option A is correct because the variable name list is first assigned to the list ['Peter', 'Paul', 'Mary'], but the subsequent def list(data): overwrites that name, rebinding list to the function. By the time print(list(list)) executes, list refers only to the function, so…

Question

What is the expected output of the following code?
list = ['Peter', 'Paul', 'Mary']
def list(data):
 del data[1]
 data[1] = 'Jane'
 return data
print(list(list))

Options

  • AThe code is erroneous.
  • B['Peter', 'Paul', 'Mary']
  • C['Peter', 'Jane']
  • D['Paul', 'Mary', 'Jane']

How the community answered

(40 responses)
  • A
    85% (34)
  • B
    5% (2)
  • C
    3% (1)
  • D
    8% (3)

Explanation

Option A is correct because the variable name list is first assigned to the list ['Peter', 'Paul', 'Mary'], but the subsequent def list(data): overwrites that name, rebinding list to the function. By the time print(list(list)) executes, list refers only to the function, so list(list) passes the function itself as data; then del data[1] attempts to delete an index from a function object, which raises a TypeError at runtime.

Option C is the most tempting distractor - if the function had received the original list, del data[1] would remove 'Paul' (leaving ['Peter', 'Mary']) and data[1] = 'Jane' would produce ['Peter', 'Jane'], but that call never succeeds. Options B and D are both wrong for the same root reason: the code never reaches a valid return, let alone preserves or rearranges the original list intact.

Memory tip: Think of Python names as sticky labels - def list(...) peels the label off the list and slaps it onto the function, so the original list becomes unreachable. Whenever you see a user-defined name colliding with an earlier assignment (or a built-in like list, dict, str), expect shadowing bugs.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice