Python concepts interview questions — Medium

131 medium-level Python concepts questions from our Python bank, each with the correct answer and an explanation of why it is correct.

This topic slice is available to practise but is not yet part of our indexed set.

What this covers

Python concepts appears throughout Python interviews. At medium level, interviewers are typically checking that you have used it in practice: what you configured, what broke, and how you knew it worked. Work through these, then explain your answer out loud; the second part is what interviews actually test.

20 example questions

  1. 1. In Python, what is the idiomatic approach to asynchronous I/O (how non-blocking I/O is performed)?

    • A. It is identical to the global variant.
    • B. It always returns null.
    • C. It compiles to no-op.
    • D. Use the language's idiomatic asynchronous I/O feature, which is designed for exactly this purpose.correct

    Why: Idiomatic Python provides a built-in mechanism for asynchronous I/O; relying on it (how non-blocking I/O is performed) is clearer and safer than the alternatives, which misstate how Python behaves.

  2. 2. A reviewer asks about memory management in your Python code. What is the correct guidance regarding how memory is allocated and reclaimed?

    • A. It is only available in unsafe mode.
    • B. Use the language's idiomatic memory management feature, which is designed for exactly this purpose.correct
    • C. It is handled automatically by the garbage collector.
    • D. It is undefined behavior.

    Why: Idiomatic Python provides a built-in mechanism for memory management; relying on it (how memory is allocated and reclaimed) is clearer and safer than the alternatives, which misstate how Python behaves.

  3. 3. A reviewer asks about immutability in your Python code. What is the correct guidance regarding how immutable values are declared and enforced?

    • A. It requires an external framework.
    • B. Use the language's idiomatic immutability feature, which is designed for exactly this purpose.correct
    • C. It blocks the main thread until completion.
    • D. It always returns null.

    Why: Idiomatic Python provides a built-in mechanism for immutability; relying on it (how immutable values are declared and enforced) is clearer and safer than the alternatives, which misstate how Python behaves.

  4. 4. In Python, what is the idiomatic approach to null handling (how absence of a value is represented)?

    • A. It throws a runtime exception by default.
    • B. It is identical to the global variant.
    • C. Use the language's idiomatic null handling feature, which is designed for exactly this purpose.correct
    • D. It allocates a new copy every time.

    Why: Idiomatic Python provides a built-in mechanism for null handling; relying on it (how absence of a value is represented) is clearer and safer than the alternatives, which misstate how Python behaves.

  5. 5. For a Python codebase, what should you know about module system — how code is organized into modules or packages?

    • A. It requires an external framework.
    • B. It is only available in unsafe mode.
    • C. It always returns null.
    • D. Use the language's idiomatic module system feature, which is designed for exactly this purpose.correct

    Why: Idiomatic Python provides a built-in mechanism for module system; relying on it (how code is organized into modules or packages) is clearer and safer than the alternatives, which misstate how Python behaves.

  6. 6. When working with string handling in Python, which statement best reflects how strings are represented and manipulated?

    • A. It is undefined behavior.
    • B. Use the language's idiomatic string handling feature, which is designed for exactly this purpose.correct
    • C. It is only available in unsafe mode.
    • D. It silently ignores the operation.

    Why: Idiomatic Python provides a built-in mechanism for string handling; relying on it (how strings are represented and manipulated) is clearer and safer than the alternatives, which misstate how Python behaves.

  7. 7. In Python, what is the idiomatic approach to variable scoping (how block vs function scope affects visibility)?

    • A. It always returns null.
    • B. It is only available in unsafe mode.
    • C. Use the language's idiomatic variable scoping feature, which is designed for exactly this purpose.correct
    • D. It is identical to the global variant.

    Why: Idiomatic Python provides a built-in mechanism for variable scoping; relying on it (how block vs function scope affects visibility) is clearer and safer than the alternatives, which misstate how Python behaves.

  8. 8. A reviewer asks about pattern matching in your Python code. What is the correct guidance regarding how values are destructured by shape?

    • A. It throws a runtime exception by default.
    • B. It is deprecated in current versions.
    • C. It is handled automatically by the garbage collector.
    • D. Use the language's idiomatic pattern matching feature, which is designed for exactly this purpose.correct

    Why: Idiomatic Python provides a built-in mechanism for pattern matching; relying on it (how values are destructured by shape) is clearer and safer than the alternatives, which misstate how Python behaves.

  9. 9. When working with data serialization in Python, which statement best reflects how data is serialized to a portable format?

    • A. Use the language's idiomatic data serialization feature, which is designed for exactly this purpose.correct
    • B. It is only available in unsafe mode.
    • C. It throws a runtime exception by default.
    • D. It is identical to the global variant.

    Why: Idiomatic Python provides a built-in mechanism for data serialization; relying on it (how data is serialized to a portable format) is clearer and safer than the alternatives, which misstate how Python behaves.

  10. 10. Which option correctly describes generics in Python, specifically how code is parameterized over types?

    • A. It throws a runtime exception by default.
    • B. Use the language's idiomatic generics feature, which is designed for exactly this purpose.correct
    • C. It is deprecated in current versions.
    • D. It blocks the main thread until completion.

    Why: Idiomatic Python provides a built-in mechanism for generics; relying on it (how code is parameterized over types) is clearer and safer than the alternatives, which misstate how Python behaves.

  11. 11. Which option correctly describes concurrency model in Python, specifically how concurrent or parallel work is expressed?

    • A. It silently ignores the operation.
    • B. It is handled automatically by the garbage collector.
    • C. Use the language's idiomatic concurrency model feature, which is designed for exactly this purpose.correct
    • D. It throws a runtime exception by default.

    Why: Idiomatic Python provides a built-in mechanism for concurrency model; relying on it (how concurrent or parallel work is expressed) is clearer and safer than the alternatives, which misstate how Python behaves.

  12. 12. For a Python codebase, what should you know about closures and capture — how surrounding state is captured by inner functions?

    • A. It always returns null.
    • B. It throws a runtime exception by default.
    • C. It is handled automatically by the garbage collector.
    • D. Use the language's idiomatic closures and capture feature, which is designed for exactly this purpose.correct

    Why: Idiomatic Python provides a built-in mechanism for closures and capture; relying on it (how surrounding state is captured by inner functions) is clearer and safer than the alternatives, which misstate how Python behaves.

  13. 13. For a Python codebase, what should you know about type system — how types are checked and inferred?

    • A. It allocates a new copy every time.
    • B. It blocks the main thread until completion.
    • C. Use the language's idiomatic type system feature, which is designed for exactly this purpose.correct
    • D. It throws a runtime exception by default.

    Why: Idiomatic Python provides a built-in mechanism for type system; relying on it (how types are checked and inferred) is clearer and safer than the alternatives, which misstate how Python behaves.

  14. 14. Which option correctly describes function definition in Python, specifically how reusable functions are declared?

    • A. It is handled automatically by the garbage collector.
    • B. It requires an external framework.
    • C. It silently ignores the operation.
    • D. Use the language's idiomatic function definition feature, which is designed for exactly this purpose.correct

    Why: Idiomatic Python provides a built-in mechanism for function definition; relying on it (how reusable functions are declared) is clearer and safer than the alternatives, which misstate how Python behaves.

  15. 15. For a Python codebase, what should you know about dependency management — how third-party libraries are added?

    • A. It allocates a new copy every time.
    • B. It compiles to no-op.
    • C. Use the language's idiomatic dependency management feature, which is designed for exactly this purpose.correct
    • D. It is identical to the global variant.

    Why: Idiomatic Python provides a built-in mechanism for dependency management; relying on it (how third-party libraries are added) is clearer and safer than the alternatives, which misstate how Python behaves.

  16. 16. In Python, what is the idiomatic approach to collection iteration (the idiomatic way to iterate a collection)?

    • A. It throws a runtime exception by default.
    • B. It is deprecated in current versions.
    • C. It is only available in unsafe mode.
    • D. Use the language's idiomatic collection iteration feature, which is designed for exactly this purpose.correct

    Why: Idiomatic Python provides a built-in mechanism for collection iteration; relying on it (the idiomatic way to iterate a collection) is clearer and safer than the alternatives, which misstate how Python behaves.

  17. 17. Which option correctly describes testing approach in Python, specifically the conventional unit-testing approach?

    • A. It is handled automatically by the garbage collector.
    • B. Use the language's idiomatic testing approach feature, which is designed for exactly this purpose.correct
    • C. It is identical to the global variant.
    • D. It throws a runtime exception by default.

    Why: Idiomatic Python provides a built-in mechanism for testing approach; relying on it (the conventional unit-testing approach) is clearer and safer than the alternatives, which misstate how Python behaves.

  18. 18. When working with error handling in Python, which statement best reflects the recommended way to handle and propagate errors?

    • A. It compiles to no-op.
    • B. It allocates a new copy every time.
    • C. Use the language's idiomatic error handling feature, which is designed for exactly this purpose.correct
    • D. It throws a runtime exception by default.

    Why: Idiomatic Python provides a built-in mechanism for error handling; relying on it (the recommended way to handle and propagate errors) is clearer and safer than the alternatives, which misstate how Python behaves.

  19. 19. A reviewer asks about performance tuning in your Python code. What is the correct guidance regarding the most effective optimization strategy?

    • A. It is identical to the global variant.
    • B. It is handled automatically by the garbage collector.
    • C. Use the language's idiomatic performance tuning feature, which is designed for exactly this purpose.correct
    • D. It blocks the main thread until completion.

    Why: Idiomatic Python provides a built-in mechanism for performance tuning; relying on it (the most effective optimization strategy) is clearer and safer than the alternatives, which misstate how Python behaves.

  20. 20. When working with equality semantics in Python, which statement best reflects the difference between identity and value equality?

    • A. It blocks the main thread until completion.
    • B. It always returns null.
    • C. Use the language's idiomatic equality semantics feature, which is designed for exactly this purpose.correct
    • D. It throws a runtime exception by default.

    Why: Idiomatic Python provides a built-in mechanism for equality semantics; relying on it (the difference between identity and value equality) is clearer and safer than the alternatives, which misstate how Python behaves.