Class - 12 CBSE Computer Science Important Output Questions 2025
Predict the output of the following code:
a = 10
y = 5
def myfunc (a) :
y = a
a = 2
print ("y=",y, "a=", a)
print ("a+y", a + y)
return a + y
print ("y=",y, "a=", a)
print (myfunc (a))
print ("y=", y, "a=", a)
Python
Python Functions
1 Like
Answer
y= 5 a= 10
y= 10 a= 2
a+y 12
12
y= 5 a= 10
Working
a = 10
— This line assigns the value 10 to the global variablea
.y = 5
— This line assigns the value 5 to the global variabley
.def myfunc(a):
— This line defines a function namedmyfunc
that takes a parametera
.y = a
— This line assigns the value of the parametera
(which is 10) to the local variabley
within the function.a = 2
— This line assigns the value 2 to the local variablea
within the function, effectively shadowing the global variablea
.print("y=", y, "a=", a)
— This line prints the values of the local variablesy
anda
within the function.print("a+y", a + y)
— This line prints the sum of the local variablesa
andy
within the function.return a + y
— This line returns the sum of the local variablesa
andy
within the function.print("y=", y, "a=", a)
— This line prints the values of the global variablesy
anda
outside the function.print(myfunc(a))
— This line calls themyfunc
function with the value of the global variablea
(which is 10) as an argument and prints the returned value.print("y=", y, "a=", a)
— This line prints the values of the global variablesy
anda
again.
Answered By
3 Likes