# Python Questions and Answers – Global vs Local Variables – 1

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Global vs Local Variables – 1”.

1. What will be the output of the following Python code?

```def f1():
x=15
print(x)
x=12
f1()```

a) Error
b) 12
c) 15
d) 1512
View Answer

Answer: c
Explanation: 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.
advertisement
advertisement

2. What will be the output of the following Python code?

```def f1():
x=100
print(x)
x=+1
f1()```

a) Error
b) 100
c) 101
d) 99
View Answer

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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following Python code?

advertisement
```def san(x):
print(x+1)
x=-2
x=4
san(12)```

a) 13
b) 10
c) 2
d) 5
View Answer

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.
advertisement

4. What will be the output of the following Python code?

```def f1():
global x
x+=1
print(x)
x=12
print("x")```

a) Error
b) 13
c)

```13
x```

d) x
View Answer

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
x

5. 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```
View Answer
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

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) Error
b) 12 4
c) 4 12
d) 4 15
View Answer

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.

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```
View Answer
Answer: b
Explanation: 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

8. 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) Error
d) [3,4,2]
View Answer

Answer: d
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.

9. 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
View Answer

Answer: c
Explanation: The above code shows a combination of local and global variables. The output of this code is: 10 20 30 40

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
View Answer

Answer: a
Explanation: 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.

11. 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) Error
b) 7
c) 8
d) 15
View Answer

Answer: b
Explanation: 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.

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
View Answer

Answer: a
Explanation: 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.

13. 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
View Answer

Answer: c
Explanation: 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.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.