LearnFrenzy
Sign in Take a free test

Strings · Exercise 3 of 4

Strings Find the Error

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

Each Python program has a string mistake: a syntax error, an exception at run time, or a logical bug such as testing find() with if. 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
word = "python"
print("Last letter:", word[len(word)])
A TypeError: len() returns a str
B IndexError: valid indexes are 0 to len(word) - 1; use word[-1] or word[len(word) - 1]
C It prints Last letter: n
D SyntaxError: nested brackets
Question page & discussion
Question 2 Easy

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

main.py
otp_digits = [4, 8, 2, 1]
print("OTP:", "".join(otp_digits))
A TypeError: join() accepts only strings; use "".join(map(str, otp_digits))
B ValueError: the list is too short
C It prints OTP: 4821
D AttributeError: lists have no join
Question page & discussion
Question 3 Medium

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

main.py
email = "ravi.learnfrenzy.com"
at = email.index("@")
print("User:", email[:at])
A It prints User: ravi
B IndexError: string index out of range
C It prints User: (an empty name)
D ValueError: index() raises when the substring is absent; check "@" in email first, or use find() and test for -1
Question page & discussion
Question 4 Easy

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

main.py
print("She said "hello" to me")
A print cannot print quotes
B hello must be a variable
C The inner double quotes end the string early; use single quotes outside or escape them as \"
D Strings cannot contain spaces
Question page & discussion
Question 5 Easy

The program should print the name in capitals. Which statement correctly identifies the bug?

main.py
name = "meera"
name.upper()
print("Welcome,", name)
A upper() only works on single letters
B print should use an f-string
C upper() returns a new string, and its result is thrown away; write name = name.upper()
D name must be declared as str
Question page & discussion
Question 6 Hard

The text starts with "python", yet the program reports it as missing. Which statement correctly identifies the bug?

main.py
text = "python rocks"
if text.find("python"):
    print("found")
else:
    print("not found")
A find() is case-sensitive
B The if needs brackets
C find() only searches from the end
D find() returns 0 for a match at the start, and 0 is falsy (while -1 for "absent" is truthy); use "python" in text
Question page & discussion
Question 7 Medium

The sentence has 3 words, but the program counts 6. Which statement correctly identifies the bug?

main.py
sentence = "Learn   Python  daily"
words = sentence.split(" ")
print("Words:", len(words))
A len() counts characters
B split(" ") splits at every single space, so runs of spaces produce empty strings; use split() with no argument
C The sentence must be stripped first
D split() needs a maximum count
Question page & discussion
Question 8 Medium

"Madam" should be reported as a palindrome. Which statement correctly identifies the bug?

main.py
word = "Madam"
if word == word[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")
A [::-1] does not reverse strings
B == cannot compare strings
C The word is too short
D The comparison is case-sensitive: "Madam" reversed is "madaM"; compare word.lower() with its reverse
Question page & discussion
Question 9 Easy

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

main.py
code = "LF"
code.append("2026")
print(code)
A TypeError: cannot add numbers
B It prints LF2026
C NameError: code is not defined
D AttributeError: str has no append() method; strings are immutable – use code = code + "2026"
Question page & discussion
Question 10 Medium

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

main.py
width = 30 / 2
print("=" * width)
A It prints 15 = signs
B ValueError: width is too large
C TypeError: a string can be repeated only an int number of times, and 30 / 2 is the float 15.0; use 30 // 2
D SyntaxError: * needs spaces
Question page & discussion
Up next · Exercise 4Strings Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links