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 handle a configuration value that may be missing. Which
code snippet properly implements safe retrieval with a default value?
A. value = config['timeout'] if config['timeout'] else 30 B. value = config.get('timeout', 30)
[CORRECT] C. value = config['timeout'] or 30 D. value = config.timeout || 30
Correct Answer: B
Rationale: The dict.get(key, default) method safely retrieves values without raising KeyError if
the key is absent. Option A fails with KeyError if 'timeout' doesn't exist. Option C also raises
KeyError before the or evaluation. Option D uses JavaScript syntax (||) and dot notation invalid
for Python dictionaries. This pattern is essential for robust configuration management in IT
automation.
Q2: [CODE ANALYSIS] What is the output of the following script?
Python
Copy
import sys
def main():
if len(sys.argv) != 3:
print("Usage: script.py <source> <destination>")
sys.exit(1)
, print(f"Copying {sys.argv[1]} to {sys.argv[2]}")
if __name__ == "__main__":
main()
Executed as: python script.py file1.txt
A. Copying file1.txt to B. Usage: script.py <source> <destination> [CORRECT] C. Copying file1.txt
to file1.txt D. No output; script hangs
Correct Answer: B
Rationale: The script validates argument count (len(sys.argv) includes script name, so 3
elements = 2 arguments). With only one argument provided, len(sys.argv) == 2, triggering the
usage message and sys.exit(1). This defensive pattern prevents undefined behavior and provides
clear user feedback—critical for IT automation tools used by operations teams.
Q3: Which shebang line should be used for a Python 3 automation script intended to run on
diverse Linux distributions?
A. #!/usr/bin/python B. #!/usr/bin/env python3 [CORRECT] C. #!/bin/python3 D. #!python3
Correct Answer: B
Rationale: #!/usr/bin/env python3 uses the environment's PATH to locate python3, ensuring
portability across systems where Python may be installed in /usr/bin, /usr/local/bin, or other
locations. Option A may invoke Python 2. Options C and D use non-standard or incomplete
paths. Portability is essential for deployment across heterogeneous infrastructure.
Q4: [MULTIPLE RESPONSE] Which practices improve error handling in IT automation scripts?
(Select all that apply.)
A. Using bare except: clauses to catch all errors B. Catching specific exceptions like
FileNotFoundError and PermissionError [CORRECT] C. Logging full stack traces for debugging
[CORRECT] D. Silently ignoring errors to ensure script completion E. Using finally blocks to
release resources [CORRECT]
Correct Answers: B, C, E
, Rationale: Specific exception handling (B) enables targeted responses (retry vs. abort). Logging
traces (C) aids post-incident analysis. finally blocks (E) ensure cleanup (file handles, network
connections). Bare except: (A) masks KeyboardInterrupt and SystemExit, preventing clean
shutdown. Silent error suppression (D) creates false positives and operational blind spots.
Q5: A script processes server hostnames. Which data structure provides O(1) membership
testing for deduplication?
A. hostnames = [] (list) B. hostnames = () (tuple) C. hostnames = set() [CORRECT] D. hostnames =
"" (string)
Correct Answer: C
Rationale: Sets implement hash tables, providing O(1) average-case lookup versus O(n) for
lists/tuples. For deduplicating thousands of server hostnames, set() dramatically improves
performance. Example: seen = set(); for h in hosts: if h not in seen: process(h); seen.add(h).
Q6: [CODE COMPLETION] Complete the function to safely parse an integer from environment
variable MAX_RETRIES, defaulting to 3:
Python
Copy
import os
def get_max_retries():
______
A. return int(os.getenv('MAX_RETRIES', 3)) B. return int(os.environ.get('MAX_RETRIES', 3))
[CORRECT] C. return int(os.environ['MAX_RETRIES']) or 3 D. return os.getenv('MAX_RETRIES', 3)
Correct Answer: B
Rationale: os.environ.get() safely retrieves with default. Option A uses os.getenv() (also valid
but less explicit). Option C raises KeyError if missing and int() fails on None. Option D returns
string 3, not integer. Explicit int() conversion ensures type correctness for subsequent numeric
comparisons.