hardMultiple ChoiceObjective-mapped
220-1102 Practice Question: A technician is writing a Python script to…
A technician is writing a Python script to automate the installation of software on multiple Windows machines. The script needs to check if the software is already installed by looking for a specific registry key. If the key exists, the script should skip the installation. Which Python library and method should the technician use to read the registry?
⚠ Common exam trap
CompTIA often tests the distinction between general-purpose modules (`os`, `sys`, `subprocess`) and platform-specific modules (`winreg`), trapping candidates who assume a generic module can handle registry access or who overlook the native Python library for Windows registry operations.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use the 'winreg' module with 'OpenKey' and 'QueryValueEx'
The `winreg` module is the standard Python library for accessing the Windows registry. `OpenKey` opens a specified registry key, and `QueryValueEx` retrieves the value data and type for a given value name. This allows the script to check for the software's registry key and conditionally skip installation if it exists.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use the 'os' module with 'os.regread'
Why it's wrong here
The 'os' module offers a portable way to interact with operating system-dependent functionality, including file system operations, environment variables, and process management. While it provides a broad range of OS interaction functions, the 'os' module explicitly does not include a function named 'regread' or any other dedicated method for reading from or writing to the Windows Registry. Its scope is primarily file and process management, not direct registry manipulation.
- ✗
Use the 'subprocess' module to run 'reg query'
Why it's wrong here
The 'subprocess' module is designed to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. While technically feasible to execute the 'reg query' command-line utility using 'subprocess.run()', this approach is generally inefficient and less reliable for programmatic registry access. It incurs the overhead of launching an external process, requires complex parsing of string output, and is more susceptible to breaking if the 'reg.exe' utility's output format changes, unlike direct API calls.
- ✓
Use the 'winreg' module with 'OpenKey' and 'QueryValueEx'
Why this is correct
The 'winreg' module is Python's standard library interface for interacting directly with the Windows Registry API. Using 'winreg.OpenKey()' allows a script to obtain a handle to a specific registry key, such as HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER, and then navigate to subkeys. Subsequently, 'winreg.QueryValueEx()' is the precise function designed to retrieve the data associated with a named value within that opened key, making this the most direct, efficient, and robust method for programmatic registry access.
- ✗
Use the 'sys' module to access registry via system calls
Why it's wrong here
The 'sys' module provides access to system-specific parameters and functions, primarily dealing with the Python interpreter itself, such as command-line arguments, standard input/output streams, and exception handling. It does not expose any direct functions or interfaces for interacting with the underlying operating system's registry database or for making low-level system calls specifically for registry access. Therefore, attempting to use 'sys' for this purpose would be futile.
Go deeper
Related to this question
Learn chapter
Windows Editions and Features
Key term
Registry
The Windows Registry is a central hierarchical database that stores configuration settings and options for the operating system, hardware, software, and user preferences.
Key term
Windows
Windows is a family of operating systems developed by Microsoft that manages computer hardware and software, providing a graphical user interface for users to interact with their devices.
About these practice questions
This 220-1202 question is part of Courseiva's 495-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 220-1202 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 220-1202 exam.