A script uses `subprocess.run(['ping', '-c', '1', host], capture_output=True,
text=True, check=True)` inside a loop over 10,000 hosts. It fails on the first
unreachable host. Which change best preserves error handling while avoiding a
crash?
A. Replace check=True with check=False and inspect returncode for each
host.
B. Set check=True but wrap the call in a try/except
subprocess.CalledProcessError.
C. Use subprocess.Popen with shell=True and ignore errors.
D. Remove capture_output=True to reduce memory usage.
Correct Answer: B - Set check=True but wrap the call in a
try/except subprocess.CalledProcessError.
RATIONALE
Wrapping in try/except subprocess.CalledProcessError allows the loop
to continue while still detecting failures via the exception. Option A
also avoids crashing but loses the explicit exception handling and
requires manual returncode checks, which is less idiomatic. Option C
introduces shell injection risks and ignores errors. Option D does not
address the error.
Question 2
When automating REST API calls with the `requests` library, which approach
best handles rate limiting (HTTP 429) while maintaining resilience and
avoiding infinite loops?
A. Retry immediately in a while loop until success.
B. Use a fixed 1-second delay between retries and ignore the Retry-After
header.
C. Implement exponential backoff with a maximum retry limit, respecting
the Retry-After header if present.
Page 2
, D. Catch the exception and log it, then continue without retrying.
Correct Answer: C - Implement exponential backoff with a
maximum retry limit, respecting the Retry-After header if
present.
RATIONALE
Exponential backoff with a retry limit prevents overwhelming the
server and avoids infinite loops, while respecting Retry-After aligns
with server guidance. Option A can cause hammering and infinite
loops. Option B ignores server-provided timing and may still hit rate
limits. Option D abandons the request entirely.
Question 3
A Python script uses `os.walk()` to delete all `.tmp` files in a directory tree.
Which modification ensures that symbolic links to directories are not followed,
preventing unintended deletions outside the tree?
A. Set `followlinks=False` in `os.walk()` (default).
B. Use `os.path.islink()` to check each file before deletion.
C. Set `followlinks=True` to ensure all links are processed.
D. Replace `os.walk()` with `glob.glob('**/*.tmp', recursive=True)`.
Correct Answer: A - Set `followlinks=False` in `os.walk()`
(default).
RATIONALE
By default, `os.walk()` does not follow symbolic links to directories
(followlinks=False), which prevents traversal outside the intended
tree. Option B checks files, not directory links, and may miss
symlinked directories. Option C explicitly enables following links,
increasing risk. Option D with `glob` may also follow symlinks
depending on the Python version and flags.
Page 3