Coding Solution

C# MCQ test: 12 questions with answers (CodinGame, SkillValue type)

MCQC#12 questionsUpdated

This quiz is for .NET developers getting ready for a technical screening or a C# interview. Every question is modeled on a classic trap from recruitment platforms, with the answer and a detailed explanation.

In short

C# recruitment tests (SkillValue, CodinGame, HackerRank) are mostly code-reading questions: you are shown a short program and asked what it prints. The most frequent topics are value types versus reference types, string immutability, LINQ deferred execution, async/await and boxing. Next come exceptions with finally and using, nullable types and the generic collections (List, Dictionary, HashSet).

The topics that come up most

Practise: 12 questions with answers

Pick an answer to see the explanation.

What does this program print?
struct Point { public int X; }

class Program
{
    static void Modify(Point p) { p.X = 10; }

    static void Main()
    {
        Point pt = new Point { X = 1 };
        Modify(pt);
        Console.WriteLine(pt.X);
    }
}

Point is a struct, so a value type: Modify receives a copy of pt and changes that copy. The original keeps X = 1. Had Point been declared as a class, the reference would be shared and the output would be 10. The code compiles fine, and 0 would only be the default of an uninitialized int, which is not the case here.

What does this code print?
string s = "hello";
s.ToUpper();
Console.WriteLine(s);

Strings are immutable in C#: ToUpper() does not change s, it returns a new string that is discarded here. To print HELLO you would need s = s.ToUpper(). The code compiles (calling a method and ignoring its result is allowed) and s is never emptied.

What does this code print?
var d = new Dictionary<string, int>();
d["a"] = 1;
d["a"] = 2;
d.Add("b", 3);
Console.WriteLine(d["a"] + d.Count);

The indexer d[key] = value adds the key when missing and overwrites the value when present, so d["a"] is 2 and the dictionary holds 2 entries (a and b): 2 + 2 = 4. It is the Add method that throws an ArgumentException on a duplicate key, not the indexer. Conversely, reading d["missing"] would throw a KeyNotFoundException, which is why TryGetValue exists.

Which statement about interfaces and abstract classes is true in C#?

C# has no multiple class inheritance: a class derives from at most one class (abstract or not) but can implement as many interfaces as it wants. An abstract class can mix abstract methods with implemented ones, which is the whole point. An interface cannot declare instance fields (only members such as methods, properties and events). And a class marked abstract can never be instantiated directly, even without any abstract member.

What does this code print?
var list = new List<int> { 1, 2, 3 };
var query = list.Where(x => x > 1);
list.Add(4);
Console.WriteLine(query.Count());

Where uses deferred execution: the query only runs when enumerated, here by Count(). At that point the list holds 1, 2, 3, 4 and three elements are greater than 1. You would get 2 by calling ToList() right after the Where, which snapshots the result. The InvalidOperationException applies to modifying a collection while an enumeration is in progress, which is not what happens here.

In what order are the digits printed?
static async Task Work()
{
    Console.WriteLine("1");
    await Task.Delay(100);
    Console.WriteLine("2");
}

static async Task Main()
{
    Task t = Work();
    Console.WriteLine("3");
    await t;
    Console.WriteLine("4");
}

An async method runs synchronously until the first await whose task is not complete. Work prints 1, hits Task.Delay and returns control to Main, which prints 3. Main then awaits t: when the delay ends, Work resumes and prints 2, then Main resumes and prints 4. You would get 1 2 3 4 only if Main had written await Work() directly.

