Softenant
Technologies
Python Variables MCQs (25) — Answers at the End

Python Variables MCQs (25)

Python Training in Vizag (Softenant)

1) Which variable name is valid?

A. 1value
B. value_1
C. class
D. first-name

2) What is the output?

a = 2
b = a
a = 3
print(b)
A. 2
B. 3
C. Error
D. None

3) Multiple assignment result?

x, y, z = 1, 2, 3
print(x + z)
A. 3
B. 4
C. 2
D. Error

4) Packing/unpacking output?

a, *b = [10, 20, 30, 40]
print(a, b)
A. 10 [20, 30, 40]
B. 10 20 30 40
C. [10] [20, 30, 40]
D. Error

5) Swapping variables?

a, b = 1, 2
a, b = b, a
print(a, b)
A. 1 2
B. 2 1
C. Error
D. None 1

6) Identity vs equality?

a = [1, 2]
b = a
print(a == b, a is b)
A. True False
B. True True
C. False True
D. False False

7) Copy vs alias?

a = [1, 2]
b = a[:]     # shallow copy
a.append(3)
print(b)
A. [1, 2, 3]
B. [1, 2]
C. [3]
D. Error

8) Mutability check?

t = (1, 2, 3)
try:
    t[0] = 9
except TypeError:
    print("E")
A. E
B. 9
C. 1
D. No output

9) Strings are…

A. Mutable
B. Immutable
C. Neither
D. Depends on length

10) Augmented assignment on list?

a = [1, 2]
b = a
a += [3]
print(a, b)
A. [1, 2, 3] [1, 2]
B. [1, 2, 3] [1, 2, 3]
C. [1, 2] [1, 2, 3]
D. Error

11) Augmented assignment on int?

x = 5
y = x
x += 1
print(x, y)
A. 6 5
B. 6 6
C. 5 6
D. Error

12) Dict aliasing?

d1 = {"a": 1}
d2 = d1
d1["b"] = 2
print(d2)
A. {“a”: 1}
B. {“a”: 1, “b”: 2}
C. {“b”: 2}
D. Error

13) Shadowing globals?

x = 10
def f():
    x = 20
    return x
print(f(), x)
A. 20 10
B. 20 20
C. 10 10
D. Error

14) Using global?

x = 1
def g():
    global x
    x = 5
g()
print(x)
A. 1
B. 5
C. Error
D. None

15) Using nonlocal?

def outer():
    x = 1
    def inner():
        nonlocal x
        x = 3
    inner()
    return x
print(outer())
A. 1
B. 3
C. Error
D. None

16) Rebinding vs mutation?

a = [1, 2]
a = a + [3]
print(a)
A. [1, 2]
B. [1, 2, 3]
C. Error
D. None

17) Deleting a name?

x = 7
del x
print(x)
A. 7
B. NameError
C. None
D. 0

18) Unpacking star count?

a, *b, c = [1, 2, 3, 4]
print(b)
A. [2, 3]
B. [1, 2, 3, 4]
C. [3, 4]
D. [2, 3, 4]

19) Name lookup order inside function?

A. GLEN: Global → Local → Enclosing → Builtins
B. LEBG: Local → Enclosing → Builtins → Global
C. LEGB: Local → Enclosing → Global → Builtins
D. LGBE: Local → Global → Builtins → Enclosing

20) What prints?

a = [1, [2, 3]]
b = a[:]
b[1].append(4)
print(a)
A. [1, [2, 3]]
B. [1, [2, 3, 4]]
C. [1, [4]]
D. Error

21) Safe statement about is?

A. Use is for value equality.
B. is checks object identity (same object).
C. is and == are identical.
D. Use is only for numbers.

22) Rebinding local with same name as global?

x = 100
def h():
    x = x + 1
    return x
print(h())
A. 101
B. 100
C. UnboundLocalError
D. NameError

23) Tuple immutability but containing list?

t = (1, [2, 3])
t[1].append(4)
print(t)
A. (1, [2, 3])
B. (1, [2, 3, 4])
C. TypeError
D. (1, 2, 3, 4)

24) id() after rebinding int?

x = 5
i1 = id(x)
x = x + 1
i2 = id(x)
print(i1 == i2)
A. True
B. False
C. Depends on OS
D. Error

25) Which statement is true?

A. Variables store values directly, not references.
B. Names are bindings to objects; objects have types.
C. Every reassignment mutates the same object.
D. Python variables must be declared with a type first.

Answer Key

1) B

2) A

3) B

4) A

5) B

6) B

7) B

8) A

9) B

10) B

11) A

12) B

13) A

14) B

15) B

16) B

17) B

18) A

19) C

20) B

21) B

22) C

23) B

24) B

25) B