Bird
0
0

You want to create a list of 3 Student objects with names "Anna", "Ben", and "Cara" using object instantiation with new. Which code correctly does this?

hard🚀 Application Q8 of 15
C Sharp (C#) - Classes and Objects
You want to create a list of 3 Student objects with names "Anna", "Ben", and "Cara" using object instantiation with new. Which code correctly does this?
Avar students = new List<Student> { new Student("Anna"), new Student("Ben"), new Student("Cara") };
Bvar students = new List<Student>(); students = { "Anna", "Ben", "Cara" };
Cvar students = new List<Student>(); students.Add("Anna", "Ben", "Cara");
Dvar students = new List<Student> { Student("Anna"), Student("Ben"), Student("Cara") };
Step-by-Step Solution
Solution:
  1. Step 1: Understand list initialization with objects

    To create a list with objects, use collection initializer with new Student() for each item.
  2. Step 2: Evaluate each option

    var students = new List { new Student("Anna"), new Student("Ben"), new Student("Cara") }; correctly creates new Student objects inside the list initializer. Others have syntax errors or wrong method calls.
  3. Final Answer:

    var students = new List { new Student("Anna"), new Student("Ben"), new Student("Cara") }; -> Option A
  4. Quick Check:

    Use new inside list initializer for objects [OK]
Quick Trick: Use new ClassName() inside collection initializers [OK]
Common Mistakes:
MISTAKES
  • Trying to assign strings directly to object list
  • Using Add with multiple arguments incorrectly
  • Omitting new keyword for objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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