Python Output Questions with Answers (Predict the Output)

Output-prediction questions test whether you really understand how Python runs your code. Read each snippet, decide what it prints, then check the answer.

  1. Q1. What does this code print?

    nums = [20, 20, 20, 90, 30]
    print(nums[3:5])
    • A. []
    • B. [90, 30]
    • C. [30, 90]
    • D. [30]
    Show answer

    Answer: B. [90, 30]
    A slice [start:stop:step] includes start but stops before stop; negative numbers count from the end. nums[3:5] is [90, 30].

  2. Q2. What does this code print?

    nums = [70, 70, 10, 40, 20, 20, 90]
    print(nums[-1::-2])
    • A. [90, 20, 10, 70]
    • B. [70]
    • C. [70, 10, 20, 90]
    • D. [90]
    Show answer

    Answer: A. [90, 20, 10, 70]
    A slice [start:stop:step] includes start but stops before stop; negative numbers count from the end. nums[-1::-2] is [90, 20, 10, 70].

  3. Q3. What does this code print?

    a = 15
    b = 7
    print(a // b, a % b)
    • A. 2 -1
    • B. 1 2
    • C. 2 1
    • D. 3 1
    Show answer

    Answer: C. 2 1
    In Python, // rounds down (towards −∞) and % has the same sign as the divisor, so 15 // 7 = 2 and 15 % 7 = 1.

  4. Q4. What does this code print?

    a = 36
    b = 4
    print(a // b, a % b)
    • A. 10 0
    • B. 9 1
    • C. 8 1
    • D. 9 0
    Show answer

    Answer: D. 9 0
    In Python, // rounds down (towards −∞) and % has the same sign as the divisor, so 36 // 4 = 9 and 36 % 4 = 0.

  5. Q5. What does this code print?

    s = "keyboard"
    print(s[1], s[-1], len(s * 3))
    • A. e d 24
    • B. e k 24
    • C. y d 24
    • D. e d 11
    Show answer

    Answer: A. e d 24
    Indexes start at 0, s[-1] counts from the end, and s * 3 repeats the word 3 times (8 × 3 = 24 characters).

  6. Q6. What does this code print?

    s = "program"
    print(s[2], s[-4], len(s * 3))
    • A. o g 10
    • B. o g 21
    • C. g g 21
    • D. o r 21
    Show answer

    Answer: B. o g 21
    Indexes start at 0, s[-4] counts from the end, and s * 3 repeats the word 3 times (7 × 3 = 21 characters).

  7. Q7. What does this code print?

    print(sum(range(3, 18, 2)))
    • A. 98
    • B. 78
    • C. 80
    • D. 82
    Show answer

    Answer: C. 80
    range(3, 18, 2) gives 3, 5, 7, 9, 11, 13, 15, 17 (stop is not included). Their sum is 80.

  8. Q8. What does this code print?

    print(sum(range(5, 13, 1)))
    • A. 81
    • B. 69
    • C. 67
    • D. 68
    Show answer

    Answer: D. 68
    range(5, 13, 1) gives 5, 6, 7, 8, 9, 10, 11, 12 (stop is not included). Their sum is 68.

  9. Q9. What does this code print?

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

    Answer: C. 2
    a[:] makes a new copy, so appending to b does not change a.

  10. Q10. What does this code print?

    a = [1, 2]
    b = a
    b.append(7)
    print(len(a))
    • A. 2
    • B. Error
    • C. 3
    • D. 4
    Show answer

    Answer: C. 3
    b = a does not copy the list: both names point to the same list, so a also grows.

  11. Q11. What does this code print?

    i = 3
    count = 0
    while i < 100:
        i = i * 3
        count += 1
    print(count, i)
    • A. 3 81
    • B. 4 243
    • C. 4 81
    • D. 5 729
    Show answer

    Answer: B. 4 243
    i goes 3 → 9 → 27 → 81 → 243; the loop runs 4 times and stops once i reaches 243.

  12. Q12. What does this code print?

    i = 1
    count = 0
    while i < 50:
        i = i * 2
        count += 1
    print(count, i)
    • A. 6 64
    • B. 5 32
    • C. 7 128
    • D. 6 32
    Show answer

    Answer: A. 6 64
    i goes 1 → 2 → 4 → 8 → 16 → 32 → 64; the loop runs 6 times and stops once i reaches 64.

  13. Q13. What does this code print?

    d = {'b': 7, 'c': 7, 'a': 5}
    d['c'] = 12
    print(len(d), d.get('c', 0))
    • A. 3 0
    • B. 3 12
    • C. 4 12
    • D. 3 None
    Show answer

    Answer: B. 3 12
    Assigning to an existing key replaces its value; a new key adds an entry. get() returns the default 0 when the key is missing.

  14. Q14. What does this code print?

    d = {'a': 8, 'c': 5, 'b': 7}
    d['c'] = 17
    print(len(d), d.get('x', 0))
    • A. 3 None
    • B. 3 0
    • C. 3 17
    • D. 4 0
    Show answer

    Answer: B. 3 0
    Assigning to an existing key replaces its value; a new key adds an entry. get() returns the default 0 when the key is missing.

  15. Q15. What does this code print?

    print([i * i for i in range(6) if i % 2 == 0])
    • A. [0, 4, 16]
    • B. [0, 4, 8]
    • C. [4, 16, 36]
    • D. [1, 9, 25]
    Show answer

    Answer: A. [0, 4, 16]
    range(6) is 0..5; keep the multiples of 2 (0 counts!) and square them: [0, 4, 16].

  16. Q16. What does this code print?

    print([i * i for i in range(7) if i % 2 == 0])
    • A. [0, 4, 16, 36]
    • B. [1, 9, 25]
    • C. [0, 4, 8, 12]
    • D. [4, 16, 36]
    Show answer

    Answer: A. [0, 4, 16, 36]
    range(7) is 0..6; keep the multiples of 2 (0 counts!) and square them: [0, 4, 16, 36].

  17. Q17. What does this code print?

    count = 0
    for i in range(4):
        for j in range(i, 7):
            count += 1
    print(count)
    • A. 22
    • B. 23
    • C. 29
    • D. 18
    Show answer

    Answer: A. 22
    For each i the inner loop runs 7 − i times: 7 + 6 + 5 + 4 = 22.

  18. Q18. What does this code print?

    count = 0
    for i in range(5):
        for j in range(i, 6):
            count += 1
    print(count)
    • A. 25
    • B. 15
    • C. 20
    • D. 26
    Show answer

    Answer: C. 20
    For each i the inner loop runs 6 − i times: 6 + 5 + 4 + 3 + 2 = 20.

  19. Q19. What does this code print?

    def f(n):
        if n <= 1:
            return 1
        return n + f(n - 1)
    
    print(f(6))
    • A. 21
    • B. 15
    • C. 27
    • D. 22
    Show answer

    Answer: A. 21
    f(6) = 6 + 5 + 4 + 3 + 2 + 1 = 21.

  20. Q20. What does this code print?

    def f(n):
        if n <= 1:
            return 1
        return n + f(n - 1)
    
    print(f(3))
    • A. 9
    • B. 7
    • C. 6
    • D. 5
    Show answer

    Answer: C. 6
    f(3) = 3 + 2 + 1 = 6.

  21. Q21. What does this code print?

    a, b = 4, 8
    print(a & b, a | b, a ^ b, a << 1)
    • A. 0 12 12 8
    • B. 12 0 12 8
    • C. 0 12 12 2
    • D. 0 12 12 4
    Show answer

    Answer: A. 0 12 12 8
    In binary a = 100, b = 1000. & keeps bits set in both, | in either, ^ in exactly one; << 1 multiplies by 2.

  22. Q22. What does this code print?

    a, b = 3, 8
    print(a & b, a | b, a ^ b, a << 2)
    • A. 0 11 11 12
    • B. 11 0 11 12
    • C. 0 11 11 6
    • D. 0 11 11 0
    Show answer

    Answer: A. 0 11 11 12
    In binary a = 11, b = 1000. & keeps bits set in both, | in either, ^ in exactly one; << 2 multiplies by 4.

  23. Q23. What does this code print?

    s = "pink,green,red,"
    print(len(s.split(",")), s.count("e"))
    • A. 4 4
    • B. 3 3
    • C. 4 3
    • D. 5 3
    Show answer

    Answer: C. 4 3
    split(",") keeps empty strings between consecutive commas, giving 4 pieces; "e" appears 3 times.

  24. Q24. What does this code print?

    s = "pink,,,red,blue"
    print(len(s.split(",")), s.count("r"))
    • A. 5 2
    • B. 5 1
    • C. 3 1
    • D. 4 1
    Show answer

    Answer: B. 5 1
    split(",") keeps empty strings between consecutive commas, giving 5 pieces; "r" appears 1 time.

  25. Q25. What does this code print? (sorted() is stable: equal keys keep their original order.)

    words = ['plum', 'mango', 'kiwi', 'banana']
    print(sorted(words, key=len, reverse=True))
    • A. ['plum', 'kiwi', 'mango', 'banana']
    • B. ['plum', 'mango', 'kiwi', 'banana']
    • C. ['banana', 'kiwi', 'mango', 'plum']
    • D. ['banana', 'mango', 'plum', 'kiwi']
    Show answer

    Answer: D. ['banana', 'mango', 'plum', 'kiwi']
    key=len sorts by word length, longest first; words of equal length keep their original order.

  26. Q26. What does this code print? (sorted() is stable: equal keys keep their original order.)

    words = ['apple', 'banana', 'plum', 'dates']
    print(sorted(words, key=len))
    • A. ['plum', 'apple', 'dates', 'banana']
    • B. ['apple', 'banana', 'plum', 'dates']
    • C. ['apple', 'banana', 'dates', 'plum']
    • D. ['banana', 'dates', 'apple', 'plum']
    Show answer

    Answer: A. ['plum', 'apple', 'dates', 'banana']
    key=len sorts by word length; words of equal length keep their original order.

  27. Q27. What does this code print?

    x, y, z = 5, 4, 9
    flag = True
    print(x > y and y > z or not flag)
    • A. None
    • B. True
    • C. False
    • D. Error
    Show answer

    Answer: C. False
    not binds tightest, then and, then or: (x > y and y > z) is False, not flag is False, so the result is False.

  28. Q28. What does this code print?

    x, y, z = 8, 4, 2
    flag = False
    print(x > y and y > z or not flag)
    • A. Error
    • B. True
    • C. None
    • D. False
    Show answer

    Answer: B. True
    not binds tightest, then and, then or: (x > y and y > z) is True, not flag is True, so the result is True.

Practised these? Now prove it.

Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.

Start a mock test