Question 243 of 750
Scripting BasicseasyMultiple ChoiceObjective-mapped

Quick Answer

The answer is an if-else statement, because conditional statements in scripting are designed to evaluate a true/false condition and branch execution accordingly. In this scenario, the script must check for a registry key’s existence before modifying it—a classic decision point. An if-else construct uses a condition like `Test-Path 'HKLM:\Software\MyKey'` to run one block if the key exists (modify it) and another if it does not (skip or create it). On the CompTIA A+ Core 2 220-1202 exam, this tests your understanding of logical flow control versus loops (which repeat tasks) or try-catch (which handles errors, not existence checks). A common trap is confusing conditional statements with loops—remember, if-else asks “should I do this?” while loops ask “how many times?”. Memory tip: “If it exists, else it skips”—think of the registry key as a door: if it’s there, walk through; else, build a new one.

220-1102 Scripting Basics Practice Question

This 220-1202 practice question tests your understanding of scripting basics. This is a configuration task: choose the command set that satisfies every stated requirement. Small differences — like 'secret' vs 'password' or 'transport input ssh' vs 'all' — change whether the answer is correct. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A technician needs to deploy a configuration change to 50 Windows 10 computers using a script. The script must check if a specific registry key exists before modifying it. Which scripting construct should be used?

Question 1easymultiple choice
Full question →

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

An if-else statement

The script needs to conditionally execute code based on whether a registry key exists. An if-else statement is the correct construct for this because it evaluates a condition (e.g., Test-Path 'HKLM:\Software\MyKey') and executes one block if true (modify the key) and another if false (skip or create). Loops are for repetition, not conditional branching, and try-catch handles runtime errors, not existence checks.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • A for loop

    Why it's wrong here

    A for loop is used for iteration, not for checking a condition before action.

  • A while loop

    Why it's wrong here

    A while loop repeats code while a condition is true, but it doesn't check existence before a single modification.

  • An if-else statement

    Why this is correct

    An if-else statement allows the script to test for the registry key's existence and then act accordingly.

    Related concept

    Read the scenario before looking for a memorised answer.

  • A try-catch block

    Why it's wrong here

    Try-catch is for error handling, not for checking existence before modification.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates confuse conditional logic (if-else) with error handling (try-catch), thinking that checking for existence requires exception handling, when in fact a simple conditional test is the correct and more efficient approach.

Detailed technical explanation

How to think about this question

In PowerShell, the Test-Path cmdlet returns a Boolean ($true or $false) when checking registry key existence, which is directly consumable by an if-else statement. A common subtlety is that the registry provider uses a different path syntax (e.g., 'HKLM:\' instead of 'HKEY_LOCAL_MACHINE'), and the if-else construct ensures the script does not attempt to modify a non-existent key, which would throw a terminating error. In real-world deployments, this pattern is critical for idempotent configuration scripts that run across diverse systems.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related 220-1202 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free 220-1202 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this 220-1202 question test?

Scripting Basics — This question tests Scripting Basics — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: An if-else statement — The script needs to conditionally execute code based on whether a registry key exists. An if-else statement is the correct construct for this because it evaluates a condition (e.g., Test-Path 'HKLM:\Software\MyKey') and executes one block if true (modify the key) and another if false (skip or create). Loops are for repetition, not conditional branching, and try-catch handles runtime errors, not existence checks.

What should I do if I get this 220-1202 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

3 more ways this is tested on 220-1202

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A technician needs to write a script that runs a specific command only if a Windows service is running. If the service is stopped, the script should start it first. Which scripting method is most appropriate?

medium
  • A.Use a for loop to iterate over all services.
  • B.Use an if-else statement to check the service status.
  • C.Use a switch statement with multiple conditions.
  • D.Use a try-catch block to handle errors if the command fails.

Why B: The correct answer is B because an if-else statement is the most appropriate scripting method to check the status of a specific Windows service and conditionally execute a command or start the service. In PowerShell, you can use `Get-Service` to retrieve the service status and then an if-else block to evaluate whether the `Status` property equals 'Running'. This provides clear, linear logic that directly matches the requirement without unnecessary complexity.

Variation 2. A company uses a login script that sets environment variables and maps drives based on the user's department. The script works for most users, but some report that the drive mappings are missing. The script is written in batch and uses 'if' statements to check department codes. What is the most likely cause of the intermittent failures?

medium
  • A.The script runs too early before the network is fully initialized
  • B.The department codes in the script do not match the actual codes due to case sensitivity
  • C.The script is using 'setx' instead of 'set' for environment variables
  • D.The user accounts lack permission to run login scripts

Why B: Batch file 'if' statements are case-sensitive by default when comparing strings. If the script checks for department codes like 'SALES' but the actual environment variable or user input contains 'sales' or 'Sales', the comparison fails and the drive mapping block is skipped. This explains why the issue is intermittent — it depends on how the department code is stored or passed.

Variation 3. A technician is tasked with creating a PowerShell script that will parse a CSV file containing user information and create local user accounts on a Windows 10 machine. The CSV has columns: 'Username', 'FullName', 'Password'. The script must skip any row where the 'Username' is empty. Which control structure should the technician use to handle this requirement?

hard
  • A.A 'for' loop with a counter to skip empty rows
  • B.A 'switch' statement to match usernames
  • C.An 'if' statement to test whether the Username property is not empty
  • D.A 'try/catch' block to handle errors when creating the account

Why C: Option C is correct because the requirement is to conditionally skip rows based on a property value. An 'if' statement in PowerShell allows you to test whether the 'Username' property is empty or null using a condition like `if ($_.Username -ne '')` and then skip the row with `continue` or simply not process it. This is the most direct and efficient control structure for a simple boolean check on each row.

Keep practising

More 220-1202 practice questions

Last reviewed: Jun 24, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.