Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the var keyword do in C#?
easy
A. It lets the compiler decide the variable's type based on the assigned value.
B. It declares a variable without assigning any value.
C. It forces the variable to be of type var.
D. It creates a constant variable that cannot be changed.

Solution

  1. Step 1: Understand the purpose of var

    The var keyword tells the compiler to infer the variable type from the value assigned.
  2. Step 2: Compare options with this understanding

    Only It lets the compiler decide the variable's type based on the assigned value. correctly describes this behavior. Other options misunderstand var usage.
  3. Final Answer:

    It lets the compiler decide the variable's type based on the assigned value. -> Option A
  4. Quick Check:

    Implicit typing = compiler infers type [OK]
Hint: Remember: var means 'let compiler guess type' [OK]
Common Mistakes:
  • Thinking var means no type is assigned
  • Assuming var creates constants
  • Believing var is a type itself
2. Which of the following is a correct way to declare a variable using var in C#?
easy
A. var number;
B. var int number = 10;
C. var = 10;
D. var number = 10;

Solution

  1. Step 1: Check syntax rules for var

    Variables declared with var must be initialized immediately with a value.
  2. Step 2: Evaluate each option

    var number = 10; declares and initializes correctly. var number; misses initialization. var = 10; has wrong syntax. var int number = 10; mixes explicit type with var incorrectly.
  3. Final Answer:

    var number = 10; -> Option D
  4. Quick Check:

    var requires immediate assignment [OK]
Hint: Always assign a value when using var [OK]
Common Mistakes:
  • Declaring var without assigning a value
  • Using var as a type name
  • Combining var with explicit type
3. What will be the output of the following code?
var message = "Hello";
var count = 5;
Console.WriteLine(message + " " + count);
medium
A. Hello + 5
B. Hello5
C. Hello 5
D. Compilation error

Solution

  1. Step 1: Understand variable types inferred

    message is a string "Hello", count is an int 5.
  2. Step 2: Analyze the concatenation in WriteLine

    Using + with string and int converts int to string and concatenates with a space in between, so output is "Hello 5".
  3. Final Answer:

    Hello 5 -> Option C
  4. Quick Check:

    String + int concatenates as string [OK]
Hint: String + int converts int to string automatically [OK]
Common Mistakes:
  • Expecting no space between words
  • Thinking + prints literal plus sign
  • Assuming compilation error due to var
4. Identify the error in the following code snippet:
var x;
x = 10;
medium
A. var requires initialization at declaration.
B. Missing semicolon after var declaration.
C. No error, code runs fine.
D. Cannot assign int to var variable.

Solution

  1. Step 1: Recall var declaration rules

    Variables declared with var must be initialized immediately.
  2. Step 2: Check the code snippet

    Here, var x; declares without initialization, which is invalid.
  3. Final Answer:

    var requires initialization at declaration. -> Option A
  4. Quick Check:

    var must be assigned when declared [OK]
Hint: var must have a value when declared [OK]
Common Mistakes:
  • Thinking var can be declared without value
  • Confusing var with dynamic
  • Assuming assignment later is allowed
5. Given the code below, what is the type of result and what will be its value?
var numbers = new[] {1, 2, 3, 4};
var result = numbers.Select(n => n * 2).ToList();
Console.WriteLine(result.Count);
hard
A. Compilation error due to var usage
B. result is a List<int> with 4 elements; output: 4
C. result is a List<string>; output: 4
D. result is an array of int; output: 8

Solution

  1. Step 1: Understand the type inferred for numbers

    numbers is an int array: int[].
  2. Step 2: Analyze the LINQ Select and ToList

    Select doubles each number, producing IEnumerable<int>. ToList converts it to List<int> with 4 elements.
  3. Step 3: Check Console.WriteLine output

    result.Count returns 4, the number of elements in the list.
  4. Final Answer:

    result is a List<int> with 4 elements; output: 4 -> Option B
  5. Quick Check:

    var infers List<int>, Count = 4 [OK]
Hint: var follows method return types exactly [OK]
Common Mistakes:
  • Assuming result is an array, not a List
  • Expecting output to be sum or doubled value
  • Thinking var causes compilation error here
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard