Bird
0
0

What will happen if you run this script with -Age 150?

medium📝 Command Output Q5 of 15
PowerShell - Scripting Best Practices
What will happen if you run this script with -Age 150?
function Check-Age {
  param(
    [ValidateRange(0,120)]
    [int]$Age
  )
  Write-Output "Age is $Age"
}
Check-Age -Age 150
AOutputs: Age is 150
BOutputs: Age is 120
CThrows a validation error
DNo output, script silently ignores
Step-by-Step Solution
Solution:
  1. Step 1: Understand ValidateRange limits

    [ValidateRange(0,120)] restricts $Age to values between 0 and 120 inclusive.
  2. Step 2: Analyze input value

    Input 150 is outside the allowed range, so PowerShell throws a validation error.
  3. Final Answer:

    Throws a validation error -> Option C
  4. Quick Check:

    Out-of-range input triggers validation error [OK]
Quick Trick: Out-of-range values cause errors with [ValidateRange()] [OK]
Common Mistakes:
  • Assuming it clamps to max value
  • Expecting silent failure
  • Thinking it outputs invalid value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes