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 administrator needs to write a Python script that processes server hostnames. The
script must handle cases where a hostname might be missing (None) without crashing. Which
code snippet properly implements this error handling?
A.
Python
Copy
hostname = get_server_name()
print(hostname.upper())
B.
Python
Copy
hostname = get_server_name()
try:
print(hostname.upper())
except AttributeError:
print("No hostname provided")
``` [CORRECT]
,C.
```python
hostname = get_server_name()
if hostname is not "":
print(hostname.upper())
D.
Python
Copy
hostname = get_server_name()
print(hostname.upper() or "No hostname provided")
Correct Answer: B
Rationale: Option B correctly uses a try/except block to catch the AttributeError that occurs
when calling .upper() on None. This is the Pythonic way to handle potential None values when
the operation might fail. Option A crashes with AttributeError if hostname is None. Option C
checks for empty string but None is not an empty string—the condition passes and still crashes.
Option D uses short-circuit evaluation incorrectly; hostname.upper() executes first and raises
the exception before the or can evaluate.
Q2: A DevOps engineer is creating a virtual environment for a new automation project. Which
sequence of commands correctly sets up and activates a Python 3 virtual environment named
automation_env on a Linux system?
A. python -m venv automation_env then source automation_env/bin/activate [CORRECT]
B. pip install virtualenv automation_env then virtualenv activate
C. python3 -m virtualenv create automation_env then automation_env/Scripts/activate
D. venv create automation_env then ./automation_env/bin/activate
Correct Answer: A
,Rationale: The standard library venv module creates virtual environments: python -m venv
<name>. Activation on Linux/macOS uses source <name>/bin/activate (Windows uses
Scripts\activate). Option B incorrectly uses pip to install virtualenv without proper syntax.
Option C uses wrong module path (Scripts is Windows, not Linux). Option D omits the -m flag
required to run venv as a module.
Q3: [CODE ANALYSIS] What is the output of the following code snippet?
Python
Copy
import sys
def check_python_version():
version = sys.version_info
if version.major == 3 and version.minor >= 8:
return "Supported"
elif version.major == 3:
return "Legacy"
else:
return "Unsupported"
print(check_python_version())
Assuming the script runs on Python 3.11.4:
A. "Legacy"
B. "Unsupported"
C. "Supported" [CORRECT]
D. None
Correct Answer: C
, Rationale: sys.version_info returns a named tuple (major, minor, micro, releaselevel, serial).
Python 3.11.4 gives version.major=3, version.minor=11. The condition version.major == 3 and
version.minor >= 8 evaluates True (11 >= 8), returning "Supported". This pattern is common in
automation scripts checking for compatible Python versions before importing version-specific
modules like asyncio features or typing constructs.
Q4: A system administrator needs to process a list of IP addresses, removing duplicates while
preserving the original order of first appearance. Which Python code accomplishes this most
efficiently?
A.
Python
Copy
unique_ips = list(set(ip_list))
B.
Python
Copy
unique_ips = []
for ip in ip_list:
if ip not in unique_ips:
unique_ips.append(ip)
``` [CORRECT]
C.
```python
unique_ips = dict.fromkeys(ip_list).keys()
D.
Python
Copy