Bird
0
0

What will be the output of this PowerShell code snippet?

medium📝 Command Output Q13 of 15
PowerShell - Cross-Platform PowerShell
What will be the output of this PowerShell code snippet?
 $response = Invoke-RestMethod -Uri 'https://jsonplaceholder.typicode.com/posts/1' -Method GET
$response.title
ANull, because $response.title does not exist
BAn error because the -Method parameter is missing
CThe title of the post with ID 1 from the API
DThe entire JSON response as a string
Step-by-Step Solution
Solution:
  1. Step 1: Understand Invoke-RestMethod behavior

    Invoke-RestMethod sends a GET request and parses JSON into an object. Accessing $response.title retrieves the 'title' property.
  2. Step 2: Analyze the code and expected output

    The API returns a JSON object with a 'title' field for post ID 1. The code prints that title string.
  3. Final Answer:

    The title of the post with ID 1 from the API -> Option C
  4. Quick Check:

    Invoke-RestMethod parses JSON; access properties directly [OK]
Quick Trick: Invoke-RestMethod returns objects; access properties like .title [OK]
Common Mistakes:
  • Expecting raw JSON string instead of parsed object
  • Forgetting to specify -Method GET (optional but recommended)
  • Assuming $response.title is null without checking API

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes