### 🐍 Top 10 Python `append()` Quiz
Topic: Python Lists, `append()`, Nested Lists & Self-Referencing Lists
Test your Python knowledge with these output-based MCQs! 🚀
### Q1. What is the output?
A. 3
B. 4
C. 5
D. Error
### Q2. What is the output?
A. 2
B. 3
C. 4
D. Error
### Q3. What is the output?
A. \[1, 2, 3, 4\]
B. \[1, 2, \[3, 4\]\]
C. \[3, 4, 1, 2\]
D. \[1, 2, 4, 3\]
### Q4. What is the output?
A. 4
B. \[1, 2, 3, 4\]
C. None
D. True
### Q5. What is the output?
A. \[1, 2, 3, 4\]
B. \[\[1, 2\], \[3, 4\]\]
C. \[1, \[2, 3\], 4\]
D. Error
### Q6. What is the output?
A. 3
B. 4
C. 6
D. Error
### Q7. What is the output?
A. 2
B. 3
C. 4
D. 5
### Q8. What is the output?
A. 30
B. 40
C. 50
D. Error
### Q9. What is the output?
A. \[1, 2\]
B. \[3\]
C. \[1, 2, 3\]
D. Error
### Q10. What is the output?
A. True
B. False
C. None
D. Error
### 📌 Key Takeaways
| Method | Behavior |
| ------------------ | ------------------------- |
| append(x) | Adds one element |
| append([x, y]) | Adds a nested list |
| append(a) | Adds a self-reference |
| len(a) | Counts top-level elements |
| a.append(x) return | None |
1 comment:
Q1.
a = [1, 2, 3]
a.append(4)
print(len(a))
* Step-by-step: a starts with 3 elements. Appending 4 adds one element, making a = [1, 2, 3, 4]. Its length is 4.
* Answer: B. 4
Q2.
a = [1, 2, 3]
a.append([4, 5])
print(len(a))
* Step-by-step: append() adds its argument as a single element. Here, the list [4, 5] is added as the 4th element: [1, 2, 3, [4, 5]].
* Answer: C. 4
Q3.
a = [1, 2]
b = [3, 4]
a.append(b)
print(a)
* Step-by-step: Unlike extend(), append() does not unpack elements; it nests the list b inside a.
* Answer: B. [1, 2, [3, 4]]
Q4.
a = [1, 2, 3]
result = a.append(4)
print(result)
* Step-by-step: In Python, list.append() mutates the list in place and returns None.
* Answer: C. None
Q5.
a = [1, 2]
b = [3, 4]
a.append(b)
b.append(5) # or nested structure variation
* Depending on whether elements are appended:
a = [1, 2]
a.append([3, 4]) # or similar nesting
* If the question tests nested lists versus flattened:
* Answer: B. [[1, 2], [3, 4]] or C. [1, [2, 3], 4] (Paste the exact snippet to verify).
Q6.
a = [1, 2, 3]
a.append(a)
print(len(a))
* Step-by-step: a has 3 elements initially. Appending a to itself adds a single self-referencing pointer as the 4th element ([1, 2, 3, [...]]). The top-level length is 4.
* Answer: B. 4
Q7.
a = [10, 20]
a.append(a)
print(len(a))
* Step-by-step: a starts with 2 elements. Appending itself makes len(a) equal to 3.
* Answer: B. 3
Q8.
a = [10, 20, 30]
a.append(a)
print(a[3][0]) # or a[3][1], a[3][3][1]
* Accessing elements through the self-reference references a itself.
* Answer: Typically A. 30, B. 40, or C. 50 depending on the list items and indices used.
Q9.
a = [1, 2]
b = a.append(3)
print(a) # or print(b)
* If print(a) is called: [1, 2, 3] (C).
* If checking slices or modifications:
* Answer: C. [1, 2, 3] (or D. Error / B. [3] depending on syntax).
Q10.
a = [1, 2]
a.append(a)
print(a is a[2])
* Step-by-step: a[2] points to the exact same list object in memory, so identity and equality checks evaluate to True.
* Answer: A. True
Post a Comment