Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Object instantiation with new in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Object Instantiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code?
Consider the following C# code snippet. What will be printed to the console?
C Sharp (C#)
class Person {
    public string Name;
    public Person(string name) {
        Name = name;
    }
}

Person p = new Person("Alice");
Console.WriteLine(p.Name);
ACompilation error
Bnull
CPerson
DAlice
Attempts:
2 left
💡 Hint
Look at how the constructor sets the Name property.
Predict Output
intermediate
2:00remaining
What happens when you instantiate a class without a constructor?
What will be the output of this code?
C Sharp (C#)
class Box {
    public int Width = 5;
}

Box b = new Box();
Console.WriteLine(b.Width);
A0
BNullReferenceException
C5
DCompilation error
Attempts:
2 left
💡 Hint
Fields with initial values keep those values even without a constructor.
Predict Output
advanced
2:00remaining
What is the output of this code with multiple constructors?
What will this program print?
C Sharp (C#)
class Car {
    public string Model;
    public int Year;

    public Car() {
        Model = "Unknown";
        Year = 0;
    }

    public Car(string model, int year) {
        Model = model;
        Year = year;
    }
}

Car car = new Car("Tesla", 2023);
Console.WriteLine($"{car.Model} {car.Year}");
AUnknown 0
BTesla 2023
CTesla 0
DCompilation error
Attempts:
2 left
💡 Hint
Which constructor is called depends on the parameters passed with new.
Predict Output
advanced
2:00remaining
What error does this code raise?
What happens when you run this code?
C Sharp (C#)
class Animal {
    public string Name;
}

Animal a = new Animal();
Console.WriteLine(a.Name.Length);
ANullReferenceException
B0
CCompilation error
DEmpty string printed
Attempts:
2 left
💡 Hint
Name is not initialized, so it is null by default.
🧠 Conceptual
expert
2:00remaining
How many objects are created here?
How many objects are created in memory after running this code?
C Sharp (C#)
class Node {
    public Node Next;
}

Node first = new Node();
first.Next = new Node();
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Each 'new' keyword creates one object.

Practice

(1/5)
1. What does the new keyword do in C# when used like new MyClass()?
easy
A. It calls a static method of MyClass.
B. It deletes an existing object of MyClass.
C. It converts MyClass to a string.
D. It creates a new object instance of the class MyClass.

Solution

  1. Step 1: Understand the role of new

    The new keyword in C# is used to create a fresh object from a class blueprint.
  2. Step 2: Apply to the example new MyClass()

    This expression creates a new instance of the class MyClass by calling its constructor.
  3. Final Answer:

    It creates a new object instance of the class MyClass. -> Option D
  4. Quick Check:

    new creates object = C [OK]
Hint: Remember: new means create a fresh object [OK]
Common Mistakes:
  • Thinking new deletes or modifies existing objects
  • Confusing new with method calls
  • Forgetting parentheses after class name
2. Which of the following is the correct syntax to create a new object of class Person?
easy
A. Person p = new Person;
B. Person p = Person.new();
C. Person p = new Person();
D. Person p = Person();

Solution

  1. Step 1: Check correct use of new keyword and parentheses

    In C#, to create a new object, you must use new ClassName() with parentheses.
  2. Step 2: Analyze each option

    Person p = new Person(); uses new Person(); correctly. Person p = Person.new(); uses wrong syntax with dot notation. Person p = new Person; misses parentheses. Person p = Person(); misses new.
  3. Final Answer:

    Person p = new Person(); -> Option C
  4. Quick Check:

    Correct syntax = A [OK]
Hint: Always use new ClassName() with parentheses [OK]
Common Mistakes:
  • Omitting parentheses after class name
  • Using dot notation with new
  • Forgetting the new keyword
3. What will be the output of this code?
class Box {
  public int size;
  public Box(int s) { size = s; }
}

var b = new Box(5);
Console.WriteLine(b.size);
medium
A. 5
B. 0
C. null
D. Compilation error

Solution

  1. Step 1: Understand the constructor call

    The constructor Box(int s) sets the field size to the passed value s. Here, new Box(5) sets size = 5.
  2. Step 2: Check the output of Console.WriteLine(b.size)

    This prints the value of b.size, which was set to 5 by the constructor.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Constructor sets size = 5 [OK]
Hint: Constructor sets values; output shows assigned value [OK]
Common Mistakes:
  • Assuming default 0 instead of constructor value
  • Confusing null with int fields
  • Thinking code won't compile
4. Identify the error in this code snippet:
class Car {
  public string model;
  public Car(string m) { model = m; }
}

Car c = new Car;
medium
A. Class Car has no constructor defined.
B. Missing parentheses after Car in object creation.
C. model field is not initialized.
D. Cannot assign new Car to variable c.

Solution

  1. Step 1: Check object instantiation syntax

    In C#, when creating a new object, parentheses must follow the class name even if no arguments are passed.
  2. Step 2: Analyze the code snippet

    The code uses new Car; without parentheses, which causes a syntax error.
  3. Final Answer:

    Missing parentheses after Car in object creation. -> Option B
  4. Quick Check:

    new requires parentheses () [OK]
Hint: Always add () after new ClassName [OK]
Common Mistakes:
  • Omitting parentheses after new keyword
  • Assuming default constructor exists without parentheses
  • Ignoring compiler error messages
5. You want to create two independent objects of class Student with different names. Which code correctly does this?
hard
A. Student s1 = new Student("Alice"); Student s2 = new Student("Bob");
B. Student s1 = Student("Alice"); Student s2 = Student("Bob");
C. Student s1, s2 = new Student("Alice"), new Student("Bob");
D. Student s1 = new Student; Student s2 = new Student;

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]
Hint: Create each object with new and constructor call [OK]
Common Mistakes:
  • Forgetting new keyword
  • Trying to create multiple objects in one line incorrectly
  • Omitting constructor parameters