What happens with this code (C# 9 or later)?
class User
{
    public string Name { get; init; }
}

var u = new User { Name = "Ana" };
u.Name = "Bob";
Console.WriteLine(u.Name);

An init accessor can only be used while the object is being constructed: in an object initializer, a constructor or a with expression. The assignment u.Name = "Bob" after construction is rejected by the compiler. With { get; set; } the program would print Bob; with { get; } alone, the initializer itself would fail to compile. It is never a runtime error.

What does this program print?
static int F()
{
    try
    {
        return 1;
    }
    finally
    {
        Console.WriteLine("finally");
    }
}

static void Main()
{
    Console.WriteLine(F());
}

The finally block always runs before the method returns, even after a return statement: the return value 1 is stored, finally prints its message, then the method returns and Main prints 1. The code compiles; only a return inside the finally block itself would be illegal in C#. The finally block cannot be skipped, that is what it is designed for.

What does this code print?
string s = null;
int? len = s?.Length;
Console.WriteLine(len ?? -1);
Console.WriteLine(len.HasValue);

The null-conditional operator ?. short-circuits when s is null and yields null, which is why len must be an int? (nullable). The ?? operator supplies -1 as the fallback, and HasValue is False because len holds nothing. There is no NullReferenceException: avoiding it is exactly what ?. is for. Writing int len = s?.Length; would, however, be a compilation error.

What does this code print?
object o1 = 5;
object o2 = 5;
Console.WriteLine(o1 == o2);
Console.WriteLine(o1.Equals(o2));

Assigning an int to an object boxes it: each assignment allocates a new object on the heap. The == operator between two object operands compares references, and o1 and o2 point to two different boxes, hence False. Equals is overridden on Int32 to compare values: 5 equals 5, hence True. This is one of the most common C# test traps and the reason == on object operands is avoided.

What does this code print?
string a = "hello";
string b = new string("hello".ToCharArray());
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);
Console.WriteLine(ReferenceEquals(a, b));

The == operator on two string-typed operands is overloaded to compare contents: True. Once cast to object, == falls back to reference comparison, and b is a fresh instance created with new, distinct from the interned literal "hello": False. ReferenceEquals performs the same reference check: False. Had b been written as b = "hello", all three lines would print True thanks to literal interning.

What does this program print?
class Res : IDisposable
{
    public Res() { Console.WriteLine("open"); }
    public void Dispose() { Console.WriteLine("dispose"); }
}

static void Main()
{
    try
    {
        using (var r = new Res())
        {
            Console.WriteLine("work");
            throw new InvalidOperationException();
        }
    }
    catch (InvalidOperationException)
    {
        Console.WriteLine("catch");
    }
}

A using block compiles to try/finally: Dispose() is called in the finally, so on leaving the block, before the exception propagates to the outer catch. The order is therefore open, work, dispose, then catch. Dispose is guaranteed even when an exception is thrown, which is the whole point of using over a manual call. The exception is caught because the using sits inside the try.

On test day

  1. When reading a code snippet, first check whether the types involved are structs or classes: half the traps come from value-type copying.
  2. If a string method's result is not assigned back, the original string has not changed.
  3. On a LINQ query, find the exact moment of enumeration (foreach, ToList, Count, First): that is when the source is read.
  4. For async questions, read line by line until the first incomplete await, then jump back to the caller.

Practise with Coding Solutions

On a practice MCQ shown on screen, the Coding Solutions extension explains the right answer and the rule behind it, in 29 languages. For coding exercises, see also our explained CodinGame solutions and the platform guides.

Frequently asked questions

Which C# version do I need to know for a recruitment test?

Most questions cover fundamentals that have been stable since C# 2 to C# 6: generics, LINQ, async/await, nullables. The newer features that come up most are init accessors, records and pattern matching.

How long is a C# MCQ on SkillValue or CodinGame?

Expect 20 to 40 questions in 30 to 60 minutes, roughly one minute per question. Code-reading questions take the longest: do not get stuck, skip and come back if time allows.

Do .NET questions also cover ASP.NET or Entity Framework?

A general C# MCQ stays focused on the language and the BCL (collections, string, Task, LINQ). Frameworks are tested separately, and the test title usually says so.

How should I revise before the test?

Run every snippet in this quiz yourself in a console project or on dotnetfiddle and tweak it (struct to class, add a ToList, and so on) to see what changes. Understanding the mechanism beats memorizing the answer.

Other languages

★ 5,0 · 16 avis · 928 utilisateurs Add to Chrome