Bird
0
0

Which of the following PowerShell commands correctly uses string interpolation to produce:

hard📝 Application Q15 of 15
PowerShell - Variables and Data Types
You want to create a greeting message that includes a user's name stored in $userName and their age stored in $age. Which of the following PowerShell commands correctly uses string interpolation to produce:
Hello, Alice! You are 30 years old.
A$greeting = "Hello, $userName! You are $age years old."
B$greeting = 'Hello, $userName! You are $age years old.'
C$greeting = "Hello, $userName! You are ${age} years old."
D$greeting = "Hello, ${userName}! You are ${age} years old."
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable interpolation with complex variables

    Variables inside strings can be interpolated directly or with braces ${} to clarify boundaries.
  2. Step 2: Evaluate each option

    $greeting = "Hello, $userName! You are $age years old." works correctly and is the most common usage; $greeting = 'Hello, $userName! You are $age years old.' uses single quotes (no interpolation); $greeting = "Hello, $userName! You are ${age} years old." uses braces for $age which is valid but unnecessary here; $greeting = "Hello, ${userName}! You are ${age} years old." uses braces for both variables which is valid but less common.
  3. Final Answer:

    $greeting = "Hello, $userName! You are $age years old." -> Option A
  4. Quick Check:

    Direct interpolation without braces is common and correct = A [OK]
Quick Trick: Use ${} to clearly separate variables in strings when needed [OK]
Common Mistakes:
  • Using single quotes expecting interpolation
  • Not using braces when variable names are ambiguous
  • Mixing variable syntax incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes