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

Implicit typing with var keyword in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Var Mastery Badge
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 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);
A13.5
B13
CError: Cannot add int and double implicitly
D10 3.5
Attempts:
2 left
💡 Hint
Remember that var lets the compiler infer the type and that adding int and double results in a double.
Predict Output
intermediate
1: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" };
AList<string>
Bobject
CIEnumerable<int>
Dstring[]
Attempts:
2 left
💡 Hint
Look at the right side: new List creates a list of strings.
🔧 Debug
advanced
2: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;
Avar cannot be assigned an int value
BMissing semicolon after var
Cvar requires initialization at declaration
Dx is a reserved keyword
Attempts:
2 left
💡 Hint
var needs to know the type immediately from the assigned value.
Predict Output
advanced
2: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.");
AName Age
BAnna is 30 years old.
CAnna30
DError: Anonymous types cannot be used with var
Attempts:
2 left
💡 Hint
Anonymous types can be assigned to var and their properties accessed.
🧠 Conceptual
expert
2:30remaining
Which statement about var is true in C#?
Choose the correct statement about the var keyword in C#.
Avar is a keyword to declare variables of type object
Bvar can be used without initialization if the type is declared later
Cvar variables are dynamically typed and can change type at runtime
Dvar variables must be initialized at declaration and their type is fixed at compile time
Attempts:
2 left
💡 Hint
Think about how var works with type inference and static typing.