nerdexam
Python_Institute

PCEP-30-02 · Question #96

What is the expected output of the following code? ``python list1 = ['Peter', 'Paul', 'Mary', 'Jane'] list2 = ['Peter', 'Paul', 'Mary', 'Jane'] print(list1 is list2) print(list1 == list2) list1 =…

The correct answer is B. 1| False 2| True 3| True 4| True. Option B is correct because Python distinguishes between identity (is) and equality (==): initially, list1 and list2 are two separate list objects in memory with identical contents, so is returns False (different objects) while == returns True (same values). After the…

Question

What is the expected output of the following code?
list1 = ['Peter', 'Paul', 'Mary', 'Jane']
list2 = ['Peter', 'Paul', 'Mary', 'Jane']

print(list1 is list2)
print(list1 == list2)

list1 = list2

print(list1 is list2)
print(list1 == list2)

Options

  • A1| False 2| True 3| False 4| True
  • B1| False 2| True 3| True 4| True
  • C1| False 2| True 3| False 4| False
  • D1| False 2| False 3| True 4| True

How the community answered

(37 responses)
  • A
    16% (6)
  • B
    73% (27)
  • C
    8% (3)
  • D
    3% (1)

Explanation

Option B is correct because Python distinguishes between identity (is) and equality (==): initially, list1 and list2 are two separate list objects in memory with identical contents, so is returns False (different objects) while == returns True (same values). After the assignment list1 = list2, both variables point to the exact same object in memory, making is return True - and == also remains True since an object is always equal to itself.

Why the distractors fail: Options A and C both show False for line 3, incorrectly assuming list1 = list2 creates a copy rather than making both names reference the same object. Option C also incorrectly shows False for line 4, which would only occur if the contents differed. Option D incorrectly shows False for line 2, confusing identity with equality - two lists with identical contents are == even if they are not is.

Memory tip: Think of is as asking "same house?" and == as asking "same furniture?" - two houses can have identical furniture (== is True) but still be different houses (is is False). Assignment (list1 = list2) doesn't build a new house; it just gives you a second key to the same house, so both checks become True.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice