PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 17


What is the expected output of the following snippet?

Show Answer
Correct Answer: B

B

Discussion

9 comments
Sign in to comment
WorkingDaddyOption: B
Jul 14, 2020

Given answer, B, is correct. But keep in mind too that even if the 'for' statement is corrected, the string is immutable, so assigning a new value to s[i] will fail with "'str' object does not support item assignment. HTH

N9Option: B
Sep 3, 2022

String is immutable

premaseemOption: B
Aug 8, 2020

>>> s="abc" >>> for s in len(s): ... s[i] = s[i].upper() ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable

chxzqwOption: B
Apr 23, 2021

Even if the code were s="abc" for i in range(len(s)): >>>>s[i] = s[i].upper() print(s) it would still throw error as below: Traceback (most recent call last): File "<string>", line 5, in <module> TypeError: 'str' object does not support item assignment which was my initial thought

rbishunOption: B
Oct 15, 2021

[TypeError: 'int' object is not iterable] is returned because len(s) is an int - you can't iterate an int - it's not a collection data type.

macxszOption: B
May 3, 2022

should be for i in range(len... B. The code will cause a runtime exception

pincholincoOption: B
Nov 28, 2023

It causes an exception because the string s is imutable so attempting to assign to s[i] will fail, however s[i].upper() will succesed

aed910cOption: C
Jan 12, 2024

strings can actually be modified by this type of loop. But the loop itself doesn't work, because it's just one number, 3. If it was iterable, it would have worked. Try running this: s='abc' for i in s: s=s.upper() print(s) s is iterable, so the result is 'ABC'

smarty_arseOption: B
Jan 21, 2022

Yes, Type Error. Answer is B