Tricky Python Gotchas Quiz


Quizz Master [Discord]

Quizz Master [Discord]

Created 6/13/2024

3

57.78%

Q & A


Share This Quiz

Sources

https://docs.python-guide.org/writing/gotchas/
https://inventwithpython.com/beyond/chapter8.html

Test your knowledge of Python with this challenging quiz on common pitfalls and unexpected behaviors in the language.

Test your knowledge of Python with this challenging quiz on common pitfalls and unexpected behaviors in the language.

1. 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.

2. 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.

3. 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.

4. Which Python method sorts items by their numeric code points?

sorted()
sort()
order()
arrange()

5. 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.

6. 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.

7. 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)

8. What is the result of chaining != operators in Python, like 'a' != 'b' != 'a'?

True
False
SyntaxError
TypeError

9. True or False: Using copy.copy() is sufficient for copying nested mutable objects.

True
False
Only for lists
Only for dictionaries

10. 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

11. 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

12. 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

13. What environment variable can you set to stop Python from generating .pyc files?

PYTHONNOPYC
DISABLEBYTECODE
PYTHONNOBYTECODE
PYTHONDONTWRITEBYTECODE

14. True or False: Variables in Python are similar to boxes that contain objects.

True
False
Only for immutable objects
Only for mutable objects

15. Which module can help you deal with floating-point rounding errors?

math
fractions
decimal
float