Quiz
C# track quiz: 10 questions on syntax, async, LINQ, nullability, records.
C# — track quiz
EXAMPLE
// 10 questions. Pass: >= 7. // ===== Q1 ===== // What is the default access modifier for a class member in C#? // A) public // B) internal // C) private // D) protected // ===== Q2 ===== // What does 'init' allow? // A) Property settable only inside the constructor // B) Property settable only during object initialisation // C) Same as readonly // D) Async initialisation // ===== Q3 ===== // What does 'record' provide compared to 'class'? // A) Value equality + ToString + Deconstruct + with-expressions // B) Garbage-collected automatically // C) Asynchronous methods // D) None of the above // ===== Q4 ===== // What does 'string?' mean (with nullable reference types enabled)? // A) String can hold any value // B) Compiler treats null as a legitimate value; warns on dereference // C) Same as 'string' // D) Means string of length 0 or 1 // ===== Q5 ===== // What's the difference between 'IEnumerable<T>' and 'IQueryable<T>'? // A) IEnumerable executes in-memory; IQueryable defers + translates to SQL // B) IQueryable is async; IEnumerable is sync // C) They are the same // D) IEnumerable cannot be ToList()d // ===== Q6 ===== // What does 'await' do? // A) Synchronously waits // B) Returns a Task // C) Suspends the method until the awaited task completes; resumes when ready // D) Causes a deadlock // ===== Q7 ===== // 'async void' should be used for: // A) Any async method that doesn't return data // B) Event handlers only // C) Methods called from Main // D) Never // ===== Q8 ===== // What is the typical purpose of 'using' declarations (C# 8+)? // A) Import namespaces // B) Dispose IDisposable objects automatically at scope end // C) Suppress warnings // D) Inline a method // ===== Q9 ===== // What does '?? ' (null-coalescing operator) do? // A) Bitwise OR // B) Returns left if not null; right otherwise // C) Comparison // D) Pattern matching // ===== Q10 ===== // In ASP.NET Core, what is dependency injection registered via? // A) Static class // B) IServiceCollection in Program.cs (Configure / WebApplicationBuilder.Services) // C) [Inject] attribute only // D) DI is not supported // ===== Answers ===== // 1. C — private is default for class members // 2. B — init: only during object initialisation // 3. A — record adds value equality, ToString, Deconstruct, with-expressions // 4. B — nullable ref type; compiler tracks null // 5. A — IQueryable defers + translates (e.g. to SQL via EF) // 6. C — await suspends + resumes // 7. B — async void only for event handlers // 8. B — using disposes at scope end // 9. B — null-coalescing // 10. B — register via WebApplicationBuilder.Services // ===== Patterns ===== // - Records for DTOs + value-shape types // - Nullable refs enabled, treat warnings as errors // - await Task.WhenAll for parallel async // - DI everywhere; constructor injection // ===== Pitfalls ===== // - .Result on Task (deadlocks) // - async void (silent exceptions) // - decimal vs double for money // - Long LINQ chains in hot loops
Why it matters
Quiz takeaway: nullable refs + records + init properties + DI + async/await are the modern C# reflexes. Avoid .Result, async void, and decimal/double mixups. Master those and you write idiomatic .NET 8+ code.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…