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

Exam PCAP All Questions

View all questions & answers for the PCAP exam

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 (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
WorkingDaddy
Highly Voted 4 years, 4 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 10 months, 2 weeks 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
1 year 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
2 years, 2 months ago
Selected Answer: B
String is immutable
upvoted 4 times
...
macxsz
2 years, 7 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, 10 months ago
Yes, Type Error. Answer is B
upvoted 1 times
...
rbishun
3 years, 1 month 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
3 years, 7 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
4 years, 3 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 ...