exam questions

Exam PCEP-30-02 All Questions

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

Exam PCEP-30-02 topic 4 question 28 discussion

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

What is the expected output of the following code?

  • A. 1 | 1. x: 42
    2 | 2. x: 42
    3 | 3. x: 42
  • B. None of the above.
  • C. 1 | 1. x: 42
    2 | 2. x: 23
    3 | 3. x: 23
  • D. 1 | 1. x: 42
    2 | 2. x: 23
    3 | 3. x: 42
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
Donny_575
1 month, 1 week ago
Selected Answer: C
Line 1: x = 42 A global variable x is set to 42. Line 4–8: Function Definition def func(): global x # Refers to the global 'x' global x tells Python: “When I refer to x in here, I mean the one outside the function.” print('1. x:', x) x = 23 # Modifies the global variable print('2. x:', x) Line 11: func() #Calls func(): Line 6: #prints 1. x: 42 Line 7: #sets x = 23 (updates the global x) Line 8: # prints 2. x: 23 Line 12: print('3. x:', x) #Prints the updated value of global x, which is now 23 Final Output: 1. x: 42 2. x: 23 3. x: 23
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