Python Interview Questions and Answers

The Python questions freshers are asked most in campus drives and technical rounds, each with the answer and a short explanation.

  1. Q1. Which of these is a mutable data type in Python?

    • A. int
    • B. tuple
    • C. list
    • D. str
    Show answer

    Answer: C. list
    Lists can be changed after creation; tuples, strings and ints cannot.

  2. Q2. What is the output of type(3 / 2) in Python 3?

    print(type(3 / 2))
    • A. <class 'double'>
    • B. <class 'decimal'>
    • C. <class 'int'>
    • D. <class 'float'>
    Show answer

    Answer: D. <class 'float'>
    / always returns a float in Python 3; // is integer (floor) division.

  3. Q3. Which keyword is used to define a function in Python?

    • A. function
    • B. def
    • C. define
    • D. fun
    Show answer

    Answer: B. def
    Functions are defined with def name(params):

  4. Q4. What does len({1, 2, 2, 3, 3, 3}) return?

    print(len({1, 2, 2, 3, 3, 3}))
    • A. 6
    • B. 4
    • C. 3
    • D. Error
    Show answer

    Answer: C. 3
    A set keeps only unique values: {1, 2, 3}.

  5. Q5. Which statement about Python tuples is true?

    • A. They are always sorted
    • B. They use square brackets
    • C. They cannot hold mixed types
    • D. They are immutable
    Show answer

    Answer: D. They are immutable
    Tuples can't be changed after creation and are written with ( ).

  6. Q6. What is the correct way to start a single-line comment in Python?

    • A. #
    • B. --
    • C. /*
    • D. //
    Show answer

    Answer: A. #
    Python comments start with #.

  7. Q7. What does this code print?

    print([1, 2, 3] * 2)
    • A. [1, 2, 3, 1, 2, 3]
    • B. [2, 4, 6]
    • C. Error
    • D. [1, 2, 3, 2]
    Show answer

    Answer: A. [1, 2, 3, 1, 2, 3]
    Multiplying a list repeats it.

  8. Q8. Which method adds an item to the end of a list?

    • A. push()
    • B. insert_end()
    • C. add()
    • D. append()
    Show answer

    Answer: D. append()
    list.append(x) adds x at the end. Sets use add().

  9. Q9. What does this code print?

    def f(a, b=2):
        return a * b
    
    print(f(3))
    • A. 6
    • B. 120
    • C. 15
    • D. None
    Show answer

    Answer: A. 6
    The default argument b=2 is used, so 3 * 2 = 6.

  10. Q10. What is the output of bool('') and bool('0')?

    print(bool(''), bool('0'))
    • A. False True
    • B. True False
    • C. True True
    • D. False False
    Show answer

    Answer: A. False True
    Empty strings are falsy; any non-empty string, even '0', is truthy.

  11. Q11. Which of these creates an empty dictionary?

    • A. {}
    • B. set()
    • C. ()
    • D. []
    Show answer

    Answer: A. {}
    {} is an empty dict; an empty set must be written set().

  12. Q12. What does the 'is' operator check?

    • A. Whether two values are equal
    • B. Whether two names refer to the same object
    • C. The type of an object
    • D. Whether a value is True
    Show answer

    Answer: B. Whether two names refer to the same object
    is compares identity; == compares values.

  13. Q13. What does this code print?

    def add(x, items=[]):
        items.append(x)
        return items
    
    print(add(1), add(2))
    • A. [1] [2]
    • B. [1, 2] [1, 2]
    • C. [1] [1, 2]
    • D. [1, 2] [2]
    Show answer

    Answer: B. [1, 2] [1, 2]
    The default list is created once and shared between calls, a classic Python trap. Use None as the default instead.

  14. Q14. What is a lambda in Python?

    • A. A loop keyword
    • B. A module for maths
    • C. A small anonymous function written in one expression
    • D. A type of exception
    Show answer

    Answer: C. A small anonymous function written in one expression
    lambda x: x * 2 creates a function without a name.

  15. Q15. What does this code print?

    def f():
        try:
            return 1
        finally:
            return 2
    
    print(f())
    • A. 3
    • B. 2
    • C. 1
    • D. Error
    Show answer

    Answer: B. 2
    The return inside try runs, but finally always runs too, and a return in finally overrides it.

  16. Q16. Which built-in function returns both index and value while looping?

    • A. zip()
    • B. range()
    • C. enumerate()
    • D. items()
    Show answer

    Answer: C. enumerate()
    for i, value in enumerate(items):

  17. Q17. What does zip([1, 2, 3], ['a', 'b']) produce when converted to a list?

    print(list(zip([1, 2, 3], ['a', 'b'])))
    • A. [1, 'a', 2, 'b', 3]
    • B. [(1, 'a'), (2, 'b'), (3, None)]
    • C. Error
    • D. [(1, 'a'), (2, 'b')]
    Show answer

    Answer: D. [(1, 'a'), (2, 'b')]
    zip stops at the shortest input.

  18. Q18. What is the purpose of __init__ in a Python class?

    • A. It imports a module
    • B. It makes a class abstract
    • C. It deletes an object
    • D. It initialises a new object's attributes
    Show answer

    Answer: D. It initialises a new object's attributes
    __init__ runs automatically when an object is created.

  19. Q19. What does this code print?

    x = 10
    def change():
        x = 5
    change()
    print(x)
    • A. 15
    • B. Error
    • C. 5
    • D. 10
    Show answer

    Answer: D. 10
    The function creates its own local x; the global x stays 10.

  20. Q20. Which keyword lets a function modify a variable defined at module level?

    • A. nonlocal
    • B. static
    • C. global
    • D. extern
    Show answer

    Answer: C. global
    global x inside the function refers to the module-level x. nonlocal is for enclosing functions.

  21. Q21. What does a generator function use instead of return to produce values one at a time?

    • A. emit
    • B. yield
    • C. produce
    • D. next
    Show answer

    Answer: B. yield
    yield pauses the function and hands back a value; it resumes on the next request.

  22. Q22. What does this code print?

    print(['a', 'b', 'c'][-2:])
    • A. ['a', 'b']
    • B. ['b', 'c']
    • C. ['c']
    • D. ['a', 'b', 'c']
    Show answer

    Answer: B. ['b', 'c']
    Negative indexes count from the end; [-2:] takes the last two items.

  23. Q23. Which of these is NOT a valid Python variable name?

    • A. 2nd_place
    • B. _count
    • C. studentName
    • D. total2
    Show answer

    Answer: A. 2nd_place
    Names can't start with a digit.

  24. Q24. What is the result of 2 ** 3 ** 2 in Python?

    print(2 ** 3 ** 2)
    • A. 12
    • B. 512
    • C. 36
    • D. 64
    Show answer

    Answer: B. 512
    ** is right-associative: 3 ** 2 = 9 first, then 2 ** 9 = 512.

  25. Q25. Which exception is raised by int('abc')?

    • A. TypeError
    • B. ValueError
    • C. NameError
    • D. SyntaxError
    Show answer

    Answer: B. ValueError
    The type is right (a string) but the value can't be converted.

  26. Q26. What does the with statement do when opening a file?

    • A. Locks the file for other programs
    • B. Reads the whole file into memory
    • C. Closes the file automatically when the block ends
    • D. Opens the file in write mode
    Show answer

    Answer: C. Closes the file automatically when the block ends
    with uses a context manager so the file is closed even if an error happens.

  27. Q27. What does this code print?

    d = {'a': 1, 'b': 2}
    d['b'] = 3
    print(d)
    • A. Error
    • B. {'a': 1, 'b': 2}
    • C. {'a': 1, 'b': 2, 'b': 3}
    • D. {'a': 1, 'b': 3}
    Show answer

    Answer: D. {'a': 1, 'b': 3}
    Keys are unique: assigning to an existing key replaces its value.

  28. Q28. What is the difference between a list and a tuple?

    • A. Lists can't hold strings
    • B. There is no difference
    • C. Tuples are faster to change
    • D. Lists are mutable, tuples are immutable
    Show answer

    Answer: D. Lists are mutable, tuples are immutable
    That immutability also makes tuples usable as dictionary keys.

  29. Q29. What does this code print?

    s = 'python'
    print(s.upper(), s[::-1])
    • A. PYTHON PYTHON
    • B. PYTHON nohtyp
    • C. Python python
    • D. python nohtyP
    Show answer

    Answer: B. PYTHON nohtyp
    upper() returns a new uppercase string; [::-1] reverses the original lowercase one.

  30. Q30. What does PEP 8 describe?

    • A. Python's package manager
    • B. The Python 3.8 release
    • C. A Python web framework
    • D. The style guide for Python code
    Show answer

    Answer: D. The style guide for Python code
    PEP 8 covers naming, indentation (4 spaces), line length and other style rules.

  31. Q31. What does this code print?

    count = 0
    for n in [2, 5, 8, 11, 14]:
        if n % 2 == 0:
            count += 1
    print(count)
    • A. 3
    • B. 0
    • C. 2
    • D. 5
    Show answer

    Answer: A. 3
    2, 8 and 14 are even, so count is 3.

  32. Q32. Which data structure would you use for fast membership checks (x in collection)?

    • A. list
    • B. tuple
    • C. string
    • D. set
    Show answer

    Answer: D. set
    Sets (and dict keys) use hashing, so 'in' is O(1) on average; lists are O(n).

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