Mutability & Aliasing Questions with Answers

29 practice questions on Mutability & Aliasing (Python) with answers and step-by-step solutions. Try 10 samples below, then practise all 29 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

x = 5
y = x
x += 1
print(x, y)
  1. 5 5
  2. 6 6
  3. 6 5
  4. 5 6

Answer: C. 6 5

Explanation

Integers are immutable; x += 1 rebinds x to a new object and y keeps referring to 5.

Step-by-step solution

  1. Y gets the integer 5, and x refers to the same 5.
  2. X += 1 rebinds x to a new integer 6.
  3. Y still refers to 5, because integers cannot be changed.
  4. The output is 6 5.

Why the other options are wrong

  • A. X was rebound to 6, so it is not 5.
  • B. Y still refers to 5, so it is not 6.
  • D. X is 6 and y is 5, the reverse of this option.

Tip: Integers and strings are immutable, so += rebinds the name instead of changing the object.

Question 2Easy

What is the output?

a = [1, 2]
b = a
b = b + [3]
print(a, b)
  1. [1, 2, 3] [1, 2]
  2. [1, 2] [1, 2, 3]
  3. [1, 2] [1, 2]
  4. [1, 2, 3] [1, 2, 3]

Answer: B. [1, 2] [1, 2, 3]

Explanation

b + [3] creates a new list and rebinds b, leaving a unchanged.

Step-by-step solution

  1. B and a start as the same list.
  2. B plus [3] builds a new list [1, 2, 3] and rebinds b to it.
  3. A still refers to the original list [1, 2].
  4. The output is [1, 2] [1, 2, 3].

Why the other options are wrong

  • A. A is unchanged, so it stays [1, 2], and b holds the new list.
  • C. B is rebound to the new list with 3, so it is not [1, 2].
  • D. A is not changed by the plus operation, so it is not [1, 2, 3].

Tip: Plus on lists builds a new list, while += changes the list in place.

Question 3Easy

What is the output?

a = "abc"
print(id(a) == id(a), a.replace("a", "z"), a)
  1. True zbc abc
  2. True zbc zbc
  3. False zbc abc
  4. True abc abc

Answer: A. True zbc abc

Explanation

replace returns a new string and the original stays "abc".

Step-by-step solution

  1. Id of a equals id of a, so the first value is True.
  2. Replace returns a new string zbc, so it prints zbc.
  3. The original string a is immutable and still reads abc.
  4. The output is True zbc abc.

Why the other options are wrong

  • B. Replace does not change a, so the last value is abc, not zbc.
  • C. Id of a is the same object, so the first value is True.
  • D. Replace does change the replaced text in its result, so the middle value is zbc.

Tip: String methods like replace return a new string and never change the original.

Question 4Easy

What is the output?

a = [3, 1, 2]
b = a
a = sorted(a)
print(b)
  1. None
  2. [3, 1, 2]
  3. [1, 2, 3]
  4. [2, 1, 3]

Answer: B. [3, 1, 2]

Explanation

sorted() returns a new list that is bound to a; b still refers to the original unsorted list.

Step-by-step solution

  1. B and the original a point to the same list [3, 1, 2].
  2. a = sorted(a) builds a new sorted list and rebinds a to it.
  3. The original list is not changed, so b still holds [3, 1, 2].
  4. The output is [3, 1, 2].

Why the other options are wrong

  • A. Sorted returns a list, and print shows the list that b refers to.
  • C. Only a was rebound to the sorted list; b still holds the unsorted original.
  • D. The order [2, 1, 3] is not what the original list holds.

Tip: Sorted returns a new list; the old list stays the same for any other name that refers to it.

Question 5Medium

What is the output?

import copy
a = [[1], [2]]
b = copy.copy(a)
c = copy.deepcopy(a)
a[0].append(9)
print(b[0], c[0])
  1. [1] [1, 9]
  2. [1] [1]
  3. [1, 9] [1]
  4. [1, 9] [1, 9]

Answer: C. [1, 9] [1]

Explanation

A shallow copy shares the inner lists with the original; a deep copy duplicates them.

Step-by-step solution

  1. B is a shallow copy, so its inner lists are the same objects as a's.
  2. C is a deep copy, so its inner lists are separate.
  3. Append 9 changes a[0], which b shares, but not c's copy.
  4. So b[0] is [1, 9] and c[0] is [1].

Why the other options are wrong

  • A. B shares the inner list with a, so b[0] also gets the 9.
  • B. B's first inner list is shared with a, so it shows 9 too.
  • D. C is a deep copy, so its first inner list stays [1].

Tip: copy.copy duplicates only the outer list; copy.deepcopy duplicates the inner items too.

Question 6Medium

