Python Interview Essentials: From Fundamentals to Advanced Concepts Quiz

Challenge your Python knowledge with this 15-question quiz covering everything from basic syntax, data types, and variables to advanced topics such as decorators, generators, and concurrency. Perfect for candidates preparing for Python interviews or anyone wanting a quick, effective review of essential Python programming concepts.

  1. Dynamic Typing

    What does it mean that Python is a dynamically typed language?

    1. Variable types are determined at runtime.
    2. All variables must be declared with their type.
    3. Only integer variables can be created.
    4. Typing errors can only be caught at compile time.

    Explanation: In Python, variable types are determined at runtime, making the language dynamically typed. Unlike statically typed languages, you do not have to explicitly declare a variable's type. Typing errors will not be caught at compile time because Python does not compile code in the same way as some other languages. Declaring variables as only integers is incorrect and restrictive.

  2. Integer Precision

    Which statement is true about integers in Python?

    1. Integers in Python have unlimited precision.
    2. Integers in Python have a maximum value of 2^31-1.
    3. Integer division always returns a float.
    4. Integers are mutable in Python.

    Explanation: Integers in Python can represent numbers of any size, limited only by your system's memory, giving them effectively unlimited precision. Python does not restrict integers to 2^31-1, as is common in some older or lower-level languages. Integer division with // returns another integer, and all integers are immutable objects in Python.

  3. String Immutability

    What happens if you try to modify an individual character of a Python string?

    1. A TypeError is raised because strings are immutable.
    2. The original string is changed.
    3. Characters in the string are shifted.
    4. A new character is appended to the string.

    Explanation: Strings in Python are immutable, so trying to change an individual character like my_str[0] = 'a' will raise a TypeError. The original string cannot change, and characters won't be shifted or appended in this way. To modify a string, you must create a new one.

  4. Floating-Point Precision

    Why does 0.1 + 0.2 == 0.3 evaluate to False in Python?

    1. Floating-point numbers have precision errors.
    2. Python does not support decimal arithmetic.
    3. Integer promotion causes the comparison to fail.
    4. The addition operator is redefined for floats.

    Explanation: Floating-point representation in computers cannot exactly express some decimal numbers, leading to precision errors like 0.1 + 0.2 not exactly equaling 0.3. Python does support decimal arithmetic, and the addition operator behaves normally for floats. Integer promotion is not a cause in this context.

  5. Type Conversion

    Which built-in function would you use to convert a string '7.5' into a floating-point number?

    1. float()
    2. int()
    3. str()
    4. bool()

    Explanation: The float() function converts a string representing a decimal number to its float value, so float('7.5') results in 7.5. int() tries to convert to an integer but fails on decimals. str() and bool() do not perform numeric conversions from strings.

  6. Type Checking

    If x = 10, what does type(x) return in Python?

    1. <class 'int'>
    2. <type 'integer'>
    3. <integer>
    4. 10

    Explanation: type(x) returns <class 'int'> to indicate that x is an integer object in Python 3. The other answers are not valid outputs of the type() function in Python 3, as '<type 'integer'>' was used in older Python versions and '10' is simply the value.

  7. For Loop Syntax

    Which of the following is a valid for loop in Python to print numbers 0 to 4?

    1. for i in range(5): print(i)
    2. for(i=0;i<5;i++) {print(i)}
    3. loop i from 0 to 4: print(i)
    4. for i = 0 to 4 print i

    Explanation: Python uses the syntax for i in range(5): print(i) to iterate from 0 to 4. The C-style loop and pseudo-code options are invalid in Python. Python also requires a colon and proper indentation in its loops.

  8. List Comprehension

    How can you create a list of the squares of even numbers from 0 to 9 in Python?

    1. [x**2 for x in range(10) if x % 2 == 0]
    2. [x^2 for x in range(10) if x == 2]
    3. {x*2 for x in range(10)}
    4. [x*x for x in range(0,10,2)]

    Explanation: List comprehensions in Python can include conditions, so [x**2 for x in range(10) if x % 2 == 0] creates squares of even numbers between 0 and 9. [x^2 ...] uses bitwise xor instead of exponentiation, and {x*2 ...} creates a set, not a list. [x*x for x in range(0,10,2)] creates squares of only even numbers, but misses the clarity of the conditional form.

  9. Mutable Default Arguments

    Why is it risky to use a mutable object like a list as a default value for a Python function argument?

    1. Because the default is shared across calls.
    2. Because lists cannot be used as default values.
    3. Because it causes a syntax error.
    4. Because it is slower than using None.

    Explanation: A mutable default argument like a list is shared between all calls to the function, which can lead to unintended behavior. It's not prohibited or a syntax error, but can introduce subtle bugs. While performance may be affected in rare cases, the main issue is unexpected sharing.

  10. Dictionaries

    What is the primary use of a dictionary in Python?

    1. To store key-value pairs
    2. To store only unique values
    3. To sort a list automatically
    4. To enforce immutability

    Explanation: Dictionaries in Python are used to store data as key-value pairs, allowing efficient lookups. They do not enforce uniqueness of values, nor do they automatically sort items. Dictionaries themselves are mutable, so they do not enforce immutability.

  11. Inheritance

    What does inheritance allow in Python's object-oriented programming?

    1. A class to use methods and properties of another class.
    2. A class to have multiple constructors only.
    3. Only data hiding within classes.
    4. A class to become immutable.

    Explanation: Inheritance allows one class to use and override the methods and properties of another, supporting code reuse and logical structure. It does not offer multiple constructors or data hiding exclusively. Making a class immutable is unrelated to inheritance.

  12. Decorators

    What is the primary purpose of a decorator in Python?

    1. To modify or extend a function's behavior
    2. To create new variable types
    3. To check for syntax errors
    4. To encrypt data

    Explanation: Decorators are functions that modify or extend the behavior of the functions or methods they wrap. They do not introduce new types, check for syntax errors, or handle encryption. Decorators are frequently used for logging, access control, or other enhancements.

  13. Generators

    Which keyword is used in Python to define a generator?

    1. yield
    2. return
    3. break
    4. continue

    Explanation: The yield keyword converts a function into a generator, which can be iterated over to produce values one at a time. The return keyword simply exits a function and gives back a value. The break and continue keywords are used within loops, not for defining generators.

  14. Concurrency

    Which Python library can be used for running code in parallel on multiple CPU cores?

    1. multiprocessing
    2. math
    3. sys
    4. logging

    Explanation: The multiprocessing library allows you to create processes that run in parallel, making use of multiple CPU cores. The math library provides mathematical functions, sys is for system-specific parameters, and logging is for creating log messages.

  15. Sets

    What is a main characteristic of a set in Python?

    1. It contains only unique items.
    2. It preserves the order of elements.
    3. It allows duplicates.
    4. It stores data as key-value pairs.

    Explanation: Sets in Python are collections of unique, unordered items. They do not preserve the order of elements, allow duplicates, or store key-value pairs. Sets are commonly used to test membership and remove duplicates.

  16. range() Function

    If you call range(1, 6), what sequence is generated?

    1. 1, 2, 3, 4, 5
    2. 1, 2, 3, 4, 5, 6
    3. 0, 1, 2, 3, 4
    4. 1, 2, 3, 4

    Explanation: The range(1, 6) function generates numbers from 1 up to, but not including, 6: that is, 1, 2, 3, 4, 5. Including 6 is incorrect because the stop value is exclusive. Starting from 0 or ending at 4 also does not match the provided parameters.