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.
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?
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.
What does an empty list evaluate to in a Python conditional statement such as 'if my_list:'?
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.
What is a common pitfall of using a mutable object like a list as a default argument in a Python function?
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.
If a local variable has the same name as a global variable in a function (without using 'global'), what happens inside the function?
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.
What happens if you try to change a character in a Python string using assignment, such as my_str[0] = 'A'?
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.
Which key feature distinguishes a tuple from a list in Python?
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.
Which property of sets guarantees that adding duplicate elements does not change the set?
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.
What is the main difference between the 'is' and '==' operators in Python?
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.
Which of these types is invalid as a dictionary key in Python?
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.
What is a major limitation of lambda functions in Python compared to regular 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.
What will be the result of the expression '7 // 2' in Python?
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.
What happens if you use 'my_list[:]' to copy a list in Python?
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.
Which of these expressions will properly join the list words = ['Python', 'Quiz'] into 'Python Quiz'?
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.
What will be the output of the following list comprehension: [x*2 for x in [1, 2, 3]]?
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.
What does the Boolean value of the integer 0 evaluate to in Python?
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.
What is the purpose of the 'else' block in a Python for-else 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.