OBJECTIVE ASSESSMENT PRACTICE 2026/2027 | Verified
Code-Centric & Scenario-Based Questions and Answers
| Grade A Target | Pass Guaranteed
Section 1: Python Foundations & Scripting Environment (10 Questions)
Q1: A system administrator needs to create a Python script that processes server hostnames.
The script should accept a variable number of hostname arguments and validate that each
contains only alphanumeric characters and hyphens. Which function signature and validation
approach is most Pythonic?
A.
Python
Copy
def validate_hosts(host_list):
for host in host_list:
if not host.isalnum():
return False
return True
B.
Python
Copy
def validate_hosts(*hosts):
return all(h.replace('-', '').isalnum() for h in hosts) [CORRECT]
C.
Python
,Copy
def validate_hosts(hosts):
valid = True
for i in range(len(hosts)):
if not re.match(r'^[a-zA-Z0-9-]+$', hosts[i]):
valid = False
return valid
D.
Python
Copy
def validate_hosts(**hosts):
return [h for h in hosts.values() if h.isalnum()]
Correct Answer: B
Rationale: Option B demonstrates Pythonic practices: using *hosts for variadic arguments
(accepting any number of positional arguments), generator expression with all() for efficient
short-circuit evaluation, and str.replace() combined with isalnum() for validation. The all()
function stops at the first False, optimizing performance. Option A fails because isalnum()
rejects hyphens, which are valid in hostnames. Option C works but is verbose; the regex is
unnecessary when string methods suffice, and manual indexing with range(len()) is unPythonic.
Option D incorrectly uses **kwargs (for keyword arguments) and returns a list instead of a
boolean, while also failing to handle hyphens.
Q2: A DevOps engineer is writing a configuration parser. The code needs to handle missing
configuration keys gracefully. Which implementation best follows Python exception handling
best practices?
A.
Python
Copy
def get_config_value(config, key):
, try:
return config[key]
except:
return None
B.
Python
Copy
def get_config_value(config, key):
if key in config:
return config[key]
else:
raise KeyError(f"Key {key} not found")
C.
Python
Copy
def get_config_value(config, key):
try:
return config[key]
except KeyError:
return config.get('default', None) [CORRECT]
except TypeError:
raise ValueError("Config must be a dictionary")
D.
Python
Copy
def get_config_value(config, key):
, return config[key] or None
Correct Answer: C
Rationale: Option C demonstrates proper exception handling: catching specific exceptions
(KeyError, TypeError) rather than bare except, providing fallback behavior for missing keys, and
raising descriptive errors for invalid input types. This follows EAFP (Easier to Ask Forgiveness
than Permission) and maintains clear error boundaries. Option A uses bare except which
catches KeyboardInterrupt and SystemExit, making the script unkillable—an anti-pattern.
Option B violates the principle of least surprise by raising an exception for a missing key when a
default return would be more useful; it also uses LBYL (Look Before You Leap) which is less
Pythonic than EAFP for this case. Option D doesn't handle the case where key doesn't exist
(raises KeyError) and misuses or logic.
Q3: An automation script needs to process a list of server dictionaries, extracting only servers in
the "production" environment with status "running". Which list comprehension is correct?
Given data:
Python
Copy
servers = [
{'name': 'web01', 'env': 'production', 'status': 'running'},
{'name': 'web02', 'env': 'staging', 'status': 'running'},
{'name': 'db01', 'env': 'production', 'status': 'stopped'}
]
A.
Python
Copy
prod_running = [s for s in servers if s['env'] == 'production'
and s.status == 'running']
B.
Python