Bird
0
0

You want to create a function that returns the length of a string parameter. Which function correctly does this?

hard📝 Application Q8 of 15
PowerShell - Functions
You want to create a function that returns the length of a string parameter. Which function correctly does this?
Afunction Get-Length { param $str return $str.length() }
Bfunction Get-Length { param($str) return $str.Length }
Cfunction Get-Length { param($str) return length($str) }
Dfunction Get-Length { param($str) return $str.size }
Step-by-Step Solution
Solution:
  1. Step 1: Recall string length property in PowerShell

    Strings have a .Length property (capital L) to get length.
  2. Step 2: Check syntax correctness

    function Get-Length { param($str) return $str.Length } uses correct param block and property. Others use invalid syntax or properties.
  3. Final Answer:

    function Get-Length { param($str) return $str.Length } -> Option B
  4. Quick Check:

    Use $str.Length to get string length [OK]
Quick Trick: Use .Length property (capital L) for string length [OK]
Common Mistakes:
  • Using lowercase .length()
  • Calling length() as a function
  • Using .size which doesn't exist

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes