How Well Do You Understand Python?

4


Gensen Huang
Idyllic Icon

Gensen Huang

Created 6/20/2024

4.0k

75.7%

Q & A


Share This Quiz

Sources

https://raw.githubusercontent.com/WeitaoZhu/Python/1987308e0acff0fa5a42252557a91d0a2bb3d878/Fluent.Python.2nd.Edition.(z-lib.org).pdf

Put your Python knowledge to the test with this quiz and see how many correct answers you can get out of 20!

Put your Python knowledge to the test with this quiz and see how many correct answers you can get out of 20!

1. Which of the following is a characteristic of Python's garbage collection mechanism?

Manual memory management
Reference counting and a cyclic garbage collector
Only manual memory deallocation
No garbage collection

2. Which feature is NOT inherited by Python from the ABC language?

Built-in tuple types
Structure by indentation
Strong typing without variable declarations
Object-oriented programming

3. What is 'Timsort' in Python?

A type of variable
A sorting algorithm
A data structure
A built-in function

4. In Python, which statement is used to handle exceptions?

try/except
catch/throw
validate/rescue
attempt/handle

5. Which of the following is NOT an immutable sequence type in Python?

tuple
string
list
bytes

6. What does the 'yield' keyword do in Python?

Exits a function and returns a value
Causes the function to return a generator
Creates a lambda function
Defines a new variable

7. In Python, what does the '*' operator do when used in a function definition?

Unpacks a list or tuple
Creates a pointer
Raises an error
Multiplies values

8. What does the `enumerate()` function do when applied to a list?

Counts the elements in the list
Returns an iterator that yields pairs of index and value
Sorts the list in-place
Concatenates the list elements into a string

9. Which Python feature allows for the creation of one-liner loops and conditionals inside a list declaration?

Lambda functions
List comprehensions
Generator expressions
Map functions

10. Which built-in function can be used to display the documentation of a function object?

manual()
doc()
info()
help()

11. What is the primary function of the `__init__` method in a class?

To initialize the class object
To destroy the class object
To define class-level variables
To create private variables

12. What does `self` refer to in a class method?

The class itself
A global variable
The instance calling the method
None of the above

13. Which Python feature allows a quick check if an object supports a protocol via try/except without using `isinstance` or `hasattr`?

Duck Typing
Strict Typing
Static Typing
Dynamic Typing

14. What is the difference between `list.sort()` and `sorted(list)`?

list.sort() sorts the list in place, while sorted(list) returns a new sorted list.
list.sort() returns a new sorted list, while sorted(list) sorts the list in place.
There is no difference
list.sort() can sort lists of mixed data types, while sorted(list) cannot.

15. Which method is used to replace parts of a string in Python?

replace()
substitute()
substring()
swap()

16. What does the `defaultdict` class from the collections module do?

Automatically handles missing keys by inserting a default value
Generates a dictionary from a list
Removes duplicate keys
Sorts dictionary keys

17. What is a primary advantage of using Python's `deque` over a list for queue operations?

More consistent syntax
Efficient append and pop operations from both ends
Increased security
Less memory usage

18. Which of the following is NOT a built-in sequence type in Python?

string
list
tuple
map

19. What is a Python decorator typically used for?

To modify or extend the behavior of a function or method
To define a new class
To execute a function in parallel
To import modules

20. What is the primary purpose of the `with` statement in Python?

To create a loop
To open files and handle exceptions
To define a class
To handle context management