POC Troubleshooting & Problem Resolution 2 — Questions and Answers
Question 1: A Python script raises `RecursionError: maximum recursion depth exceeded`. What is the most appropriate fix?
- Add a base case to terminate recursion (Correct answer)
- Increase the recursion limit with sys.setrecursionlimit()
- Convert all integers to floats
- Use a try/except block to catch the error and ignore it
Correct answer: Add a base case to terminate recursion
A missing or unreachable base case is the root cause of infinite recursion; adding one stops the runaway calls.
Question 2: You call `int('3.14')` and get a `ValueError`. How do you convert the string '3.14' to an integer correctly?
- int(float('3.14')) (Correct answer)
- int('3.14', base=10)
- str(3.14).int()
- round('3.14')
Correct answer: int(float('3.14'))
`int()` cannot parse a string containing a decimal point, so you must first convert to float then to int.
Question 3: A `KeyError` occurs when accessing `my_dict['name']`. Which approach gracefully handles a missing key?
- my_dict.get('name', 'default') (Correct answer)
- my_dict['name'] or 'default'
- my_dict.fetch('name')
- del my_dict['name']
Correct answer: my_dict.get('name', 'default')
`dict.get(key, default)` returns the default value instead of raising `KeyError` when the key is absent.
Question 4: Your script produces `IndentationError: unexpected indent`. What is the most likely cause?
- Mixing tabs and spaces in the same block (Correct answer)
- Using a variable before assigning it
- Calling a function that doesn't exist
- Dividing by zero inside a loop
Correct answer: Mixing tabs and spaces in the same block
Python treats tabs and spaces as different characters, so mixing them causes inconsistent indentation levels.
Question 5: A `TypeError: 'NoneType' object is not subscriptable` appears. What likely happened?
- A function returned None and the result was indexed with [] (Correct answer)
- An integer was added to a string
- A list was divided by a number
- A module was imported incorrectly
Correct answer: A function returned None and the result was indexed with []
This error means you tried to use subscript notation on a None value, often from a function that returns nothing explicitly.
Question 6: The `pdb` debugger is running and you want to step INTO the next function call. Which command do you use?
- s (Correct answer)
- n
- c
- r
Correct answer: s
`s` (step) enters the function being called, whereas `n` (next) executes it and stays in the current frame.
Question 7: You see `MemoryError` while building a large list. Which technique reduces memory usage most effectively?
- Use a generator expression instead of a list comprehension (Correct answer)
- Wrap the list in a tuple
- Call gc.collect() before building the list
- Import the list from a module
Correct answer: Use a generator expression instead of a list comprehension
Generators yield items one at a time and never store the entire sequence in memory, unlike list comprehensions.
A Python script raises `RecursionError: maximum recursion depth exceeded`.
What is the most appropriate fix?