16.
What is the output of the code shown below?
def f1():
x=100
print(x)
x=+1
f1()
a) Error
b) 100
c) 101
d) 99
Answer: b
EXPLANATION: The variable x is a local variable. It is first printed and then modified. Hence the output of this code is 100.
17.
What is the output of the code shown below?
def san(x):
print(x+1)
x=-2
x=4
san(12)
a) 13
b) 10
c) 2
d) 5
Answer: a
EXPLANATION: The value passed to the function san() is 12. This value is incremented by one and printed. Hence the output of the code shown above is 13.
18.
What is the output of the code shown?
def f1():
global x
x+=1
print(x)
x=12
print("x")
a) Error
b) 13
c) 13
d) x
Answer: d
EXPLANATION: In the code shown above, the variable ‘x’ is declared as global within the function. Hence the output is ‘x’. Had the variable ‘x’ been a local variable, the output would have been:
13
19.
What is the output of the code shown below?
def f1(x):
global x
x+=1
print(x)
f1(15)
print("hello")
a) error
b) hello
c) 16
d) 16
Answer: a
EXPLANATION: The code shown above will result in an error because ‘x’ is a global variable. Had it been a local variable, the output would be: 16
hello
20.
What is the output of the following code?
x=12
def f1(a,b=x):
print(a,b)
x=15
f1(4)
a) Error
b) 12 4
c) 4 12
d) 4 15
Answer: c
EXPLANATION: At the time of leader processing, the value of ‘x’ is 12. It is not modified later. The value passed to the function f1 is 4. Hence the output of the code shown above is 4 12.