Tricky Python Gotchas Quiz
Test your knowledge of Python with this challenging quiz on common pitfalls and unexpected behaviors in the language.
Question 1 of 15Tricky Python Gotchas Quiz
All 15 questions
- What happens when you use a mutable default argument in a Python function?A new instance of the mutable object is created each time the function is called. · The mutable object is created once and shared between all calls to the function. · An error is raised each time the function is called. · The mutable object is reset to its default state each time the function is called.
- Why should you avoid adding or removing items from a list while iterating over it?It can cause an IndexError. · It can create an infinite loop or skip elements. · It will always result in a syntax error. · It makes the code slower.
- What is the primary reason string concatenation inside a loop is considered a 'gotcha'?It doesn't work with f-strings. · It causes the loop to terminate prematurely. · It leads to slow performance due to the creation and destruction of intermediate strings. · It results in a TypeError.
- Which Python method sorts items by their numeric code points?sorted() · sort() · order() · arrange()
- How would you properly handle mutable default arguments in a Python function?By using an immutable default value like None and then checking for it inside the function. · By using deepcopy to create a new instance each time. · By using a dictionary instead of a list. · By avoiding default arguments altogether.
- What happens if you forget the trailing comma in a single-item tuple?It's recognized as a tuple anyway. · It's recognized as the enclosed data type (e.g., int, string). · Python raises a SyntaxError. · The code doesn't compile.
- Which method can you use to ensure that strings are sorted in case-insensitive order?sorted(casefold=) · sort()` · sort(key=str.lower) · order(case-insensitive=True)
- What is the result of chaining != operators in Python, like 'a' != 'b' != 'a'?True · False · SyntaxError · TypeError
- True or False: Using copy.copy() is sufficient for copying nested mutable objects.True · False · Only for lists · Only for dictionaries
- What is demonstrated by the following code: spam = 'Hello', spam = spam + ' world!', id(spam)?String mutability · Memory address recycling · Late binding in closures · String immutability
- What is a recommended method for building a large string from multiple smaller strings in a loop?Using repeated += operations · Appending the smaller strings to a list and then joining the list · Concatenating directly in a loop · Using the '+' operator in an f-string
- How can you avoid Python’s late binding closure gotcha?Using a lambda function · Passing the variable as a default argument · Using functools.partial · Rewriting the logic without loops
- What environment variable can you set to stop Python from generating .pyc files?PYTHONNOPYC · DISABLEBYTECODE · PYTHONNOBYTECODE · PYTHONDONTWRITEBYTECODE
- True or False: Variables in Python are similar to boxes that contain objects.True · False · Only for immutable objects · Only for mutable objects
- Which module can help you deal with floating-point rounding errors?math · fractions · decimal · float





