Python MCQs on Global and Local Variables: SET 1

Python MCQs on Global and Local Variables

In this section, we are going to introduce you to the most frequently asked Python MCQs on Global and Local Variables. These Python MCQs on Global and Local Variables will be going to help you in Interviews and Exams.

 

 

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

Check Answer

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

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

Check Answer

Answer: b
The variable x is a local variable. It is first printed and then modified. Hence the output of this code is 100.

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

def san(x):

print(x+1)

x=-2

x=4

san(12)

a) 13

b) 10

c) 2

d) 5

Check Answer

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

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

Answer: b
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]

Check Answer

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

Check Answer

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

Check Answer

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

Page  1  2

 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment