blog-cover-image

30 Advanced Python Interview Questions with Answers

This collection of 30 tricky Python interview questions covers core topics from data types and mutability to advanced scenarios involving iterators, generators, decorators, and common Python pitfalls. 

30 Tricky Python Interview Questions


1. What is the output of the following code snippet?


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

Correct answer: A

Explanation: Both a and b point to the same list object in memory. When a.append(4) is called, the list is modified in place. So b reflects the same change.


2. What will be printed?


x = [0, 1, 2]
y = x
x = x + [3, 4]
print(y)
  • A. [0, 1, 2, 3, 4]
  • B. [0, 1, 2]
  • C. [3, 4]
  • D. Error

Correct answer: B

Explanation: The + operator creates a new list. x now references the new list, but y still references the original [0, 1, 2].


3. What is the output?


def func(nums):
    nums += [4]
values = [1, 2, 3]
func(values)
print(values)
  • A. [1, 2, 3, 4]
  • B. [1, 2, 3]
  • C. [4]
  • D. Error

Correct answer: A

Explanation: The += operator modifies the list in place, so values is changed inside func. If nums = nums + [4] was used, it would not change the original list.


4. Which of the following statements about Python sets is FALSE?

  • A. Sets do not allow duplicate elements
  • B. Sets are unordered collections
  • C. Sets support indexing
  • D. Sets can be used to remove duplicates from a list

Correct answer: C

Explanation: Sets do not support indexing because they are unordered.


5. What will be the output?


def f(a, L=[]):
    L.append(a)
    return L
print(f(1))
print(f(2))
print(f(3))
  • A. [1] [2] [3]
  • B. [1] [1, 2] [1, 2, 3]
  • C. [1] [1, 2] [3]
  • D. Error

Correct answer: B

Explanation: Default mutable arguments retain their state across calls. L is created once and reused.


6. What will print(x is y) output?


x = 256
y = 256
print(x is y)
  • A. True
  • B. False
  • C. Error
  • D. None

Correct answer: A

Explanation: Python caches small integers (usually -5 to 256), so x and y point to the same memory location for 256.


7. What will print(x is y) output?


x = 257
y = 257
print(x is y)
  • A. True
  • B. False
  • C. Error
  • D. None

Correct answer: B

Explanation: Integers outside the cache range are not necessarily the same object.


8. What does the following code print?


a = "hello"
b = a
a += " world"
print(b)
  • A. "hello world"
  • B. "hello"
  • C. " world"
  • D. Error

Correct answer: B

Explanation: Strings are immutable. a += " world" creates a new string object; b still points to the original.


9. What will this code print?


d = {"a": 1}
print(d.get("b", 2))
  • A. None
  • B. 2
  • C. KeyError
  • D. 1

Correct answer: B

Explanation: dict.get(key, default) returns default if key is not present.


10. What is the result of bool([]) and bool(0)?

  • A. True
  • B. False
  • C. Error
  • D. None

Correct answer: B

Explanation: bool([]) is False, bool(0) is False. False and False is False.


11. Which expression evaluates to True?

  • A. [] is []
  • B. [] == []
  • C. [1, 2] is [1, 2]
  • D. None == 0

Correct answer: B

Explanation: == checks value equality, is checks identity. Separate list objects are equal in value but not identity.


12. What is the output?


a = [1, 2, 3, 4, 5]
print(a[1:4])
  • A. [1, 2, 3]
  • B. [2, 3, 4]
  • C. [2, 3, 4, 5]
  • D. [1, 2, 3, 4]

Correct answer: B

Explanation: Slicing is [start:end], end is exclusive. So [1:4] is elements at indices 1, 2, and 3.


13. What does this code print?


x = [1, 2, 3, 4, 5]
print(x[::-1])
  • A. [5, 4, 3, 2, 1]
  • B. [1, 2, 3, 4, 5]
  • C. [1, 2, 3]
  • D. []

Correct answer: A

Explanation: [::-1] slices the list in reverse order.


14. What is the output?


for i in range(3):
    print(i)
else:
    print("Done")
  • A. 0 1 2
  • B. 0 1 2 Done
  • C. 0 1 2\nDone
  • D. Error

Correct answer: C

Explanation: The else block after a for loop executes if the loop completes normally (not via break).


15. What does the following code output?


try:
    print(1 / 0)
except ZeroDivisionError:
    print("Error!")
finally:
    print("Finally!")
  • A. Error!
  • B. Finally!
  • C. 1 / 0
  • D. Error!\nFinally!

Correct answer: D

Explanation: The exception is caught and "Error!" is printed. finally always executes, so "Finally!" is printed next.


16. What is the output?


def f(x=[]):
    x.append(1)
    return x

print(f())
print(f())
  • A. [1] [1]
  • B. [1] [1, 1]
  • C. [1, 1] [1, 1]
  • D. Error

Correct answer: B

Explanation: Default mutable arguments persist between calls. The list x is shared.


17. What does this code print?


def foo(val, seq=[]):
    seq.append(val)
    return seq

s = foo(1)
foo(2)
print(s)
  • A. [1]
  • B. [2]
  • C. [1, 2]
  • D. Error

Correct answer: C

Explanation: Same mutable default argument is used for both calls.


