Python interview questions

15 questions from the Python Fundamentals: From Basics to Async course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 2: Basics

1. What does int(3.9) return?

  1. 4
  2. 3
  3. 3.9
  4. ValueError
Show answer

Answer: 3

int() truncates toward zero; use round() to round.

Lesson: Data Types

2. Which collection is immutable?

  1. list
  2. dict
  3. tuple
  4. set
Show answer

Answer: tuple

A tuple can't be changed after creation.

Lesson: Data Types

Module 3: Control Flow

3. When does a loop's `else` block run?

  1. Every time the loop ends
  2. Only if the loop never ran
  3. Only when an exception is raised
  4. Only if the loop finished without `break`
Show answer

Answer: Only if the loop finished without `break`

A `break` skips the loop's `else`.

Lesson: Loops

Module 4: Collections

4. What does `[n for n in range(5) if n % 2 == 0]` produce?

  1. [0, 2, 4]
  2. [1, 3]
  3. [0, 1, 2, 3, 4]
  4. [0, 2, 4, 6]
Show answer

Answer: [0, 2, 4]

Only even numbers from 0-4 pass the `if` filter.

Lesson: Comprehensions & Nested Data

Module 7: Object-Oriented Programming

5. Which decorator lets a method create a new instance without an existing object?

  1. @staticmethod
  2. @classmethod
  3. @property
  4. @abstractmethod
Show answer

Answer: @classmethod

@classmethod receives the class itself (cls) and can build and return a new instance.

Lesson: Class & Static Methods

6. Which methods does @dataclass generate automatically?

  1. __init__, __repr__ and __eq__
  2. Only __del__
  3. __enter__ and __exit__
  4. None of them
Show answer

Answer: __init__, __repr__ and __eq__

Dataclasses generate these from the annotated fields.

Lesson: Dataclasses & Enums

Module 10: Iterators & Generators

7. Why prefer a generator over a list for a huge sequence?

  1. It runs faster in every case
  2. It automatically sorts the items
  3. It produces items lazily instead of storing them all in memory
  4. It always uses more memory
Show answer

Answer: It produces items lazily instead of storing them all in memory

Generators compute one value at a time on demand, so huge or infinite sequences don't need to fit in memory.

Lesson: Generators & yield

Module 14: Advanced Topics

8. Which type is immutable?

  1. list
  2. dict
  3. tuple
  4. set
Show answer

Answer: tuple

Tuples cannot be changed after creation.

Lesson: Async Basics

9. What does `[n*n for n in range(4)]` produce?

  1. [1, 4, 9, 16]
  2. [0, 1, 2, 3]
  3. [0, 2, 4, 6]
  4. [0, 1, 4, 9]
Show answer

Answer: [0, 1, 4, 9]

range(4) yields 0..3 and each is squared.

Lesson: Async Basics

10. Why avoid `def f(x, items=[])`?

  1. The list is created once and shared across calls
  2. It is a syntax error
  3. It is slower
  4. It cannot be called
Show answer

Answer: The list is created once and shared across calls

Mutable defaults persist between calls; use None or a factory.

Lesson: Async Basics

11. What does `with open(...) as f:` guarantee?

  1. The file is encrypted
  2. The file is closed even if an error occurs
  3. The file is created
  4. The file is read-only
Show answer

Answer: The file is closed even if an error occurs

Context managers run cleanup automatically.

Lesson: Async Basics

12. What does `yield` do in a function?

  1. Ends the program
  2. Raises an exception
  3. Produces a value lazily and pauses the function
  4. Returns a list
Show answer

Answer: Produces a value lazily and pauses the function

Generators resume where they paused on the next call.

Lesson: Async Basics

13. Which decorator creates an alternate constructor?

  1. @staticmethod
  2. @property
  3. @dataclass
  4. @classmethod
Show answer

Answer: @classmethod

A classmethod receives cls and can return a new instance.

Lesson: Async Basics

14. Which keyword starts a coroutine?

  1. async def
  2. yield def
  3. lambda
  4. class
Show answer

Answer: async def

`async def` defines a coroutine you can await.

Lesson: Async Basics

15. Which tool isolates a project's packages?

  1. os
  2. venv
  3. sys
  4. math
Show answer

Answer: venv

A virtual environment keeps dependencies per project.

Lesson: Async Basics

← Back to the course