LearnFrenzy
Sign in Take a free test

Lists · Exercise 3 of 4

Lists Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
undo_stack = ["type A"]
undo_stack.pop()
undo_stack.pop()
print("Undone twice")
A ValueError: "type A" not in list
B It prints Undone twice
C TypeError: pop() needs an index
D IndexError: the second pop() runs on an empty list; check if undo_stack: first
Question page & discussion
Question 2 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
guests = ["Asha", "Ravi"]
guests.remove("Meera")
print(guests)
A IndexError: list index out of range
B It prints ['Asha', 'Ravi']
C ValueError: remove() raises when the value is not in the list; check "Meera" in guests first
D KeyError: Meera
Question page & discussion
Question 3 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
marks = [78, "91", 85]       # one mark was read as text
marks.sort()
print(marks)
A It prints [78, 85, '91']
B ValueError: invalid literal
C It prints ['91', 78, 85]
D TypeError: "<" is not supported between str and int; convert first, e.g. marks = [int(m) for m in marks]
Question page & discussion
Question 4 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
marks = [72, 95, 58]
marks = marks.sort()
print("Lowest:", marks[0])
A IndexError: the list is empty
B It prints Lowest: 58
C TypeError: sort() returns None, so marks becomes None and cannot be indexed; call marks.sort() alone or use sorted()
D AttributeError: sort is not a method
Question page & discussion
Question 5 Easy

The cart should have 4 items after adding the new ones. Which statement correctly identifies the bug?

main.py
cart = ["pen", "book"]
new_items = ["bag", "ink"]
cart.append(new_items)
print(len(cart), cart)
A len() counts nested lists twice
B append() adds the whole list as one item; use cart.extend(new_items) or cart += new_items
C new_items must be a tuple
D append() needs an index
Question page & discussion
Question 6 Hard

Booking seat (0, 1) should mark only one seat. Which statement correctly identifies the bug?

main.py
seats = [["-"] * 3] * 2
seats[0][1] = "X"
for row in seats:
    print(" ".join(row))
A join() copies the X
B [row] * 2 repeats a reference to the same row list; build rows with [["-"] * 3 for _ in range(2)]
C Strings are shared between lists
D The index should be seats[1][0]
Question page & discussion
Question 7 Medium

The program should print the last three readings. Which statement correctly identifies the bug?

main.py
readings = [31, 29, 33, 35, 30, 28]
print(readings[-3:-1])
A Negative indexes count from the start
B The stop index -1 excludes the last item; write readings[-3:]
C Slices cannot use negative numbers
D It should be readings[3:-1]
Question page & discussion
Question 8 Easy

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
primes = [2, 3 5, 7]
print(primes)
A Items in a list must be separated by commas: there is none between 3 and 5
B Lists cannot contain prime numbers
C The list needs round brackets
D It prints [2, 35, 7]
Question page & discussion
Question 9 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
sorted_marks = [55, 62, 70, 81, 93]
median = sorted_marks[len(sorted_marks) / 2]
print("Median:", median)
A IndexError: index 2.5 out of range
B It prints Median: 70
C TypeError: list indices must be integers, but len(...) / 2 is the float 2.5; use // for an int index
D ValueError: odd length
Question page & discussion
Question 10 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
history = []
history.push("home")
print(history)
A TypeError: history is empty
B It prints ['home']
C NameError: push is not defined
D AttributeError: Python lists have no push(); use append()
Question page & discussion
Up next · Exercise 4Lists Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links