Bird
0
0

You want to create two independent objects of class Student with different names. Which code correctly does this?

hard🚀 Application Q15 of 15
C Sharp (C#) - Classes and Objects
You want to create two independent objects of class Student with different names. Which code correctly does this?
AStudent s1 = new Student("Alice"); Student s2 = new Student("Bob");
BStudent s1 = Student("Alice"); Student s2 = Student("Bob");
CStudent s1, s2 = new Student("Alice"), new Student("Bob");
DStudent s1 = new Student; Student s2 = new Student;
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation with parameters

    To create objects with different names, call the constructor with the name string for each object separately using new Student(name).
  2. Step 2: Analyze each option

    Student s1 = new Student("Alice"); Student s2 = new Student("Bob"); correctly creates two objects with different names. Student s1 = Student("Alice"); Student s2 = Student("Bob"); misses new. Student s1, s2 = new Student("Alice"), new Student("Bob"); has invalid syntax for multiple declarations. Student s1 = new Student; Student s2 = new Student; misses parentheses and parameters.
  3. Final Answer:

    Student s1 = new Student("Alice"); Student s2 = new Student("Bob"); -> Option A
  4. Quick Check:

    Use new with constructor for each object [OK]
Quick Trick: Create each object with new and constructor call [OK]
Common Mistakes:
MISTAKES
  • Forgetting new keyword
  • Trying to create multiple objects in one line incorrectly
  • Omitting constructor parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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