A. Split will return the whole string as one element because spaces were removed
B. Replace syntax is incorrect, it needs three arguments
C. Split cannot be used on strings
D. The variable $newText is not assigned properly
Solution
Step 1: Understand Replace effect
$text.Replace(' ', '') removes all spaces, so $newText becomes "HelloWorld" with no spaces.
Step 2: Analyze Split on $newText
Splitting by space on "HelloWorld" returns one element array because no spaces remain.
Final Answer:
Split will return the whole string as one element because spaces were removed -> Option A
Quick Check:
Replace removes spaces, so Split finds none [OK]
Hint: Replace removes spaces before Split runs [OK]
Common Mistakes:
Thinking Replace needs 3 arguments
Expecting Split to split without separator present
Assuming $newText is unchanged
5. You have a string $data = " red; green ; light blue ; yellow ". How do you split it into an array of clean (trimmed) color names using PowerShell?
hard
A. $data.Split(';') | ForEach-Object { $_.Trim() }
B. $data.Replace(' ', '').Split(';')
C. $data.Split(';').Trim()
D. $data.Trim().Split(';')
Solution
Step 1: Split string by semicolon
Use $data.Split(';') to get array: [" red", " green ", " light blue ", " yellow "] with spaces.
Step 2: Trim spaces from each element
Pipe to ForEach-Object with .Trim() to remove spaces from each color name.
Step 3: Check other options
The option $data.Split(';').Trim() tries to call .Trim() on the array (invalid). The option $data.Replace(' ', '').Split(';') removes all spaces, merging multi-word colors like "light blue" into "lightblue" (wrong). The option $data.Trim().Split(';') trims only ends of the whole string.
Final Answer:
$data.Split(';') | ForEach-Object { $_.Trim() } -> Option A
Quick Check:
Split then trim each element = clean array [OK]
Hint: Split first, then trim each part separately [OK]
Common Mistakes:
Calling .Trim() on array directly
Removing all spaces inside words
Trimming only the whole string, not parts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.