nerdexam
Python_Institute

PCEP-30-02 · Question #81

What is the expected output of the following code? list1 = [3, 7, 23, 42] list2 = [3, 7, 23, 42] print(list1 is list2) print(list1 == list2)

The correct answer is C. 1|False 2|True. Option C is correct because Python creates two separate list objects in memory when you write list1 = [3, 7, 23, 42] and list2 = [3, 7, 23, 42], so is (which checks object identity - whether both names point to the exact same memory address) returns False; meanwhile == (which…

Question

What is the expected output of the following code? list1 = [3, 7, 23, 42] list2 = [3, 7, 23, 42] print(list1 is list2) print(list1 == list2)

Options

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

How the community answered

(24 responses)
  • A
    4% (1)
  • B
    13% (3)
  • C
    75% (18)
  • D
    8% (2)

Explanation

Option C is correct because Python creates two separate list objects in memory when you write list1 = [3, 7, 23, 42] and list2 = [3, 7, 23, 42], so is (which checks object identity - whether both names point to the exact same memory address) returns False; meanwhile == (which checks value equality - whether the contents match) returns True since both lists contain identical elements.

  • A (False/False) is wrong because == compares values, not identity, so two lists with the same elements always return True from ==.
  • B (True/True) is wrong because is would only be True if list2 = list1 (an alias), not when each is independently created.
  • D (True/False) has both answers backwards - it confuses what is and == each test.

Memory tip: Think of is as asking "are these the same person?" and == as asking "do these people look identical?" - twins look the same (== is True) but are not the same person (is is False).

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice