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 122 discussion

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

What is the expected behavior of the following code?

  • A. it outputs 321
  • B. it outputs 123
  • C. it outputs 6
  • D. it raises an exception
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

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
zantrz
9 months, 2 weeks ago
Selected Answer: C
string = "123": This line initializes a string variable named string with the value "123". dummy = 0: This line initializes an integer variable named dummy with the value 0. for character in reversed(string):: This line iterates through the characters of the string string in reverse order using the reversed() function. dummy += int(character): Inside the loop, each character of the reversed string is converted to an integer using the int() function, and then added to the dummy variable. print(dummy): Finally, after the loop finishes, the value of dummy is printed. So, when you run this code, it adds the integer values of each character in the string "123" (which are 1, 2, and 3) and prints the sum, which is 6.
upvoted 1 times
...
macxsz
2 years, 6 months ago
Selected Answer: C
C. it outputs 6
upvoted 1 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 ...