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
What is printed?
var s = new HashSet<int> { 1, 2, 2, 3, 3, 3 };
Console.WriteLine(s.Count);
All questions with answers
Which keyword declares a constant known at compile time?
Show answer
Answer: B. const
const is compile-time; readonly can be set at runtime in the constructor.
What is the default value of an int field?
Show answer
Answer: B. 0
Value-type fields default to zero values.
Which type is a value type?
Show answer
Answer: C. DateTime
DateTime is a struct. Strings and arrays are reference types.
What does int? mean?
Show answer
Answer: B. A nullable int
Nullable<int> can hold a number or null.
What is printed?
C# Illustrative var name = "Asha"; Console.WriteLine(quot;Hello, {name}");Show answer
Answer: A. Hello, Asha
The $ prefix makes an interpolated string.
Which operator returns the right side when the left is null?
Show answer
Answer: B. ??
a ?? b gives b when a is null; ??= assigns only when null.
Which collection prevents duplicates?
Show answer
Answer: B. HashSet<T>
HashSet<T>.Add returns false for an existing value.
Which keyword lets a method be overridden?
Show answer
Answer: B. virtual
virtual allows overriding; override replaces it in a subclass.
Which block always runs after try/catch?
Show answer
Answer: A. finally
finally runs whether or not an exception was thrown.
What does this LINQ query return?
C# Illustrative var even = new[] { 1, 2, 3, 4 }.Where(n => n % 2 == 0);Show answer
Answer: B. [2, 4]
Where keeps even numbers.
Which LINQ method executes the query immediately?
Show answer
Answer: C. ToList
ToList, Count, First and similar methods enumerate the query.
Which delegate type returns a value?
Show answer
Answer: B. Func<T, TResult>
Func returns TResult; Action and EventHandler return void.
What does await do?
Show answer
Answer: B. Asynchronously waits, freeing the thread
The method is suspended and the thread can do other work.
Which return type should an async method with no result usually have?
Show answer
Answer: B. Task
async void is only for event handlers because callers cannot await it or catch its exceptions.
What does a record provide automatically?
Show answer
Answer: B. Value-based equality and ToString
Records also support with-expressions for non-destructive copies.
What does the switch expression return here?
C# Illustrative int temp = 18; var feel = temp switch { < 10 => "cold", < 25 => "mild", _ => "hot" };Show answer
Answer: B. "mild"
Relational patterns are checked in order; 18 is not < 10 but is < 25.
Which interface lets an object be used in a using statement?
Show answer
Answer: B. IDisposable
using calls Dispose at the end of the scope.
Which attribute marks an xUnit test with inline data?
Show answer
Answer: B. [Theory]
[Theory] with [InlineData(…)] runs once per data row.
What is string immutability?
Show answer
Answer: B. Operations return new strings instead of changing the original
That is why StringBuilder is better for many concatenations.
Which keyword prevents a class from being inherited?
Show answer
Answer: B. sealed
sealed classes cannot be base classes.
When does a LINQ Where(...) query actually run?
Show answer
Answer: B. When the results are enumerated
LINQ uses deferred execution; the query runs when enumerated.
From the lesson: LINQWhat is printed?
C# Illustrative int[] n = [5, 3, 8, 1]; Console.WriteLine(string.Join(",", n.Where(x => x > 2).OrderBy(x => x)));Show answer
Which is the best way to check whether any product costs more than 1000?
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: LINQWhat does C# compile into before the runtime turns it into machine code?
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#Modern .NET programs can run on Linux and macOS.
Show answer