Uncommon Python Interview Questions Quiz

Challenge yourself with this quiz on unusual Python interview questions rarely found in standard prep materials. Test your knowledge of subtle Python concepts and be better prepared for real-world interview curveballs.

  1. List Modification During Iteration

    What is the main risk when removing elements from a Python list while iterating over it, such as using a loop that deletes even numbers from a list?

    1. The list becomes sorted automatically.
    2. You may skip elements and introduce logic bugs.
    3. All elements are duplicated.
    4. The program crashes immediately.

    Explanation: When you remove elements from a list while iterating, the indices shift and you may skip checking certain items, leading to missed or unexpected behavior. The list will not sort itself, so 'The list becomes sorted automatically' is incorrect. The program normally does not crash, ruling out 'The program crashes immediately.' Elements are removed, not duplicated, making 'All elements are duplicated' an invalid option.

  2. Truthiness of Empty Containers

    What does an empty list evaluate to in a Python conditional statement such as 'if my_list:'?

    1. True
    2. None
    3. False
    4. Zero

    Explanation: Empty containers like lists, sets, and dictionaries evaluate as False in conditionals, so 'False' is correct. 'True' is only for non-empty lists. 'None' is not a Boolean value and is unrelated here. 'Zero' refers to numerical values, not to lists.

  3. Default Mutable Arguments

    What is a common pitfall of using a mutable object like a list as a default argument in a Python function?

    1. It automatically creates a new object every time.
    2. It slows down the interpreter.
    3. The same object is reused on each call.
    4. It causes a syntax error.

    Explanation: Mutable default arguments are created once and reused, leading to unexpected behavior if modified. 'It causes a syntax error' is false, because the code will run. Python does not create a new object every time, so that option is wrong. There is no significant interpreter slowdown from this; any bugs are from logic, not speed.

  4. Variable Scope Confusion

    If a local variable has the same name as a global variable in a function (without using 'global'), what happens inside the function?

    1. The local variable shadows the global one.
    2. Both variables are updated together.
    3. Python throws a NameError.
    4. The global variable is deleted.

    Explanation: Inside the function, the local variable takes precedence and hides the global variable with the same name. The variables are distinct, so 'Both variables are updated together' is incorrect. Python will not throw a NameError just because names match. The global variable is not deleted; it remains unchanged outside the function.

  5. String Immutability

    What happens if you try to change a character in a Python string using assignment, such as my_str[0] = 'A'?

    1. The rest of the string is deleted.
    2. Only the first character is changed.
    3. The string changes successfully.
    4. A TypeError is raised because strings are immutable.

    Explanation: Strings in Python are immutable, so trying to assign to an index raises a TypeError. The change is not successful and only changing the first character is not allowed. Deleting part of the string does not occur in this context.

  6. Tuple and List Differences

    Which key feature distinguishes a tuple from a list in Python?

    1. Tuples contain only numbers.
    2. Lists cannot be empty.
    3. Tuples are immutable, while lists are mutable.
    4. Lists are faster than tuples in every case.

    Explanation: Tuples cannot be changed after creation, while lists can, making this the correct distinction. Tuples can hold any object type, not just numbers, ruling out that option. Performance depends on context; lists are not always slower, but speed is not the defining characteristic. Lists can be empty, so the last option is false.

  7. Set Uniqueness

    Which property of sets guarantees that adding duplicate elements does not change the set?

    1. Sets have a fixed length.
    2. Elements are unique.
    3. Sets are immutable.
    4. Elements are ordered.

    Explanation: Sets in Python only allow unique elements, so adding duplicates has no effect. Elements are unordered, so 'Elements are ordered' is wrong. Sets are mutable (except frozensets), so 'immutable' is incorrect. Sets can grow and shrink as items are added or removed, meaning length is not fixed.

  8. The 'is' vs. '==' Operators

    What is the main difference between the 'is' and '==' operators in Python?

    1. 'is' checks for inherited methods, '==' checks for types.
    2. 'is' can only compare numbers.
    3. 'is' checks object identity, '==' checks value equality.
    4. 'is' is slower than '=='.

    Explanation: 'is' determines if two variables reference the exact same object, while '==' compares the values they hold. The operators do not check method inheritance. Speed and value types are not the key distinguishing points here. Either operator can compare many object types, not just numbers.

  9. Dictionary Key Validity

    Which of these types is invalid as a dictionary key in Python?

    1. A string
    2. An integer
    3. A tuple
    4. A list

    Explanation: Lists are mutable and unhashable, so they cannot be used as dictionary keys. Tuples, integers, and strings are hashable and can serve as keys. Only objects that are immutable and have a stable hash can act as keys, making lists invalid.

  10. Lambda Function Limitation

    What is a major limitation of lambda functions in Python compared to regular functions?

    1. They must return a string.
    2. They require the 'lambda' keyword at the end.
    3. They can only contain a single expression.
    4. They run significantly faster than normal functions.

    Explanation: Lambda functions must have a single expression with no statements or multiple lines. There is no requirement to return a string, and the 'lambda' keyword starts their definition, not ends it. Speed is similar to regular functions for small expressions, so running faster is not a key limitation.

  11. Integer Division Results

    What will be the result of the expression '7 // 2' in Python?

    1. 4
    2. 3
    3. 3.5
    4. 2

    Explanation: The '//' operator performs integer (floor) division and returns 3 in this case. '3.5' would be returned by regular division '/', not by floor division. The values 4 and 2 are incorrect for this particular expression.

  12. List Copy with ':' Slicing

    What happens if you use 'my_list[:]' to copy a list in Python?

    1. Both lists will always remain synchronized.
    2. A tuple is returned instead of a list.
    3. A new list with the same elements is created.
    4. The original list is deleted.

    Explanation: 'my_list[:]' creates a shallow copy of the original list, resulting in a new list with the same items. The slicing operation does not delete the original list, nor does it return a tuple. The new list is independent, so changes to one will not affect the other.

  13. Joining Strings with 'join'

    Which of these expressions will properly join the list words = ['Python', 'Quiz'] into 'Python Quiz'?

    1. 'words'.join(' ')
    2. join(words, ' ')
    3. words.join(' ')
    4. ' '.join(words)

    Explanation: The correct syntax is to call 'join' on the separator string, as in ' '.join(words). The alternative 'join(words, ' '),' does not exist in Python syntax. The other options either reverse the order or call 'join' incorrectly, making them invalid.

  14. List Comprehension Output

    What will be the output of the following list comprehension: [x*2 for x in [1, 2, 3]]?

    1. [2, 4, 6]
    2. [2, 3, 4]
    3. [1, 2, 3]
    4. [1, 4, 9]

    Explanation: The comprehension multiplies each number by 2, yielding [2, 4, 6]. '[1, 2, 3]' is the original list, not the result. '[1, 4, 9]' would be if x*x was used. '[2, 3, 4]' does not fit the multiplication pattern.

  15. Boolean Expressions

    What does the Boolean value of the integer 0 evaluate to in Python?

    1. None
    2. False
    3. Undefined
    4. True

    Explanation: In Python, zero is considered False in Boolean expressions. Only non-zero numbers evaluate as True. 'None' is a distinct value and not a Boolean. 'Undefined' is not a standard Boolean result in Python.

  16. for-else Statement Meaning

    What is the purpose of the 'else' block in a Python for-else loop?

    1. It runs if the loop completes without a break.
    2. It runs only if an item is found.
    3. It skips the last iteration.
    4. It always runs before the loop.

    Explanation: The else block in a for-else construct executes only if the loop is not prematurely broken with a break statement. It does not depend on finding an item. The else runs after, not before, and does not alter the loop's iterations directly.