Challenge - 5 Problems
Var Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this C# code using var?
Consider the following code snippet. What will be printed to the console?
C Sharp (C#)
var x = 10; var y = 3.5; var z = x + y; Console.WriteLine(z);
Attempts:
2 left
💡 Hint
Remember that var lets the compiler infer the type and that adding int and double results in a double.
✗ Incorrect
The variable z is inferred as double because x (int) and y (double) are added, resulting in a double value 13.5.
❓ Predict Output
intermediate1:30remaining
What type does var infer here?
What is the type of variable 'data' in this code?
C Sharp (C#)
var data = new List<string> { "apple", "banana" };
Attempts:
2 left
💡 Hint
Look at the right side: new List creates a list of strings.
✗ Incorrect
The compiler infers 'data' as List because the right side explicitly creates a List.
🔧 Debug
advanced2:00remaining
Why does this code cause a compilation error?
Examine the code below. Why does it fail to compile?
C Sharp (C#)
var x;
x = 5;Attempts:
2 left
💡 Hint
var needs to know the type immediately from the assigned value.
✗ Incorrect
In C#, var variables must be initialized when declared so the compiler can infer the type. Declaring 'var x;' without initialization causes an error.
❓ Predict Output
advanced2:00remaining
What is the output of this code with var and anonymous types?
What will this program print?
C Sharp (C#)
var person = new { Name = "Anna", Age = 30 }; Console.WriteLine($"{person.Name} is {person.Age} years old.");
Attempts:
2 left
💡 Hint
Anonymous types can be assigned to var and their properties accessed.
✗ Incorrect
The anonymous type is assigned to 'person' and its properties Name and Age are accessed correctly, printing the formatted string.
🧠 Conceptual
expert2:30remaining
Which statement about var is true in C#?
Choose the correct statement about the var keyword in C#.
Attempts:
2 left
💡 Hint
Think about how var works with type inference and static typing.
✗ Incorrect
In C#, var is a compile-time feature where the compiler infers the type from the initialization expression. The type is fixed and cannot change at runtime.