nerdexam
Python_Institute

PCEP-30-02 · Question #83

What is the expected output of the following code? nums = [3, 7, 23, 42] alphas = ['p', 'p', 'm', '3'] print(nums is alphas) print(nums == alphas) nums = alphas print(nums is alphas) print(nums ==…

The correct answer is B. 1|False 2|False 3|True 4|True. Option B is correct because is tests identity (whether two variables point to the same object in memory), while == tests equality (whether two objects have the same values). Initially, nums and alphas are two separate list objects with different contents, so both is (line 1)…

Question

What is the expected output of the following code? nums = [3, 7, 23, 42] alphas = ['p', 'p', 'm', '3'] print(nums is alphas) print(nums == alphas) nums = alphas print(nums is alphas) print(nums == alphas)

Options

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

How the community answered

(39 responses)
  • A
    13% (5)
  • B
    74% (29)
  • C
    8% (3)
  • D
    5% (2)

Explanation

Option B is correct because is tests identity (whether two variables point to the same object in memory), while == tests equality (whether two objects have the same values). Initially, nums and alphas are two separate list objects with different contents, so both is (line 1) and == (line 2) return False. After nums = alphas, both variables reference the same list object, making is return True (line 3) and == also True since an object always equals itself (line 4).

Why the distractors fail:

  • A is wrong because it shows True for line 1 - two separately created lists can never be the same object before reassignment.
  • C is wrong because it shows True for line 2 - [3, 7, 23, 42] and ['p', 'p', 'm', '3'] contain completely different values.
  • D is wrong for the same reason as A: line 1 cannot be True before the reassignment nums = alphas occurs.

Memory tip: Think of is as asking "same address?" and == as asking "same contents?" - assignment (=) makes two names share one address, but two list literals always start at different addresses even if their contents match.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice