Skip to content

Module 1 Chapter 6 Quiz Answers

Python String and Loop Quiz

Question 1

What does the following Python program print out?

str1 = "Hello"
str2 = 'there'
bob = str1 + str2
print(bob)
  • A. Hello
  • B. there
  • C. 0
  • D. Hellothere
  • E. Hello

Correct Answer: D. Hellothere

🔹 Explanation:
The + operator concatenates (joins) strings in Python. "Hello" + "there" results in "Hellothere".


Question 2

What does the following Python program print out?

x = '40'
y = int(x) + 2
print(y)
  • A. int402
  • B. 42
  • C. 402
  • D. x2

Correct Answer: B. 42

🔹 Explanation:

  • x = '40' stores the string "40".
  • int(x) converts "40" into an integer (40).
  • 40 + 2 gives 42, which is printed.

Question 3

How would you use the index operator [] to print out the letter q from the following string?

x = 'From marquard@uct.ac.za'
  • A. print(x[q])
  • B. print(x[7])
  • C. print(x[8])
  • D. print(x[9])
  • E. print x[-1]

Correct Answer: C. print(x[8])

🔹 Explanation:
Indexing starts from 0 in Python. The 8th index corresponds to the letter ‘q’ in "marquard".


Question 4

How would you use string slicing [:] to print out 'uct' from the following string?

x = 'From marquard@uct.ac.za'
  • A. print(x[14:17])
  • B. print(x[14/17])
  • C. print(x[15:18])
  • D. print(x[14+17])
  • E. print(x[15:3])
  • F. print(x[14:3])

Correct Answer: A. print(x[14:17])

🔹 Explanation:
String slicing extracts part of a string using [start:end]. The substring "uct" starts at index 14 and ends before index 17.


Question 5

What is the iteration variable in the following Python code?

for letter in 'banana':
print(letter)
  • A. in
  • B. 'banana'
  • C. for
  • D. print
  • E. letter

Correct Answer: E. letter

🔹 Explanation:
The iteration variable (letter) takes each character from 'banana' one by one in the loop.


Question 6

What does the following Python code print out?

print(len('banana') * 7)
  • A. bananabananabananabananabananabananabanana
  • B. 42
  • C. banana7
  • D. 0

Correct Answer: B. 42

🔹 Explanation:

  • len('banana') gives 6 (number of characters in 'banana').
  • 6 * 7 = 42.

Question 7

How would you print out the following variable in all uppercase in Python?

greet = 'Hello Bob'
  • A. print(upper(greet))
  • B. puts(greet.ucase);
  • C. print(greet.upper())

Correct Answer: C. print(greet.upper())

🔹 Explanation:
.upper() is a built-in method that converts a string to uppercase.


Question 8

Which of the following is not a valid string method in Python?

  • A. boldface()
  • B. upper()
  • C. lower()
  • D. lstrip()
  • E. startswith()

Correct Answer: A. boldface()

🔹 Explanation:
Python does not have a boldface() method for strings. However, upper(), lower(), lstrip(), and startswith() are valid methods.


Question 9

What will the following Python code print out?

data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
pos = data.find('.')
print(data[pos:pos+3])
  • A. uct
  • B. Sat
  • C. .ma
  • D. mar

Correct Answer: C. .ma

🔹 Explanation:

  • data.find('.') finds the first occurrence of . at position 13.
  • data[13:16] extracts .ma.

Question 10

Which of the following string methods removes whitespace from both the beginning and end of a string?

  • A. split()
  • B. strip()
  • C. wsrem()
  • D. strtrunc()

Correct Answer: B. strip()

🔹 Explanation:

  • strip() removes leading and trailing spaces from a string.
  • split() breaks a string into parts but does not remove spaces.
  • wsrem() and strtrunc() do not exist in Python.