All subjects.NET · 90 questions · 34 junior / 47 mid / 9 senior

C# quiz and interview questions

C# interviews test three things: whether you know how the type system works (value vs reference, boxing, immutability), whether you can read code and predict what it prints, and whether you can spot bugs like off-by-one errors, async void and deadlocks. The quiz below mixes all three, from junior to senior level.

C# quiz90 questions in the bank · 10 per round
Level
Question type
Mode

C#: key concepts

Value vs reference types
Structs, int, double and bool are value types: assigning copies the data. Classes, string, arrays and object are reference types: assigning copies the reference, so both variables point to the same object.
Strings are immutable
Methods like ToUpper() or Replace() return a new string and never change the original. Use StringBuilder when you build long strings in a loop.
LINQ deferred execution
Where, Select and most LINQ operators don't run until you enumerate (foreach, ToList, Count). If the source changes before that, the result reflects the change.
async / await
await frees the thread while waiting for I/O. Return Task (not void) from async methods so callers can await them and catch exceptions; avoid .Result and .Wait(), which can deadlock.
Boxing
Converting a value type to object or an interface allocates a copy on the heap. It's slow in hot loops — generics avoid it.
IDisposable and using
Objects that hold files, connections or sockets implement IDisposable. A using statement calls Dispose() automatically, even when an exception is thrown.

All 90 C# questions with answers

Try each one first, then open the answer. Making a class test? Use the test maker.

  1. juniorConcepts

    Which of these is a value type in C#?

    • A. string
    • B. int
    • C. object
    • D. int[]
    Show answer

    B. int — int (System.Int32) is a struct, so it's a value type. string, object and arrays are reference types.

  2. juniorPredict the output

    What does this print?

    int a = 5;
    int b = a++ + ++a;
    Console.WriteLine(b);
    • A. 10
    • B. 11
    • C. 12
    • D. 13
    Show answer

    C. 12 — a++ returns 5 and then a becomes 6. ++a makes a 7 and returns 7. So b = 5 + 7 = 12.

  3. juniorPredict the output

    What does this print?

    string s = "hello";
    s.ToUpper();
    Console.WriteLine(s);
    • A. HELLO
    • B. hello
    • C. Hello
    • D. Compile error
    Show answer

    B. hello — Strings are immutable. ToUpper() returns a new string, but the result is thrown away, so s is still "hello".

  4. juniorConcepts

    What is the main difference between const and readonly?

    • A. There is no difference
    • B. readonly can be assigned in the constructor; const must be set at compile time
    • C. const can be changed later; readonly can't
    • D. readonly only works with strings
    Show answer

    B. readonly can be assigned in the constructor; const must be set at compile time — const values are baked in at compile time. readonly fields can be set at declaration or in a constructor, so they can hold runtime values.

  5. juniorFind the bug

    What goes wrong when this runs?

    int[] nums = { 1, 2, 3 };
    for (int i = 0; i <= nums.Length; i++)
        Console.WriteLine(nums[i]);
    • A. Syntax error
    • B. Prints 1 2 3 and stops normally
    • C. Prints 1 2 3, then throws IndexOutOfRangeException
    • D. Infinite loop
    Show answer

    C. Prints 1 2 3, then throws IndexOutOfRangeException — Off-by-one error: valid indexes are 0–2, but i <= nums.Length also tries nums[3]. Use i < nums.Length.

  6. midPredict the output

    What does this print?

    var actions = new List<Action>();
    for (int i = 0; i < 3; i++)
        actions.Add(() => Console.Write(i));
    foreach (var a in actions) a();
    • A. 012
    • B. 333
    • C. 222
    • D. 000
    Show answer

    B. 333 — The lambdas capture the same loop variable i, not its value at that moment. When they run, the loop has finished and i is 3. (foreach loops get a fresh variable per iteration since C# 5; for loops don't.)

  7. midPredict the output

    What does this print?

    var list = new List<int> { 1, 2, 3 };
    var query = list.Where(x => x > 1);
    list.Add(4);
    Console.WriteLine(query.Count());
    • A. 2
    • B. 3
    • C. 4
    • D. 1
    Show answer

    B. 3 — LINQ is deferred: Where runs only when Count() enumerates it, after 4 was added. Matching items are 2, 3 and 4.

  8. midConcepts

    What is boxing?

    • A. Wrapping code in a try/catch
    • B. Converting a value type to object (a heap allocation)
    • C. Casting a class to an interface
    • D. Compressing a string
    Show answer

    B. Converting a value type to object (a heap allocation) — Boxing copies a value type into a new object on the heap, e.g. object o = 42;. Unboxing casts it back. It costs memory and time in hot paths.

  9. midFind the bug

    What is the main problem with this method?

    public async void SaveAsync()
    {
        await db.SaveChangesAsync();
    }
    • A. It won't compile
    • B. Callers can't await it and exceptions can crash the app
    • C. It runs synchronously
    • D. SaveChangesAsync doesn't exist
    Show answer

    B. Callers can't await it and exceptions can crash the app — async void can't be awaited, so callers don't know when it finishes and can't catch its exceptions. Use async Task — async void is only for event handlers.

  10. midPredict the output

    What does this print?

    struct Point { public int X; }
    
    var p1 = new Point { X = 1 };
    var p2 = p1;
    p2.X = 5;
    Console.WriteLine(p1.X);
    • A. 5
    • B. 1
    • C. 0
    • D. Compile error
    Show answer

    B. 1 — Point is a struct (value type), so p2 = p1 copies it. Changing p2 doesn't affect p1.

More subjects

Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.

Chat on WhatsApp