Python Variables: MCQs On Variables in Python Language

Python Variables. Multiple Choice Questions on Python Variables including Local and Global Variables.

 

Python Variables

 

 

What returns a dictionary of the module namespace ??
What returns a dictionary of the current namespace ??

a) locals()
globals()

b) locals()
locals()

c) globals()
locals()

d) globals()
globals()

 

Read the following Python code 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

 

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

def f(): x=4
x=1
f()
x

a) Error
b) 4
c) Junk value
d) 1

 

What will be the output of the following ??

x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)

a) 100
b) 90
c) 80
d) Error

 

Python Variables

 

What will be the output of the following code ??

a=10
globals()[‘a’]=25
print(a)

a) 10
b) 25
c) Junk value
d) Error

 

What will be the output of the following Python ??

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

 

What happens if a local variable exists with the same name as the global variable you want to access ??

a) Error
b) The local variable is shadowed
c) Undefined behavior
d) The global variable is shadowed

 

What will be the output ??

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

 

Python Variables

 

What is the output of the following ??

e=”butter”
def f(a): print(a)+e
f(“bitter”)

a) error

b) butter
error

c) bitter
error

d) bitterbutter

 

What is the output ??

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

 

On assigning a value to a variable inside a function, it automatically becomes a global variable ??

a) True
b) False

 

What is 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]

 

Python Variables

 

What is the output of the following Python ??

x=1
def cg():
global x
x=x+1
cg()
x

a) 2
b) 1
c) 0
d) Error

 

What will be the output ??

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

 

What will be the output of the following Python ??

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

 

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

 

Python Variables

 

What will be the output of the following ??

def f1():
global x
x+=1
print(x)
x=12
print(“x”)

a) Error

b) 13

c) 13
x

d) x

 

Which of the following data structures is returned by the functions globals() and locals() ??

a) list
b) set
c) dictionary
d) tuple

 

What will be the output of the code ??

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

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

 

What will be the output of the following ??

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

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

 

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

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!