PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Hard
Question 38
Q497 - Control Flow
What is the expected output of the following snippet?
- s = 'abc'
- for i in len(s):
- s[i] = s[i].upper()
- print(s)
-
A
ABC -
B
The code will cause a runtime exception.
-
C
abc -
D
123
Reveal correct answer
Correct answer: B
Explanation
Q497 (Please refer to this number, if you want to write me about this question.)
A.
The code will cause a runtime exception because the len() function returns the length of the string s, which is an integer. The for loop requires an iterable object, such as a list, tuple, or string, to iterate over its elements. Trying to iterate over an integer will result in a TypeError.
B. The code will cause a runtime exception because the len() function returns the length of the string s, which is an integer. The for loop requires an iterable object, such as a list, tuple, or string, to iterate over its elements. Trying to iterate over an integer will result in a TypeError.
C.
The code will cause a runtime exception because the len() function returns the length of the string s, which is an integer. The for loop requires an iterable object, such as a list, tuple, or string, to iterate over its elements. Trying to iterate over an integer will result in a TypeError.
D.
The code will cause a runtime exception because the len() function returns the length of the string s, which is an integer. The for loop requires an iterable object, such as a list, tuple, or string, to iterate over its elements. Trying to iterate over an integer will result in a TypeError.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
