Python Multiple Choice Questions & Answers (MCQs) focuses on “Global vs Local Variables – 1”.
Q 1. What will be the output of the following Python code?
def f1():
x=15
print(x)
x=12
f1()
A. 10
B. 12
C. 15
D. Error
Show Answer
Answer:-C. 15Explanation
In the code shown above, x=15 is a local variable whereas x=12 is a global variable. Preference is given to local variable over global variable. Hence the output of the code shown above is 15.
Q 2. What will be the output of the following Python code?
def f1():
x=100
print(x)
x=+1
f1()
A. 99
B. 100
C. 101
D. Error
Show Answer
Answer:-B. 100Explanation
The variable x is a local variable. It is first printed and then modified. Hence the output of this code is 100.Q 3. What will be the output of the following Python code?
def san(x):
print(x+1)
x=-2
x=4
san(12)
A. 5
B. 9
C. 10
D. 13
Show Answer
Answer:-D. 13Explanation
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.
Q 4. What will be the output of the following Python code?
def f1(x):
global x
x+=1
print(x)
f1(15)
print(“hello”)
A. error
B. hello
C. 16
D. 16
hello
Show Answer
Answer:-A. errorExplanation
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
Q 5. What will be the output of the following Python code?
def f1(a,b=[]):
b.append(a)
return b
print(f1(2,[3,4]))
A. [3,2,4]
B. [2,3,4]
C. [3,4,2]
D. Error
Show Answer
Answer:-C. [3,4,2]Explanation
In the code shown above, the integer 2 is appended to the list [3,4]. Hence the output of the code is [3,4,2]. Both the variables a and b are local variables.
Q 6. What will be the output of the following Python code?
x=12
def f1(a,b=x):
print(a,b)
x=15
f1(4)
A. 100
B. 12 4
C. 4 12
D. Error
Show Answer
Answer:-C. 4 12Explanation
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.
Q 7. What will be the output of the following Python code?
def f():
global a
print(a)
a = “hello”
print(a)
a = “world”
f()
print(a)
A. hello
hello
world
B. world
hello
hello
C. hello
world
world
D. world
hello
world
Show Answer
Answer:-B. world hello helloExplanation
Since the variable ‘a’ has been explicitly specified as a global variable, the value of a passed to the function is ‘world’. Hence the output of this code is: world hello hello
Q 8. What will be the output of the following Python code?
def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
A. 1 2 3 4
B. 5 10 15 4
C. 10 20 30 40
D. 5 10 15 40
Show Answer
Answer:-C. 10 20 30 40Explanation
The above code shows a combination of local and global variables. The output of this code is: 10 20 30 40
Q 9. What will be the output of the following Python code?
x = 5
def f1():
global x
x = 4
def f2(a,b):
global x
return a+b+x
f1()
total = f2(1,2)
print(total)
A. 7
B. 8
C. 15
D. Error
Show Answer
Answer:-A. 7Explanation
In the code shown above, the variable ‘x’ has been declared as a global variable under both the functions f1 and f2. The value returned is a+b+x = 1+2+4 = 7.
Q 10. What will be the output of the following Python code?
def f(x):
print(“outer”)
def f1(a):
print(“inner”)
print(a,x)
f(3)
f1(1)
A. outer
error
B. inner
error
C. outer
inner
D. error
Show Answer
Answer:-A. outer errorExplanation
The error will be caused due to the statement f1(1) because the function is nested. If f1(1) had been called inside the function, the output would have been different and there would be no error.
Q 11. Read the following Python code carefully and point out the global variables?
y, z = 1, 2
def f():
global x
x = y+z
A. x
B. y and z
C. x, y and z
D. Neither x, nor y, nor z
Show Answer
Answer:-C. x, y and zExplanation
In the code shown above, x, y and z are global variables inside the function f. y and z are global because they are not assigned in the function. x is a global variable because it is explicitly specified so in the code. Hence, x, y and z are global variables.
Q 12. What will be the output of the following Python code?
x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)
A. 100
B. 90
C. 80
D. Error
Show Answer
Answer:-A. 100Explanation
The output of the code shown above is 100. This is because the variable ‘x’ has been declared as global within the functions f1 and f2.Q 13. What will be the output of the following Python code?
def f1():
global x
x+=1
print(x)
x=12
print(“x”)
A. x
B. 13
C. 13
x
D. Error
Leave a Reply