Multiple choice practice

C# MCQ questions

Take a short practice test with instant feedback, then revise every question with its answer and the reason behind it.

Questions
84
From lessons
64
Your last test
—

Practice test

C# practice testPredict the output Question 1 of 10

What is printed?

var s = new HashSet<int> { 1, 2, 2, 3, 3, 3 };
Console.WriteLine(s.Count);
Score so far: 0/0

All questions with answers

  1. Which keyword declares a constant known at compile time?

    • Areadonly
    • Bconst
    • Cstatic
    • Dfinal
    Show answer

    Answer: B. const

    const is compile-time; readonly can be set at runtime in the constructor.

  2. What is the default value of an int field?

    • Anull
    • B0
    • C-1
    • Dundefined
    Show answer

    Answer: B. 0

    Value-type fields default to zero values.

  3. Which type is a value type?

    • Astring
    • Bint[]
    • CDateTime
    • DList<int>
    Show answer

    Answer: C. DateTime

    DateTime is a struct. Strings and arrays are reference types.

  4. What does int? mean?

    • AAn optional parameter
    • BA nullable int
    • CA pointer to int
    • DA dynamic int
    Show answer

    Answer: B. A nullable int

    Nullable<int> can hold a number or null.

  5. What is printed?

    C# Illustrative
    var name = "Asha";
    Console.WriteLine(
    quot;Hello,
    {name}");
    • AHello, Asha
    • BHello, {name}
    • CHello, name
    • DA compile error
    Show answer

    Answer: A. Hello, Asha

    The $ prefix makes an interpolated string.

  6. Which operator returns the right side when the left is null?

    • A?.
    • B??
    • C?:
    • D!!
    Show answer

    Answer: B. ??

    a ?? b gives b when a is null; ??= assigns only when null.

  7. Which collection prevents duplicates?

    • AList<T>
    • BHashSet<T>
    • CQueue<T>
    • DStack<T>
    Show answer

    Answer: B. HashSet<T>

    HashSet<T>.Add returns false for an existing value.

  8. Which keyword lets a method be overridden?

    • Aoverride
    • Bvirtual
    • Csealed
    • Dstatic
    Show answer

    Answer: B. virtual

    virtual allows overriding; override replaces it in a subclass.

  9. Which block always runs after try/catch?

    • Afinally
    • Bcatch
    • Celse
    • Dend
    Show answer

    Answer: A. finally

    finally runs whether or not an exception was thrown.

  10. What does this LINQ query return?

    C# Illustrative
    var even = new[] { 1, 2, 3, 4 }.Where(n => n % 2 == 0);
    
    • A[1, 2, 3, 4]
    • B[2, 4]
    • C[1, 3]
    • D6
    Show answer

    Answer: B. [2, 4]

    Where keeps even numbers.

  11. Which LINQ method executes the query immediately?

    • AWhere
    • BSelect
    • CToList
    • DOrderBy
    Show answer

    Answer: C. ToList

    ToList, Count, First and similar methods enumerate the query.

  12. Which delegate type returns a value?

    • AAction<T>
    • BFunc<T, TResult>
    • CEventHandler
    • DAction<T1, T2>
    Show answer

    Answer: B. Func<T, TResult>

    Func returns TResult; Action and EventHandler return void.

  13. What does await do?

    • ABlocks the thread until the task finishes
    • BAsynchronously waits, freeing the thread
    • CStarts a new thread
    • DCancels the task
    Show answer

    Answer: B. Asynchronously waits, freeing the thread

    The method is suspended and the thread can do other work.

  14. Which return type should an async method with no result usually have?

    • Avoid
    • BTask
    • Cobject
    • DThread
    Show answer

    Answer: B. Task

    async void is only for event handlers because callers cannot await it or catch its exceptions.

  15. What does a record provide automatically?

    • ADatabase mapping
    • BValue-based equality and ToString
    • CThread safety
    • DSerialization to XML
    Show answer

    Answer: B. Value-based equality and ToString

    Records also support with-expressions for non-destructive copies.

  16. What does the switch expression return here?

    C# Illustrative
    int temp = 18;
    var feel = temp switch { < 10 => "cold", < 25 => "mild", _ => "hot" };
    
    • A"cold"
    • B"mild"
    • C"hot"
    • DIt does not compile
    Show answer

    Answer: B. "mild"

    Relational patterns are checked in order; 18 is not < 10 but is < 25.

  17. Which interface lets an object be used in a using statement?

    • AIEnumerable
    • BIDisposable
    • CIComparable
    • DICloneable
    Show answer

    Answer: B. IDisposable

    using calls Dispose at the end of the scope.

  18. Which attribute marks an xUnit test with inline data?

    • A[Fact]
    • B[Theory]
    • C[Test]
    • D[TestMethod]
    Show answer

    Answer: B. [Theory]

    [Theory] with [InlineData(…)] runs once per data row.

  19. What is string immutability?

    • AStrings cannot be null
    • BOperations return new strings instead of changing the original
    • CStrings are stored on the stack
    • DStrings are compile-time constants
    Show answer

    Answer: B. Operations return new strings instead of changing the original

    That is why StringBuilder is better for many concatenations.

  20. Which keyword prevents a class from being inherited?

    • Astatic
    • Bsealed
    • Cabstract
    • Dprivate
    Show answer

    Answer: B. sealed

    sealed classes cannot be base classes.

  21. When does a LINQ Where(...) query actually run?

    • AWhen Where is called
    • BWhen the results are enumerated
    • CAt compile time
    • DNever
    Show answer

    Answer: B. When the results are enumerated

    LINQ uses deferred execution; the query runs when enumerated.

    From the lesson: LINQ
  22. What is printed?

    C# Illustrative
    int[] n = [5, 3, 8, 1];
    Console.WriteLine(string.Join(",", n.Where(x => x > 2).OrderBy(x => x)));
    
    • A5,3,8
    • B3,5,8
    • C8,5,3
    • D1,3,5,8
    Show answer

    Answer: B. 3,5,8

    Where keeps 5, 3 and 8; OrderBy sorts them ascending.

    From the lesson: LINQ
  23. Which is the best way to check whether any product costs more than 1000?

    • Aproducts.Count(p => p.Price > 1000) > 0
    • Bproducts.Any(p => p.Price > 1000)
    • Cproducts.Where(p => p.Price > 1000).ToList().Count != 0
    • Dproducts.First(p => p.Price > 1000) != null
    Show answer

    Answer: B. products.Any(p => p.Price > 1000)

    Any stops at the first match; the others do more work or throw.

    From the lesson: LINQ
  24. What does C# compile into before the runtime turns it into machine code?

    • AJavaScript
    • BIL (Intermediate Language)
    • CPython bytecode
    • DHTML
    Show answer

    Answer: B. IL (Intermediate Language)

    The C# compiler produces IL; the .NET runtime JIT-compiles IL to machine code.

    From the lesson: Introduction to C#
  25. Modern .NET programs can run on Linux and macOS.

    • ATrue
    • BFalse
    Show answer

    Answer: A. True

    Since .NET Core, .NET is cross-platform.

    From the lesson: Introduction to C#