Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
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 10 discussion

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

What will be the output of the following code snippet?

  • A. 3
  • B. 2
  • C. 4
  • D. 1
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
consultsk
1 month, 3 weeks ago
Selected Answer: C
Let's analyze this code step by step: d = {} creates an empty dictionary. d[1] = 1 adds a key-value pair where the key is the integer 1 and the value is 1. d['1'] = 2 adds another key-value pair where the key is the string '1' and the value is 2. d[1] += 1 increments the value associated with the key 1. So now d[1] becomes 2. sum = 0 initializes a variable sum to 0. The for loop for k in d: iterates over the keys in the dictionary. In each iteration, it adds the value associated with the current key to sum. After the loop, sum will contain: 2 (from d[1]) 2 (from d['1']) Therefore, the final value of sum will be 4. print(sum) will output this final value. So, the output of this code will be: 4
upvoted 2 times
...
megan_mai
1 month, 3 weeks ago
Selected Answer: C
d = {} d[1] = 1 >> value of the key integer 1 is integer 1 d['1'] = 2 >> value of the key string '1' is 2 d[1] +=1 >> update the value of the key integer 1 to 1+1 =2 >>> final list is {1: 2, '1': 2} sum = 0 for k in d: sum += d[k] >> sum the values that associate to all the keys >> the answer is 2+2 = 4 (C) print(sum) print(d)
upvoted 1 times
...
christostz03
3 months ago
c is the correct answer
upvoted 1 times
...
froster02
4 months, 1 week ago
d = {} # Create an empty dictionary d d[1] = 1 # Add a key-value pair where key is 1 and value is 1 d['1'] = 2 # Add a key-value pair where key is the string '1' and value is 2 d[1] += 1 # Increment the value associated with the key 1 by 1 {1: 2, '1': 2} sum = 0 # Initialize sum to 0 for k in d: # Iterate over each key in the dictionary d sum = sum + d[k] # Add the value associated with key k to sum
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 ...