nerdexam
Python_Institute

PCEP-30-02 · Question #22

``python 1 fruits1 = ['Apple', 'Pear', 'Banana'] 2 fruits2 = fruits1 3 fruits3 = fruits1[:] 4 5 fruits2[0] = 'Cherry' 6 fruits3[1] = 'Orange' 7 8 res = 0 9 10 for i in (fruits1, fruits2, fruits3)…

The correct answer is B. 12. Option B (12) is correct because fruits2 = fruits1 creates an alias (both variables point to the same list object), so fruits2[0] = 'Cherry' also changes fruits1[0] to 'Cherry'. The slice fruits1[:] creates an independent copy, so fruits3[1] = 'Orange' only affects fruits3…

Question

1 fruits1 = ['Apple', 'Pear', 'Banana']
2 fruits2 = fruits1
3 fruits3 = fruits1[:]
4 
5 fruits2[0] = 'Cherry'
6 fruits3[1] = 'Orange'
7 
8 res = 0
9 
10 for i in (fruits1, fruits2, fruits3):
11 if i[0] == 'Cherry':
12 res += 1
13 if i[1] == 'Orange':
14 res += 10
15 
16 print(res)

Options

  • A22
  • B12
  • C0
  • D11

How the community answered

(37 responses)
  • A
    3% (1)
  • B
    76% (28)
  • C
    8% (3)
  • D
    14% (5)

Explanation

Option B (12) is correct because fruits2 = fruits1 creates an alias (both variables point to the same list object), so fruits2[0] = 'Cherry' also changes fruits1[0] to 'Cherry'. The slice fruits1[:] creates an independent copy, so fruits3[1] = 'Orange' only affects fruits3. When the loop runs, both fruits1 and fruits2 have 'Cherry' at index 0 (adding 1 + 1 = 2), and only fruits3 has 'Orange' at index 1 (adding 10), giving 2 + 10 = 12.

Why the distractors fail:

  • A (22) assumes 'Orange' appears twice (scoring 20), but it only exists in fruits3 since fruits1/fruits2 still have 'Pear' at index 1.
  • C (0) assumes no conditions are ever met, ignoring that the alias mutation makes the Cherry check trigger twice.
  • D (11) is the most tempting trap - it assumes Cherry only appears once (as if fruits1 and fruits2 were independent), forgetting they share the same object.

Memory tip: Think of = as "pointing two signs at the same house" (alias), while [:] is "building a new identical house next door" (copy) - renovating the alias-linked house changes both signs, but the copy stays untouched.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice