Softenant
Technologies
Python Nested If–Else MCQs (25) — Answers at the End

Python Nested If–Else MCQs (25)

Covers nested conditions, elif vs nested if, truthiness, membership, and edge cases. Answers at the end

Python Training in Vizag (Softenant)

1) Output?

x = 5
if x > 0:
    if x % 2 == 0:
        print("A")
    else:
        print("B")
else:
    print("C")
A. A
B. B
C. C
D. No output

2) What prints?

x = 2
if x > 1:
    if x > 2:
        print("A")
    else:
        print("B")
else:
    print("C")
A. A
B. B
C. C
D. Error

3) Choose the output.

x = 0
if x:
    if x > 0:
        print("P")
    else:
        print("N")
else:
    print("Z")
A. P
B. N
C. Z
D. Error

4) Output?

x = 3
y = 4
if x % 2:
    if y % 2:
        print("OO")
    else:
        print("OE")
else:
    if y % 2:
        print("EO")
    else:
        print("EE")
A. OO
B. OE
C. EO
D. EE

5) What prints?

s = "hi"
if s:
    if len(s) > 2:
        print("L")
    else:
        print("S")
else:
    print("E")
A. L
B. S
C. E
D. Error

6) Output?

n = 10
if n > 5:
    if n < 10:
        print("X")
    elif n == 10:
        print("Y")
    else:
        print("Z")
else:
    print("W")
A. X
B. Y
C. Z
D. W

7) Choose the output.

x = 1
if True:
    if False:
        print("A")
    else:
        print("B")
else:
    print("C")
A. A
B. B
C. C
D. No output

8) What prints?

x = 5
if x > 2:
    if x < 5:
        print("A")
    elif x == 5:
        print("B")
    else:
        print("C")
else:
    print("D")
A. A
B. B
C. C
D. D

9) Output?

x = 7
if x > 10:
    print(">10")
elif x > 5:
    if x < 8:
        print("6-7")
    else:
        print(">=8")
else:
    print("<=5")
A. >10
B. 6-7
C. >=8
D. <=5

10) Choose the output.

x = 3; y = 3
if x == y:
    if x > 2:
        print("T")
    else:
        print("F")
else:
    print("N")
A. T
B. F
C. N
D. Error

11) What prints?

s = ""
if s == "":
    if not s:
        print(1)
    else:
        print(2)
else:
    print(3)
A. 1
B. 2
C. 3
D. No output

12) Output?

lst = [1, 2, 3]
if lst:
    if lst[0] == 1 and lst[-1] == 3:
        print("OK")
    else:
        print("NO")
else:
    print("EMPTY")
A. OK
B. NO
C. EMPTY
D. Error

13) Choose the output.

x = 4
if x % 2 == 0:
    if x % 4 == 0:
        print("D")
    else:
        print("E")
else:
    print("O")
A. D
B. E
C. O
D. No output

14) Output?

x = 5
if x > 0:
    if x > 10:
        print("A")
    elif x > 7:
        print("B")
    else:
        print("C")
else:
    print("D")
A. A
B. B
C. C
D. D

15) What prints?

flag = False
if not flag:
    if flag:
        print("X")
    else:
        print("Y")
else:
    print("Z")
A. X
B. Y
C. Z
D. No output

16) Choose the output.

x = 0
y = 1
if x:
    if y:
        print("A")
    else:
        print("B")
else:
    if y:
        print("C")
    else:
        print("D")
A. A
B. B
C. C
D. D

17) Output?

x = 10
if x >= 0:
    if x and (x // 5 == 2):
        print("M")
    else:
        print("N")
else:
    print("P")
A. M
B. N
C. P
D. No output

18) What prints?

x = 3
if x > 1:
    if x > 2:
        if x > 3:
            print("A")
        else:
            print("B")
    else:
        print("C")
else:
    print("D")
A. A
B. B
C. C
D. D

19) Choose the output.

x = -1
if x:
    if x > 0:
        print("P")
    else:
        print("N")
else:
    print("Z")
A. P
B. N
C. Z
D. No output

20) Output?

x = 5
y = 10
if x > y:
    print("A")
elif x < y:
    if y - x == 5:
        print("B")
    else:
        print("C")
else:
    print("D")
A. A
B. B
C. C
D. D

21) What prints?

x = 2
if x > 1:
    if x > 2:
        print(1)
    elif x == 2:
        print(2)
    else:
        print(3)
else:
    print(4)
A. 1
B. 2
C. 3
D. 4

22) Choose the output.

x = 1
y = 0
if x or y:
    if x and y:
        print("XY")
    elif x:
        print("X")
    else:
        print("Y")
else:
    print("N")
A. XY
B. X
C. Y
D. N

23) Output?

x = [0]
if x:
    if x[0]:
        print("T")
    else:
        print("F")
else:
    print("E")
A. T
B. F
C. E
D. No output

24) What prints?

s = "abc"
if "a" in s:
    if s.startswith("ab"):
        if s.endswith("c"):
            print(1)
        else:
            print(2)
    else:
        print(3)
else:
    print(4)
A. 1
B. 2
C. 3
D. 4

25) Choose the output.

x = None
if x is None:
    if x == 0:
        print("Z")
    elif x is None:
        print("N")
    else:
        print("X")
else:
    print("Y")
A. Z
B. N
C. X
D. Y

Answer Key

1) B

2) B

3) C

4) B

5) B

6) B

7) B

8) B

9) B

10) A

11) A

12) A

13) A

14) C

15) B

16) C

17) A

18) B

19) B

20) B

21) B

22) B

23) B

24) A

25) B