OBJECTIVE ASSESSMENT PRACTICE 2026/2027 | Verified
Code-Centric & Scenario-Based Questions and Answers
| Grade A Target | Pass Guaranteed
SECTION 1: Python Foundations & Scripting Environment (Questions 1-10)
Q1: An IT automation script needs to process a list of server hostnames and ensure no
duplicates exist while maintaining the original order of first appearance. Which Python code
segment accomplishes this most efficiently?
A. unique_servers = list(set(server_list)) B. unique_servers = sorted(set(server_list),
key=server_list.index) C. seen = set(); unique_servers = [x for x in server_list if not (x in seen or
seen.add(x))] [CORRECT] D. unique_servers = []; [unique_servers.append(x) for x in server_list if
x not in unique_servers]
Correct Answer: C
Rationale: Option C uses a set for O(1) membership testing while preserving order through a list
comprehension with side effect. The seen set tracks encountered items; the or short-circuit
ensures add() only executes if x not in seen is True. This achieves O(n) time complexity. Option A
loses original order (sets are unordered). Option B is O(n²) due to index() calls. Option D is O(n²)
due to linear not in searches on a growing list. For IT automation processing large infrastructure
inventories, Option C provides optimal performance. [CORRECT: C]
Q2: A system administrator is writing a script to handle potential file access errors. Which
exception handling structure follows Python best practices for catching specific errors while
avoiding bare except clauses?
A.
Python
Copy
try:
with open('/etc/passwd', 'r') as f:
, data = f.read()
except:
print("Error occurred")
B.
Python
Copy
try:
with open('/etc/passwd', 'r') as f:
data = f.read()
except FileNotFoundError as e:
logger.error(f"Password file missing: {e}")
except PermissionError as e:
logger.error(f"Insufficient permissions: {e}")
except Exception as e:
logger.critical(f"Unexpected error: {e}")
raise
``` [CORRECT]
C.
```python
try:
f = open('/etc/passwd', 'r')
data = f.read()
except (FileNotFoundError, PermissionError, OSError):
print("File access failed")
finally:
, f.close()
D.
Python
Copy
try:
data = open('/etc/passwd', 'r').read()
except Exception:
pass
Correct Answer: B
Rationale: Option B demonstrates proper exception hierarchy: specific exceptions
(FileNotFoundError, PermissionError) are caught first for targeted handling, followed by a
general Exception handler that logs and re-raises unexpected errors. This follows PEP 8 and the
Zen of Python ("Errors should never pass silently"). The with statement ensures proper resource
cleanup. Option A uses a bare except, catching KeyboardInterrupt and SystemExit
inappropriately. Option C risks UnboundLocalError if open() fails before f is assigned. Option D
silently suppresses all errors, masking critical failures in automation scripts. [CORRECT: B]
Q3: [Code Analysis] What is the output of the following script when executed on a Linux
system?
Python
Copy
#!/usr/bin/env python3
import sys
def main():
if len(sys.argv) < 2:
print("Usage: script.py <hostname>", file=sys.stderr)
sys.exit(1)
, hostname = sys.argv[1]
domain_parts = hostname.split('.')
if len(domain_parts) >= 2:
print(f"Domain: {'.'.join(domain_parts[-2:])}")
else:
print("Invalid FQDN")
if __name__ == "__main__":
main()
Executed as: python3 script.py web01.prod.example.com
A. Domain: web01.prod B. Domain: example.com [CORRECT] C. Domain: prod.example.com D.
Usage: script.py <hostname>
Correct Answer: B
Rationale: The script extracts the last two domain components. hostname.split('.') produces
['web01', 'prod', 'example', 'com']. The slice [-2:] selects ['example', 'com'], which join()
combines as "example.com". This is a common pattern in IT automation for extracting domain
information from FQDNs. Option A incorrectly takes the first two elements. Option C takes
elements 1-3 (index 1 onward). Option D would only occur with no command-line arguments
provided. [CORRECT: B]
Q4: Which shebang line should be used for a Python 3 automation script intended to run across
heterogeneous Unix-like environments (Linux, macOS, BSD) where Python may be installed in
various locations?
A. #!/bin/python3 B. #!/usr/bin/python3 C. #!/usr/bin/env python3 [CORRECT] D. #!python3
Correct Answer: C
Rationale: #!/usr/bin/env python3 uses the env command to locate python3 in the user's
$PATH, ensuring portability across systems where Python may be in /usr/bin, /usr/local/bin,