0
0
C Sharp (C#)programming~20 mins

Creating instances dynamically in C Sharp (C#) - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Instance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dynamic instance creation with Activator.CreateInstance
What is the output of this C# code that creates an instance dynamically?
C Sharp (C#)
using System;

class Person {
    public string Name { get; set; } = "Unknown";
    public override string ToString() => $"Person: {Name}";
}

class Program {
    static void Main() {
        Type t = typeof(Person);
        var obj = Activator.CreateInstance(t);
        Console.WriteLine(obj.ToString());
    }
}
ASystem.Object
BPerson: Unknown
CNullReferenceException
DPerson:
Attempts:
2 left
💡 Hint
Activator.CreateInstance creates an object using the default constructor.
Predict Output
intermediate
2:00remaining
Output when creating instance with parameterized constructor dynamically
What will this C# code print when creating an instance dynamically with parameters?
C Sharp (C#)
using System;
using System.Reflection;

class Car {
    public string Model { get; }
    public Car(string model) {
        Model = model;
    }
    public override string ToString() => $"Car model: {Model}";
}

class Program {
    static void Main() {
        Type t = typeof(Car);
        object obj = Activator.CreateInstance(t, new object[] { "Tesla" });
        Console.WriteLine(obj.ToString());
    }
}
ASystem.Object
BCar model:
CMissingMethodException
DCar model: Tesla
Attempts:
2 left
💡 Hint
Activator.CreateInstance can pass constructor arguments as an object array.
🔧 Debug
advanced
2:00remaining
Identify the error when creating instance dynamically without default constructor
What error does this code produce when trying to create an instance dynamically?
C Sharp (C#)
using System;

class Book {
    public string Title { get; }
    public Book(string title) {
        Title = title;
    }
}

class Program {
    static void Main() {
        Type t = typeof(Book);
        var obj = Activator.CreateInstance(t);
        Console.WriteLine(obj);
    }
}
AMissingMethodException
BNullReferenceException
CInvalidCastException
DNo error, prints Book object
Attempts:
2 left
💡 Hint
Activator.CreateInstance without parameters requires a default constructor.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly creates an instance dynamically using reflection?
Choose the code snippet that correctly creates an instance of class Animal dynamically with a default constructor.
Avar obj = Activator.CreateInstance(typeof(Animal));
Bvar obj = typeof(Animal).CreateInstance();
Cvar obj = Activator.CreateInstance<Animal>();
Dvar obj = new Activator(typeof(Animal));
Attempts:
2 left
💡 Hint
Activator.CreateInstance(Type) is the correct method to create an instance dynamically.
🚀 Application
expert
2:00remaining
How many instances are created dynamically in this code?
Given this code, how many objects are created dynamically using Activator.CreateInstance?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Shape {
    public string Name { get; set; }
    public Shape() { Name = "Shape"; }
}

class Program {
    static void Main() {
        List<object> shapes = new List<object>();
        Type shapeType = typeof(Shape);
        for (int i = 0; i < 3; i++) {
            var shape = Activator.CreateInstance(shapeType);
            shapes.Add(shape);
        }
        Console.WriteLine(shapes.Count);
    }
}
A0
B1
C3
D4
Attempts:
2 left
💡 Hint
Activator.CreateInstance is called inside a loop 3 times.