C# library quiz

  • Admin
  • 1 min read

Nine questions on collections, LINQ, exceptions, files and async/await.

C#

C# library quiz

9 questions, instant feedback

Question 1 of 9
Question 1 of 9 Which collection gives O(1) lookup by key?
Why this is the answer

A dictionary hashes the key to find the value, which is constant time on average.

Question 2 of 9
Question 2 of 9 What does a List do when it grows?
Why this is the answer

The backing array doubles, and the existing elements are copied into it.

Question 3 of 9
Question 3 of 9 Which LINQ method transforms each element?
Why this is the answer

Select maps each element to a new value. Where filters, OrderBy sorts and GroupBy groups.

Question 4 of 9
Question 4 of 9 What does deferred execution mean for a LINQ query?
Why this is the answer

A query is a description of work; it executes when you iterate it or call a terminal method such as ToList.

Question 5 of 9
Question 5 of 9 Which exception should wrap an invalid argument?
Why this is the answer

ArgumentException (or ArgumentNullException) names the failure precisely. InvalidOperationException is for wrong state.

Question 6 of 9
Question 6 of 9 What does a finally block do?
Why this is the answer

finally runs on both paths, which is where cleanup that is not a using declaration belongs.

Question 7 of 9
Question 7 of 9 What does using do for a file stream?
Why this is the answer

using calls Dispose on an IDisposable at the end of the scope, even when an exception is thrown.

Question 8 of 9
Question 8 of 9 What should an async method return when it produces no value?
Why this is the answer

Task is awaited and carries exceptions. async void is only for event handlers, because it cannot be awaited.

Question 9 of 9
Question 9 of 9 Why avoid .Result on a Task?
Why this is the answer

Blocking on an asynchronous operation can deadlock when a synchronisation context is captured. Await instead.

Your answers stay in this browser. Nothing is sent to a server.