English

Predict the output of the given code snippet. ptr=50 def result(): global ptr ptr=ptr+1 print ptr result() print ptr

Advertisements
Advertisements

Question

Predict the output of the given code snippet.

ptr=50
def result():
     global ptr
     ptr=ptr+1
print ptr
result()
print ptr
Very Short Answer
Advertisements

Solution

Trace:

  • Initially, global ptr is 50.
  • The first print(ptr) displays 50.
  • global ptr allows result() to modify the global variable.
  • result() increases ptr to 51.
50
51
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: Classes in Python - EXERCISE [Page 63]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 3 Classes in Python
EXERCISE | Q 14. (ii) | Page 63
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×