
This set focuses on Python's three most used sequence types: strings, lists and tuples. Questions cover indexing and slicing with negative steps, common string and list methods, f-string formatting, mutability, aliasing, shallow versus deep copies and sequence unpacking. These topics appear constantly in university practical exams, coding rounds of campus placements and Python developer interviews, where predicting the exact output of a short snippet is a favourite test of understanding.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.
Topics covered in this set
- Indexing and slicing with negative steps
- String methods: find, split, strip, join
- f-string formatting
- List methods: append, extend, sort, pop
- Mutability and aliasing
- Shallow copy vs deep copy
- Tuple behaviour and extended unpacking
Python MCQs with answers and explanations
Q1. What is the output of the following code?
s = "PYTHON" print(s[-4:-1], s[::-2])
a) THON NHY
b) THO NOY
c) HON PTO
d) THO NHY
Q2. What is the output of the following code?
w = "banana"
print(w.find("na"), w.rfind("na"), w.count("an"))
a) 2 4 3
b) 3 5 2
c) 2 2 2
d) 2 4 2
Q3. What is the output of the following code?
a = [1, 2, 3] b = a b += [4] print(a)
a) [1, 2, 3, 4]
b) [1, 2, 3]
c) [1, 2, 3, [4]]
d) TypeError
Q4. What is the output of the following code?
a = [1, 2, 3] b = a b = b + [4] print(a, b)
a) [1, 2, 3, 4] [1, 2, 3, 4]
b) [1, 2, 3] [1, 2, 3, 4]
c) [1, 2, 3] [1, 2, 3]
d) [1, 2, 3, 4] [1, 2, 3]
Q5. What is the output of the following code?
t = (1, [2, 3]) t[1].append(4) print(t)
a) (1, [2, 3, 4])
b) TypeError
c) (1, [2, 3])
d) (1, [2, 3], 4)
Q6. What is the output of the following code?
t = (10, 20, 30) t[0] = 5 print(t)
a) (5, 20, 30)
b) IndexError
c) AttributeError
d) TypeError
Q7. What is the output of the following code?
m = [[0] * 2] * 2 m[0][0] = 1 print(m)
a) [[1, 0], [0, 0]]
b) [[1, 1], [0, 0]]
c) [[1, 0], [1, 0]]
d) [[1, 1], [1, 1]]
Q8. What is the output of the following code?
import copy a = [[1], [2]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) print(b[0], c[0])
a) [1] [1]
b) [1, 9] [1]
c) [1, 9] [1, 9]
d) [1] [1, 9]
Q9. What is the output of the following code?
print(type((5)).__name__, type((5,)).__name__)
a) tuple tuple
b) int tuple
c) int int
d) tuple int
Q10. What is the output of the following code?
x = [3, 1, 2] y = x.sort() print(y, x)
a) [1, 2, 3] [1, 2, 3]
b) [1, 2, 3] [3, 1, 2]
c) None [1, 2, 3]
d) None [3, 1, 2]
Q11. What is the output of the following code?
print(" a,b,,c ".strip().split(","))
a) ['a', 'b', 'c']
b) [' a', 'b', '', 'c ']
c) ['a', 'b', '', 'c']
d) ['a', 'b', ',', 'c']
Q12. What is the output of the following code?
pi = 3.14159
print(f"{pi:.2f}|{42:05d}|{'ab':>4}")
a) 3.14|42000|ab
b) 3.14|00042| ab
c) 3.1|00042| ab
d) 3.14| 42|ab
Q13. What is the output of the following code?
lst = [1, 2, 3, 4, 5] lst[1:3] = [9] print(lst)
a) [1, 9, 3, 4, 5]
b) [1, [9], 4, 5]
c) [1, 9, 4, 5]
d) [1, 9, 9, 4, 5]
Q14. What is the output of the following code?
a = [1, 2, 3] a.append([4, 5]) a.extend([6, 7]) print(len(a))
a) 6
b) 7
c) 5
d) 8
Q15. For a Python list of n elements, which operation runs in amortised O(1) time?
a) lst.append(x)
b) lst.insert(0, x)
c) lst.pop(0)
d) x in lst
Q16. What is the output of the following code?
print([1, 2] * 2 == [1, 2, 1, 2], (1, 2) < (1, 3), "apple" < "Apple")
a) True False False
b) True True False
c) True True True
d) False True False
Q17. What is the output of the following code?
lst = [10, 20, 30, 40] print(lst.pop(), lst.pop(0), lst)
a) 40 10 [10, 20, 30, 40]
b) 10 40 [20, 30]
c) 40 20 [10, 30]
d) 40 10 [20, 30]
Q18. What is the output of the following code?
a, *b, c = "hello" print(b)
a) ['e', 'l', 'l']
b) 'ell'
c) ('e', 'l', 'l')
d) ['e', 'l', 'l', 'o']
Quick revision notes
- Slice s[start:stop:step] excludes stop; s[::-1] reverses a sequence.
- Strings and tuples are immutable; lists are mutable.
- b = a creates an alias, not a copy; a[:], list(a) and copy.copy(a) make shallow copies.
- copy.deepcopy() also copies nested mutable objects.
- list.sort() sorts in place and returns None; sorted() returns a new list.
- += on a list mutates it in place (like extend); a = a + b builds a new list.
- (5) is just an int; a one-element tuple needs a trailing comma: (5,).
- [[0] * n] * m repeats references to the same inner list.
Frequently asked questions
What is the difference between a shallow copy and a deep copy in Python?
A shallow copy creates a new outer container but keeps references to the same inner objects, so changes to nested lists show up in both. A deep copy recursively copies every nested object, so the two structures are fully independent.
Why can a tuple containing a list be modified?
The tuple itself is immutable, meaning it always refers to the same objects. If one of those objects is a mutable list, the list's contents can still change, although the tuple cannot be made to point at a different list.
When should I use a tuple instead of a list?
Use a tuple for fixed collections of values that should not change, such as coordinates or records, and when you need a hashable value for a dictionary key or set element. Use a list when items will be added, removed or reordered.
Comments
Post a Comment