LearnFrenzy
Sign in Take a free test

Tuples · Exercise 3 of 4

Tuples Find the Error

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

Each Python program has a tuple mistake: an exception at run time, or a logical bug such as a missing comma. 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
resolution = (1920, 1080)
resolution[0] = 1280
print(resolution)
A IndexError: index 0 out of range
B It prints (1280, 1080)
C TypeError: tuples are immutable; build a new tuple: resolution = (1280, resolution[1])
D ValueError: 1280 is too small
Question page & discussion
Question 2 Easy

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

main.py
weekdays = ("Mon", "Tue", "Wed")
weekdays.append("Thu")
print(weekdays)
A TypeError: strings cannot be appended
B It prints the four days
C AttributeError: tuple has no append(); use weekdays + ("Thu",) or a list
D IndexError
Question page & discussion
Question 3 Easy

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

main.py
record = ("Asha", 21, "CSE")
name, age = record
print(name, age)
A ValueError: the tuple has 3 items but only 2 names; add a name or use name, age, *rest = record
B It prints Asha 21
C TypeError: record is a tuple
D IndexError
Question page & discussion
Question 4 Medium

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

main.py
line = "Ravi,78"
name, marks, grade = line.split(",")
print(name, marks, grade)
A It prints Ravi 78 None
B ValueError: split gives 2 values but 3 names are waiting
C TypeError: split returns a list
D IndexError: list index out of range
Question page & discussion
Question 5 Medium

The program should print the single colour "red". Which statement correctly identifies the bug?

main.py
colors = ("red")
for c in colors:
    print(c)
A for cannot loop over tuples
B ("red") is just the string "red" in brackets, so the loop walks its characters; write ("red",)
C print adds newlines between letters
D The brackets must be square
Question page & discussion
Question 6 Medium

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

main.py
routes = {}
routes[("Delhi", ["Agra", "Jaipur"])] = 514
print(routes)
A KeyError: Delhi
B It prints the dictionary
C ValueError: key too long
D TypeError: unhashable type 'list' – a tuple used as a key may contain only hashable items; use ("Delhi", ("Agra", "Jaipur"))
Question page & discussion
Question 7 Easy

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

main.py
scores = (72, 85)
scores = scores + 91
print(scores)
A It prints (72, 85, 91)
B It prints (163, 176)
C TypeError: a tuple can only be concatenated with another tuple; write scores + (91,)
D AttributeError
Question page & discussion
Question 8 Easy

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

main.py
ranks = ("Asha", "Ravi", "Meera")
print("Kiran is rank", ranks.index("Kiran") + 1)
A It prints Kiran is rank 0
B It prints Kiran is rank -1
C ValueError: index() raises when the value is absent; check "Kiran" in ranks first
D IndexError
Question page & discussion
Question 9 Medium

The scores, once sorted, should match the expected tuple. Which statement correctly identifies the bug?

main.py
scores = (85, 72, 91)
expected = (72, 85, 91)
print("match" if sorted(scores) == expected else "no match")
A sorted() sorts in descending order
B Tuples cannot be compared with ==
C sorted() returns a list, and a list never equals a tuple; compare tuple(sorted(scores)) == expected
D expected is not sorted
Question page & discussion
Question 10 Hard

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

main.py
team = ("Alpha", ["Asha", "Ravi"])
team[1] += ["Meera"]
print(team)
A TypeError: += extends the list but then tries to assign it back to team[1], which the tuple forbids; use team[1].append("Meera")
B It prints ('Alpha', ['Asha', 'Ravi', 'Meera'])
C AttributeError: tuples have no +=
D IndexError
Question page & discussion
Up next · Exercise 4Tuples Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links