A script that reads a CSV with a header row and writes an updated CSV fails
intermittently under concurrent runs, producing truncated output. Which
change best ensures atomic, consistent writes without sacrificing
cross-platform behavior?
A. Open the destination with mode 'w' and call file.flush() after every row
write.
B. Write to a temp file in the same directory, flush and os.fsync it, then
use os.replace() to atomically swap it into place.
C. Wrap the entire write in a threading.Lock and continue writing directly
to the destination file.
D. Open the destination with mode 'a' and rely on the OS to serialize
concurrent appends.
Correct Answer: B - Write to a temp file in the same directory,
flush and os.fsync it, then use os.replace() to atomically swap it
into place.
RATIONALE
Atomic replacement via os.replace() on a same-directory temp file
prevents readers from observing partial writes and is atomic on POSIX
and Windows. flush() alone does not prevent truncation visibility, a
threading.Lock does not protect across processes, and append mode
still allows interleaved partial records.
Question 2
An automation script raises `KeyError: 'hostname'` only when run against a
particular API response. Which modification most directly produces a resilient
parse while preserving diagnostic information?
A. Wrap the parse in `try/except Exception: pass` to avoid crashing.
B. Replace dict access with `response.get('hostname')` and log a
structured warning when the key is absent.
Page 2
, C. Convert the response to a string and search for the substring
'hostname'.
D. Set a module-level default `hostname = None` and reference it
globally.
Correct Answer: B - Replace dict access with
`response.get('hostname')` and log a structured warning when the
key is absent.
RATIONALE
Using .get() with an explicit warning preserves execution and records
the missing-key condition for troubleshooting. Blanket except
suppresses evidence, substring search is brittle, and a global default
obscures the actual data gap.
Question 3
A regex intended to validate IPv4 addresses uses `^\d{1,3}(\.\d{1,3}){3}$`.
Which input incorrectly passes this pattern, demonstrating the flaw?
A. 192.168.1.1
B. 999.999.999.999
C. 10.0.0.1
D. 172.16.254.1
Correct Answer: B - 999.999.999.999
RATIONALE
The pattern only constrains each octet to 1-3 digits, so
999.999.999.999 matches despite being invalid. Proper validation
must bound each octet to 0-255, which the other listed addresses
satisfy as valid inputs.
Page 3
, Question 4
A subprocess call to a long-running CLI hangs indefinitely in CI but works
locally. Which adjustment most robustly addresses the root cause while
retaining captured output?
A. Use subprocess.run(..., shell=True) to inherit the parent shell
environment.
B. Use subprocess.run(..., timeout=N, capture_output=True) and handle
subprocess.TimeoutExpired.
C. Replace subprocess with os.system() to avoid pipe buffering.
D. Set stdin=subprocess.PIPE without providing input to force EOF.
Correct Answer: B - Use subprocess.run(..., timeout=N,
capture_output=True) and handle subprocess.TimeoutExpired.
RATIONALE
An explicit timeout with captured output prevents indefinite hangs and
surfaces the failure for troubleshooting. shell=True introduces
injection risk without solving hangs, os.system provides no output
capture, and an empty stdin pipe can itself cause blocking.
Question 5
A configuration loader must merge environment-specific overrides onto
defaults without mutating the defaults across repeated calls. Which
implementation correctly avoids shared-state bugs?
A. defaults.update(overrides); return defaults
B. return {**defaults, **overrides}
C. merged = defaults; merged.update(overrides); return merged
D. return dict.fromkeys(defaults, overrides)
Correct Answer: B - return {**defaults, **overrides}
Page 4