POC Troubleshooting & Problem Resolution 3 — Questions and Answers
Question 1: A `FileNotFoundError` is raised when opening a file. Which debugging step should you take first?
- Print or log the exact file path being used at runtime (Correct answer)
- Reinstall Python
- Switch from open() to read()
- Wrap the call in a while loop
Correct answer: Print or log the exact file path being used at runtime
Runtime paths often differ from what developers assume; printing the resolved path immediately reveals mismatches.
Question 2: You catch an exception with `except Exception as e:` but lose the original traceback. How do you re-raise it with the full traceback intact?
- Use `raise` with no arguments inside the except block (Correct answer)
- Use `raise Exception(e)`
- Call `traceback.print_exc()` then `sys.exit()`
- Use `raise e` with the variable
Correct answer: Use `raise` with no arguments inside the except block
A bare `raise` inside an except block re-raises the current exception preserving its original traceback.
Question 3: Your unit test passes locally but fails in CI because a file path uses backslashes. What is the portable fix?
- Use os.path.join() or pathlib.Path() to construct paths (Correct answer)
- Replace all backslashes with forward slashes manually
- Set os.sep = '/' at the top of the script
- Use raw strings r'path\to\file' everywhere
Correct answer: Use os.path.join() or pathlib.Path() to construct paths
`os.path.join()` and `pathlib.Path` use the correct separator for the current OS, making paths platform-independent.
Question 4: A `UnicodeDecodeError` occurs when reading a file. Which argument to `open()` most likely fixes it?
- encoding='utf-8' (Correct answer)
- mode='b'
- errors='ignore' only (without specifying encoding)
- newline='\n'
Correct answer: encoding='utf-8'
Specifying the correct encoding (commonly UTF-8) tells Python how to decode the raw bytes in the file.
Question 5: You have an infinite loop in a running Python script. Which is the quickest way to stop it without killing the terminal?
- Press Ctrl+C to send a KeyboardInterrupt (Correct answer)
- Close and reopen the terminal
- Press Ctrl+Z to pause and then run kill %1
- Type exit() in the same window
Correct answer: Press Ctrl+C to send a KeyboardInterrupt
Ctrl+C sends SIGINT to the process, which Python converts to a `KeyboardInterrupt` and stops the loop.
Question 6: After refactoring, you get `AttributeError: module 'mymodule' has no attribute 'helper'`. What should you check first?
- Whether the function was renamed or deleted in mymodule.py (Correct answer)
- Whether Python is installed correctly
- Whether the module is imported twice
- Whether the function has a return statement
Correct answer: Whether the function was renamed or deleted in mymodule.py
The error directly states the attribute is missing, so verifying the module's current contents is the logical first step.
Question 7: A script occasionally raises `ZeroDivisionError` depending on input data. What is the best defensive approach?
- Validate the divisor before dividing and handle the zero case explicitly (Correct answer)
- Wrap every arithmetic operation in a try/except
- Replace division with multiplication by a reciprocal
- Cast the divisor to float before dividing
Correct answer: Validate the divisor before dividing and handle the zero case explicitly
Checking preconditions (guard clauses) prevents the error from occurring rather than catching it after the fact.
A `FileNotFoundError` is raised when opening a file.
Which debugging step should you take first?