exam questions

Exam PCEP-30-02 All Questions

View all questions & answers for the PCEP-30-02 exam

Exam PCEP-30-02 topic 1 question 78 discussion

Actual exam question from Python Institute's PCEP-30-02
Question #: 78
Topic #: 1
[All PCEP-30-02 Questions]

What is the expected output of the following code?

  • A. 3
  • B. The code is erroneous.
  • C. 6
  • D. 7
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
hovnival
1 month, 1 week ago
Selected Answer: C
The expected output of the given code is 6. Here's a step-by-step explanation of how the code works: An empty dictionary data is created. The key 1 is assigned the value 1, so data becomes {1: 1}. The key '1' (a string) is assigned the value 2, so data becomes {1: 1, '1': 2}. The key 1.0 (a float) is assigned the value 4. In Python, the float 1.0 is considered equal to the integer 1, so this updates the value for the key 1 to 4. Now data becomes {1: 4, '1': 2}. The dictionary data now contains two key-value pairs: {1: 4, '1': 2}. The for loop iterates over the keys of the dictionary: For the first key 1, it adds data[1] (which is 4) to res, so res becomes 4. For the second key '1', it adds data['1'] (which is 2) to res, so res becomes 6. Finally, the print(res) statement 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 ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago