
C# Assessment: Sample Questions | Bryq
Question
What does the following code print?
int a = 7;int b = 2;Console.WriteLine(a / b);
Select your answer
3
3.5
4
3.0
WHY IS THIS THE CORRECT ANSWER
When both operands are integers, C# performs integer division and discards the remainder, so 7 / 2 gives 3. To get 3.5, at least one operand must be a floating-point type, for example 7.0 / 2 or (double)a / b.
Question
What does the following code print?
struct Point { public int X; }var p1 = new Point { X = 1 };var p2 = p1;p2.X = 5;Console.WriteLine(p1.X);
Select your answer
1
5
0
WHY IS THIS THE CORRECT ANSWER
Structs are value types, so assigning p1 to p2 copies the whole value. Changing p2.X only affects the copy. If Point were a class, both variables would refer to the same object and the output would be 5.
Question
What does the following code print?
var list = new List<int> { 1, 2, 3 };var query = list.Where(x => x > 1);list.Add(4);Console.WriteLine(query.Count());
Select your answer
3
2
4
An InvalidOperationException
WHY IS THIS THE CORRECT ANSWER
LINQ operators such as Where use deferred execution: the filter only runs when the query is enumerated, which happens here when Count() is called. By then the list contains 1, 2, 3 and 4, so three values match. Calling ToList() when defining the query would have captured the result straight away, giving 2.


