Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - File IO
What will be the output of this code?
using System.Text.Json;

var person = new Person { Name = "Anna", Age = 25 };
var json = JsonSerializer.Serialize(person);
Console.WriteLine(json);

Assuming class Person { public string Name { get; set; } public int Age { get; set; } }
A{"Name":"Anna","Age":25}
B{"name":"Anna","age":25}
C{"Name":Anna,"Age":25}
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Serialize Person object to JSON string

    JsonSerializer.Serialize converts the object to JSON with property names matching C# property names and string values quoted.
  2. Step 2: Confirm output format

    The output JSON string will have "Name" and "Age" with correct casing and quotes around string values.
  3. Final Answer:

    {"Name":"Anna","Age":25} -> Option A
  4. Quick Check:

    Serialization output = property names with quotes [OK]
Quick Trick: Serialized JSON uses exact C# property names with quotes [OK]
Common Mistakes:
MISTAKES
  • Assuming lowercase property names by default
  • Missing quotes around string values
  • Expecting compilation error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes