The program below stops with an error. Which statement correctly describes the problem?
undo_stack = ["type A"]
undo_stack.pop()
undo_stack.pop()
print("Undone twice")
pop() needs at least one item. Guard it with if undo_stack:.
Lists · Exercise 3 of 4
Each Python program has a list mistake: a syntax error, an exception at run time, or a logical bug such as append instead of extend. Choose the option that names the real problem.
The program below stops with an error. Which statement correctly describes the problem?
undo_stack = ["type A"]
undo_stack.pop()
undo_stack.pop()
print("Undone twice")
pop() needs at least one item. Guard it with if undo_stack:.
The program below stops with an error. Which statement correctly describes the problem?
guests = ["Asha", "Ravi"]
guests.remove("Meera")
print(guests)
Unlike discard() for sets, list.remove() has no silent version.
The program below stops with an error. Which statement correctly describes the problem?
marks = [78, "91", 85] # one mark was read as text
marks.sort()
print(marks)
Python 3 does not order unrelated types. sorted(marks, key=int) also works.
The program below stops with an error. Which statement correctly describes the problem?
marks = [72, 95, 58]
marks = marks.sort()
print("Lowest:", marks[0])
The error message is 'NoneType' object is not subscriptable.
The cart should have 4 items after adding the new ones. Which statement correctly identifies the bug?
cart = ["pen", "book"]
new_items = ["bag", "ink"]
cart.append(new_items)
print(len(cart), cart)
append always adds exactly one item – here a nested list.
Booking seat (0, 1) should mark only one seat. Which statement correctly identifies the bug?
seats = [["-"] * 3] * 2
seats[0][1] = "X"
for row in seats:
print(" ".join(row))
Both rows are one object. A comprehension creates a fresh row each time.
The program should print the last three readings. Which statement correctly identifies the bug?
readings = [31, 29, 33, 35, 30, 28]
print(readings[-3:-1])
A slice stops before its stop index; leave it out to go to the end.
Python refuses to run the program below. Which statement correctly describes the problem?
primes = [2, 3 5, 7]
print(primes)
Python even suggests "Perhaps you forgot a comma?".
The program below stops with an error. Which statement correctly describes the problem?
sorted_marks = [55, 62, 70, 81, 93]
median = sorted_marks[len(sorted_marks) / 2]
print("Median:", median)
len(sorted_marks) // 2 is 2, giving 70.
The program below stops with an error. Which statement correctly describes the problem?
history = []
history.push("home")
print(history)
push is the JavaScript name. In Python: append to add, pop to remove from the end.
Timed online tests with instant scores and explanations.