What is the difference between copy.copy() and copy.deepcopy() for a list of lists?

  1. There is no difference for lists
  2. copy.copy() copies inner lists; deepcopy() only copies the outer list
  3. copy.copy() shares the inner lists; deepcopy() recursively copies them
  4. deepcopy() only works on dictionaries

Answer: C. copy.copy() shares the inner lists; deepcopy() recursively copies them

Explanation

A shallow copy builds a new outer container holding the same element references; a deep copy recursively duplicates nested objects.

Step-by-step solution

  1. copy.copy makes a new outer list, but its inner lists are the same objects.
  2. copy.deepcopy copies the outer list and the inner lists, recursively.
  3. So changing an inner list in a shallow copy shows up in the original.
  4. The correct difference is option C.

Why the other options are wrong

  • A. There is a difference: deepcopy copies the inner lists and copy does not.
  • B. The description is reversed; copy.copy shares inner lists and deepcopy copies them.
  • D. Deepcopy works on any object, not just dictionaries.

Tip: Use copy.copy for one level; use copy.deepcopy when nested objects must be separate.

Question 7Medium

What is the output?

a = [1, 2]
b = list(a)
c = a.copy()
b.append(3)
c.append(4)
print(a)
  1. [1, 2, 3, 4]
  2. [1, 2]
  3. [1, 2, 4]
  4. [1, 2, 3]

Answer: B. [1, 2]

Explanation

list(a) and a.copy() both make new (shallow) lists, so appending to them leaves a unchanged.

Step-by-step solution

  1. list(a) makes a new shallow copy, and a.copy() makes another one.
  2. Append 3 changes only b, and append 4 changes only c.
  3. A was never changed, so it still holds [1, 2].
  4. The output is [1, 2].

Why the other options are wrong

  • A. Appending to b and c does not add anything to a.
  • C. A was not changed, so it does not get the 4 from c.
  • D. A was not changed, so it does not get the 3 from b.

Tip: list(a) and a.copy() both make new lists, so changes to the copy do not reach the original.

Question 8Medium

Why are Python strings safe to use as dict keys while lists are not?

  1. Strings are immutable, so their hash never changes
  2. Lists are stored on the stack
  3. Strings are compared by identity only
  4. Strings are shorter than lists

Answer: A. Strings are immutable, so their hash never changes

Explanation

A key's hash must stay constant while it is in the dict; mutable lists could change after insertion, so they are unhashable.

Step-by-step solution

  1. A dict key needs a hash that stays the same while it is stored.
  2. Strings are immutable, so their hash never changes.
  3. Lists can change after insertion, so they cannot be hashed safely.
  4. So the correct reason is option A.

Why the other options are wrong

  • B. Where a list lives in memory is not the reason it cannot be a key.
  • C. Strings are compared by value, not identity only.
  • D. Length does not decide whether a value can be a key.

Tip: Only immutable values, such as strings, numbers and tuples of them, can be dict keys.

Question 9Hard

What is the output?

a = [1, 2, 3]
b = a
a[:] = [7]
print(b)
  1. [[7]]
  2. [1, 2, 3]
  3. [7]
  4. [7, 2, 3]

Answer: C. [7]

Explanation

Slice assignment a[:] = ... mutates the existing list object, which b also refers to.

Step-by-step solution

  1. B and a refer to the same list.
  2. a[:] = [7] replaces all the items of that same list object with 7.
  3. Since b refers to that object, b also shows [7].
  4. The output is [7].

Why the other options are wrong

  • A. The slice assignment puts 7 in the list, so there is no nested list.
  • B. a[:] changes the shared list, so b is not unchanged.
  • D. All three items are replaced, so 2 and 3 are gone.

Tip: a[:] = ... changes the list in place, so every name for it sees the change.

Question 10Hard

In Python, how are arguments passed to functions?

  1. Always by value: the function gets a deep copy
  2. By object reference: the parameter is bound to the same object, and rebinding it does not affect the caller
  3. Immutable objects by reference, mutable objects by value
  4. By reference: assigning to a parameter changes the caller's variable

Answer: B. By object reference: the parameter is bound to the same object, and rebinding it does not affect the caller

Explanation

Often called pass-by-assignment: mutating a passed mutable object is visible to the caller, but assigning a new object to the parameter is not.

Step-by-step solution

  1. Python passes each argument as a reference to the same object.
  2. Changing a mutable object inside the function is visible to the caller.
  3. Rebinding the parameter to a new object does not change the caller's variable.
  4. So the correct answer is option B.

Why the other options are wrong

  • A. Python does not make a deep copy when calling a function.
  • C. All arguments are passed by object reference, not split by mutability.
  • D. Assigning to a parameter does not change the caller's variable.

Tip: Python passes object references: mutation is visible, but rebinding the parameter is not.

Practise all 29 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics