How would you test a function that does not return a value, but prints something out, using unittest?
- Manually check the printed output during testing.
- Redirect the printed output to a file and compare the file contents in the test case.
- This cannot be tested with unittest as it's impossible to capture printed output.
- Use the unittest.mock library to capture the printed output and compare it to the expected output.
To test a function that prints something without returning a value, you can use the unittest.mock library to capture the printed output and then compare it to the expected output in your test case. This allows you to assert that the function is producing the expected output.
How would you use a metaclass to automatically register all subclasses of a base class in Python?
- Define a register_subclasses function within the base class.
- Subclasses cannot be automatically registered using metaclasses.
- Use the @register_subclass decorator in conjunction with a metaclass.
- You can use the __init_subclass__ method in a metaclass to automatically register subclasses.
The __init_subclass__ method in a metaclass allows you to automatically register subclasses when they are defined, enabling a way to track and manage them.
How would you optimize the performance of a RESTful API that serves large datasets?
- A. Use HTTP GET for all requests
- B. Implement pagination and filtering
- C. Remove all error handling for faster processing
- D. Use a single, monolithic server
B. Implementing pagination and filtering allows clients to request only the data they need, reducing the load on the server and improving performance. Options A, C, and D are not recommended practices and can lead to performance issues.
How would you implement a dequeue (double-ended queue) data structure?
- Array
- Linked List
- Queue
- Stack
A dequeue can be efficiently implemented using a doubly linked list, where you can add or remove elements from both ends in constant time. While arrays are also used, they may not provide the same level of efficiency for both ends' operations.
How would you implement a stack in Python?
- Using a dictionary
- Using a list
- Using a set
- Using a tuple
In Python, you can implement a stack using a list. Lists provide built-in methods like append() and pop() that make it easy to simulate a stack's behavior.
How would you implement rate limiting in a RESTful API to prevent abuse?
- A. Use JWT tokens
- B. Implement a token bucket algorithm
- C. Limit requests based on IP addresses
- D. Disable API access during peak hours
B. Implementing a token bucket algorithm is a common method for rate limiting in RESTful APIs. It allows you to control the rate of requests over time, preventing abuse while allowing legitimate usage. Options A, C, and D are not effective methods for rate limiting.
How would you initialize an empty list in Python?
- empty_list = []
- empty_list = [None]
- empty_list = {}
- empty_list = None
To initialize an empty list in Python, you use square brackets []. Option 2 initializes an empty dictionary, option 3 initializes a variable as None, and option 4 initializes a list with a single element, which is None.
How would you investigate memory leaks in a Python application?
- Manually inspect each variable in the code to find memory leaks.
- Use a memory profiler like memory_profiler to track memory usage over time.
- Use the psutil library to monitor CPU usage and infer memory leaks.
- Use the timeit module to measure execution time and find memory leaks.
To investigate memory leaks in a Python application, you can use a memory profiler like memory_profiler, which tracks memory usage over time, helping you identify areas of concern. Manual inspection (Option 3) is impractical for large codebases, and psutil (Option 2) primarily focuses on CPU usage. The timeit module (Option 4) measures execution time, not memory usage.
How would you optimize a Python function that is found to be CPU-bound during profiling?
- a) Use a Just-In-Time (JIT) compiler like PyPy.
- b) Increase the number of threads to parallelize the code.
- c) Optimize the algorithm or use data structures that are more efficient.
- d) Use a faster computer for running the code.
When a Python function is CPU-bound, the most effective optimization is usually to optimize the algorithm or use more efficient data structures. JIT compilation (a) can help in some cases, but it may not be as effective as algorithmic improvements. Increasing the number of threads (b) might help if the code can be parallelized, but this is not always the case. Using a faster computer (d) is generally not a solution to CPU-bound code as it doesn't address the underlying inefficiencies.
If multiple base classes have methods with the same name, method resolution in a derived class follows the _______ rule.
- FIFO (First In, First Out)
- LIFO (Last In, First Out)
- LOO (Last Out, Out)
- MRO (Method Resolution Order)
In Python, when multiple base classes have methods with the same name, the method resolution follows the Method Resolution Order (MRO) to determine which method to call.