18. Which code correctly creates a shallow copy of a list a?

  • A. b = a
  • B. b = a.copy()
  • C. b = list(a)
  • D. b = a[:]

Correct answer: B

Explanation: a.copy() is explicit and available in Python 3. list(a) and a[:] also create shallow copies, but a.copy() is the standard method.


19. What is the output?


x = [1, 2, [3, 4]]
import copy
y = copy.deepcopy(x)
x[2][0] = 99
print(y)
  • A. [1, 2, [99, 4]]
  • B. [1, 2, [3, 4]]
  • C. [1, 2, [3, 99]]
  • D. Error

Correct answer: B

Explanation: deepcopy creates a new list and recursively copies nested lists. So y is unaffected by changes to x[2].


20. What will this code print?


def foo(x):
    return [i*i for i in range(x)]

print(foo(3))
  • A. [0, 1, 4]
  • B. [1, 4, 9]
  • C. [1, 2, 3]
  • D. [0, 1, 2]

Correct answer: A

Explanation: The list comprehension squares i for i in range(3), which is 0, 1, 2.


21. What will be printed?

<

21. What will be printed?


a = [1, 2, 3, 4]
b = [x for x in a if x % 2 == 0]
print(b)
  • A. [1, 3]
  • B. [2, 4]
  • C. [1, 2, 3, 4]
  • D. [2]

Correct answer: B

Explanation: The list comprehension filters for even numbers. Only 2 and 4 are even.


22. What is the output?


def foo(*args, **kwargs):
    print(args, kwargs)

foo(1, 2, three=3, four=4)
  • A. (1, 2, 3, 4) {}
  • B. (1, 2) (three=3, four=4)
  • C. (1, 2) {'three': 3, 'four': 4}
  • D. [1, 2] {'three': 3, 'four': 4}

Correct answer: C

Explanation: *args collects positional arguments as a tuple, **kwargs collects keyword arguments as a dictionary.


23. What does this code print?


def outer():
    x = 'local'
    def inner():
        nonlocal x
        x = 'nonlocal'
    inner()
    return x

print(outer())
  • A. local
  • B. nonlocal
  • C. global
  • D. Error

Correct answer: B

Explanation: nonlocal allows the inner function to modify x in the enclosing (non-global) scope.


24. What is the output?


x = [1, 2, 3]
y = x
del x
print(y)
  • A. Error
  • B. [1, 2, 3]
  • C. None
  • D. []

Correct answer: B

Explanation: del x deletes the name x, but y still refers to the list.


25. What does this code print?


x = 10
def foo():
    print(x)
foo()
  • A. 10
  • B. Error
  • C. None
  • D. UnboundLocalError

Correct answer: A

Explanation: x is not assigned inside foo, so the global x is used.


26. What does this code print?


def foo():
    try:
        return 1
    finally:
        return 2

print(foo())
  • A. 1
  • B. 2
  • C. None
  • D. Error

Correct answer: B

Explanation: If both try and finally have a return, the value in finally overrides.


27. What does this code output?


def make_adder(x):
    def adder(y):
        return x + y
    return adder

add5 = make_adder(5)
print(add5(10))
  • A. 5
  • B. 15
  • C. 10
  • D. Error

Correct answer: B

Explanation: make_adder(5) returns a function that adds 5 to its argument.


28. What will be printed?


def gen():
    yield 1
    yield 2
g = gen()
print(next(g))
print(next(g))
  • A. 1 1
  • B. 2 2
  • C. 1 2
  • D. Error

Correct answer: C

Explanation: The generator yields 1 on the first next(), then 2.


29. What is the output?


def decorator(func):
    def wrapper():
        return func() + 1
    return wrapper

@decorator
def foo():
    return 10

print(foo())
  • A. 10
  • B. 11
  • C. 1
  • D. Error

Correct answer: B

Explanation: The decorator adds 1 to the result of foo.


30. Which of the following statements about Python's set and dict types is TRUE?

  • A. Both set and dict are ordered in all Python versions
  • B. Both set and dict are mutable
  • C. set can have duplicate values
  • D. dict keys can be lists

Correct answer: B

Explanation: Both set and dict are mutable. Sets cannot have duplicates. dict keys must be hashable (lists are not). Dictionaries are insertion-ordered only in Python 3.7+.


Summary Table: Python Interview Question Concepts

Concept Common Pitfall Sample Question(s)
Mutability & Object References Unintentional sharing of mutable objects 1, 2, 3, 5, 16, 17, 18, 19
Type Conversion & Operators Confusing is vs ==, boolean logic 6, 7, 10, 11
Default Arguments Mutable default values persist across calls 5, 16, 17
Slicing & Comprehensions End-index is exclusive in slicing 12, 13, 20, 21
Scope & Closures Confusing local/global/nonlocal variable use 23, 24, 25, 27
Exception Handling finally overriding try returns 15, 26
Iterators & Generators Understanding yield and next 28
Decorators How return values are modified 29
Dictionaries & Sets Ordering, mutability, hashability 4, 9, 30

Conclusion

Mastering these tricky Python interview questions will sharpen your understanding of Python’s data model, scope, memory management, and subtle behaviors. Review these patterns, experiment with variations, and always test your assumptions in an interactive Python shell to build true confidence for your next interview.

Related Articles