Challenge - 5 Problems
Dynamic Instance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()); } }
Attempts:
2 left
💡 Hint
Activator.CreateInstance creates an object using the default constructor.
✗ Incorrect
The code uses Activator.CreateInstance to create a Person object. The default Name is "Unknown", so ToString returns "Person: Unknown".
❓ Predict Output
intermediate2: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()); } }
Attempts:
2 left
💡 Hint
Activator.CreateInstance can pass constructor arguments as an object array.
✗ Incorrect
The code calls the constructor with "Tesla" as argument, so the Model property is set to "Tesla" and printed.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
Activator.CreateInstance without parameters requires a default constructor.
✗ Incorrect
Book has only a constructor with a string parameter. Activator.CreateInstance(t) tries to call a default constructor which does not exist, causing MissingMethodException.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
Activator.CreateInstance(Type) is the correct method to create an instance dynamically.
✗ Incorrect
Option A uses Activator.CreateInstance with a Type argument correctly. Option A is invalid syntax. Option A uses a generic overload that requires compile-time type information. Option A is invalid because Type does not have CreateInstance method.
🚀 Application
expert2: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); } }
Attempts:
2 left
💡 Hint
Activator.CreateInstance is called inside a loop 3 times.
✗ Incorrect
The loop runs 3 times, each time creating a new Shape instance dynamically and adding it to the list. So 3 instances are created.