Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 17 discussion

Actual exam question from Python Institute's PCAP
Question #: 17
Topic #: 1
[All PCAP Questions]

What is the expected output of the following snippet?

  • A. abc
  • B. The code will cause a runtime exception
  • C. ABC
  • D. 123
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
WorkingDaddy
Highly Voted 3 years, 9 months ago
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
upvoted 22 times
...
aed910c
Most Recent 3 months, 1 week ago
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'
upvoted 2 times
...
pincholinco
4 months, 3 weeks ago
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
upvoted 2 times
...
N9
1 year, 7 months ago
Selected Answer: B
String is immutable
upvoted 4 times
...
macxsz
1 year, 11 months ago
Selected Answer: B
should be for i in range(len... B. The code will cause a runtime exception
upvoted 2 times
...
smarty_arse
2 years, 3 months ago
Yes, Type Error. Answer is B
upvoted 1 times
...
rbishun
2 years, 6 months ago
[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.
upvoted 2 times
...
chxzqw
2 years, 12 months ago
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
upvoted 2 times
...
premaseem
3 years, 8 months ago
>>> 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
upvoted 3 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...