Sample questions
Question 1Easy
What is the output?
x = 5
y = x
x += 1
print(x, y)
- 5 5
- 6 6
- 6 5
- 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
- Y gets the integer 5, and x refers to the same 5.
- X += 1 rebinds x to a new integer 6.
- Y still refers to 5, because integers cannot be changed.
- 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, 2, 3] [1, 2]
- [1, 2] [1, 2, 3]
- [1, 2] [1, 2]
- [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
- B and a start as the same list.
- B plus [3] builds a new list [1, 2, 3] and rebinds b to it.
- A still refers to the original list [1, 2].
- 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)
- True zbc abc
- True zbc zbc
- False zbc abc
- True abc abc
Answer: A. True zbc abc
Explanation
replace returns a new string and the original stays "abc".
Step-by-step solution
- Id of a equals id of a, so the first value is True.
- Replace returns a new string zbc, so it prints zbc.
- The original string a is immutable and still reads abc.
- 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)
- None
- [3, 1, 2]
- [1, 2, 3]
- [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
- B and the original a point to the same list [3, 1, 2].
- a = sorted(a) builds a new sorted list and rebinds a to it.
- The original list is not changed, so b still holds [3, 1, 2].
- 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, 9]
- [1] [1]
- [1, 9] [1]
- [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
- B is a shallow copy, so its inner lists are the same objects as a's.
- C is a deep copy, so its inner lists are separate.
- Append 9 changes a[0], which b shares, but not c's copy.
- 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?
- There is no difference for lists
- copy.copy() copies inner lists; deepcopy() only copies the outer list
- copy.copy() shares the inner lists; deepcopy() recursively copies them
- 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
- copy.copy makes a new outer list, but its inner lists are the same objects.
- copy.deepcopy copies the outer list and the inner lists, recursively.
- So changing an inner list in a shallow copy shows up in the original.
- 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, 2, 3, 4]
- [1, 2]
- [1, 2, 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
- list(a) makes a new shallow copy, and a.copy() makes another one.
- Append 3 changes only b, and append 4 changes only c.
- A was never changed, so it still holds [1, 2].
- 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?
- Strings are immutable, so their hash never changes
- Lists are stored on the stack
- Strings are compared by identity only
- 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
- A dict key needs a hash that stays the same while it is stored.
- Strings are immutable, so their hash never changes.
- Lists can change after insertion, so they cannot be hashed safely.
- 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)
- [[7]]
- [1, 2, 3]
- [7]
- [7, 2, 3]
Answer: C. [7]
Explanation
Slice assignment a[:] = ... mutates the existing list object, which b also refers to.
Step-by-step solution
- B and a refer to the same list.
- a[:] = [7] replaces all the items of that same list object with 7.
- Since b refers to that object, b also shows [7].
- 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?
- Always by value: the function gets a deep copy
- By object reference: the parameter is bound to the same object, and rebinding it does not affect the caller
- Immutable objects by reference, mutable objects by value
- 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
- Python passes each argument as a reference to the same object.
- Changing a mutable object inside the function is visible to the caller.
- Rebinding the parameter to a new object does not change the caller's variable.
- 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 →