Challenge - 5 Problems
Runtime Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Type Name Retrieval
What is the output of this C# code snippet?
C Sharp (C#)
int number = 42; Console.WriteLine(number.GetType().Name);
Attempts:
2 left
💡 Hint
Use GetType() and check the Name property.
✗ Incorrect
The GetType() method returns the runtime type of the object. The Name property returns the simple name of the type, which for an int is "Int32".
❓ Predict Output
intermediate2:00remaining
Output of Checking Type Equality
What will this code print?
C Sharp (C#)
object obj = "hello"; if (obj.GetType() == typeof(string)) Console.WriteLine("Match"); else Console.WriteLine("No Match");
Attempts:
2 left
💡 Hint
Compare the runtime type of obj with typeof(string).
✗ Incorrect
The object obj holds a string, so obj.GetType() returns System.String, which equals typeof(string).
🔧 Debug
advanced2:00remaining
Identify the Error in Type Checking
What error does this code produce when run?
C Sharp (C#)
object val = null; if (val.GetType() == typeof(object)) Console.WriteLine("Object"); else Console.WriteLine("Not Object");
Attempts:
2 left
💡 Hint
Check what happens when calling GetType() on null.
✗ Incorrect
Calling GetType() on a null reference causes a NullReferenceException at runtime.
🧠 Conceptual
advanced2:00remaining
Understanding typeof vs GetType()
Which statement is true about typeof and GetType() in C#?
Attempts:
2 left
💡 Hint
Think about when you use typeof and when you call GetType().
✗ Incorrect
typeof is used with a type name at compile time, while GetType() is called on an instance to get its runtime type.
❓ Predict Output
expert2:00remaining
Output of Type Checking with Inheritance
What is the output of this code?
C Sharp (C#)
class Animal {} class Dog : Animal {} Animal pet = new Dog(); Console.WriteLine(pet.GetType() == typeof(Animal));
Attempts:
2 left
💡 Hint
GetType() returns the actual runtime type, not the declared type.
✗ Incorrect
pet is declared as Animal but holds a Dog instance. GetType() returns Dog, which is not equal to typeof(Animal).