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

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

What is the output of the following snippet?

  • A. 12
  • B. (2, 1)
  • C. (1, 2)
  • D. 21
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

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
consultsk
Highly Voted 7 months ago
Selected Answer: D
To determine the output of this code, let's go through it step by step: dct = {} creates an empty dictionary. dct['1'] = (1, 2) adds a key-value pair to the dictionary. The key is the string '1', and the value is the tuple (1, 2). dct['2'] = (2, 1) adds another key-value pair. The key is the string '2', and the value is the tuple (2, 1). The for loop iterates over the keys of the dictionary using dct.keys(). For each key x, it prints dct[x][1] with end=''. This means it's printing the second element of each tuple (index 1), and the end='' argument prevents a newline after each print. So, when we iterate over the keys: For key '1', it will print 2 (the second element of (1, 2)) For key '2', it will print 1 (the second element of (2, 1)) These will be printed on the same line because of end=''. Therefore, the output will be: 21
upvoted 6 times
...
Knight82
Most Recent 2 weeks, 5 days ago
Selected Answer: D
I see what the program is doing. The other explanation is kind of confusing. So the program is doing the following: It creates an empty dictionary. So its format is "x":"other value here". So, the code creates a key and a value: dct["1"]=(1,2) = dct={ "1":"1,2"} dct["2"]=(2,1) = dct={"2":"2,1"} Thus, dct = {"1": "1,2" , "2" : "1,2"} for each key in dct: print( the first element of each key in dct) so key "1" = 2(first element) and key 2 = 1(first element) which gives us 21 here.
upvoted 1 times
...
megan_mai
8 months, 1 week ago
Selected Answer: D
dct = {} dct['1'] = (1,2) #second element of this key is 2 dct['2'] = (2,1) #second element of this key is 1 for x in dct.keys(): print(dct[x][1], end='') #print out the second element of each key continuously #>>> the answer is 21 (D)
upvoted 2 times
...
christostz03
8 months, 2 weeks ago
d is the correct answer
upvoted 1 times
...
froster02
9 months, 2 weeks ago
answer : 21 2 is of the dct['1'] and 1 is of dct['2']
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