LINQ Questions with Answers

7 practice questions on LINQ (C#) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

Which LINQ method returns the first element, or the type's default value if the sequence is empty?

  1. First
  2. Single
  3. FirstOrDefault
  4. ElementAt

Answer: C. FirstOrDefault

Explanation

FirstOrDefault returns null (or 0 for numbers) instead of throwing when nothing matches.

Step-by-step solution

  1. Sequence may be empty.
  2. Use FirstOrDefault.
  3. Check for default value.

Why the other options are wrong

  • A. First throws if the sequence is empty.
  • B. Single throws if there is not exactly one element.
  • D. ElementAt throws for an out-of-range index.

Question 2Easy

What does LINQ's Select method do?

  1. It keeps only the items that match a condition
  2. It removes duplicate items
  3. It sorts the items in order
  4. It turns each item into a new value

Answer: D. It turns each item into a new value

Explanation

Select projects each element of a sequence into a new form. It maps each input item to one output value.

Step-by-step solution

  1. Select takes a function for each element.
  2. It applies that function to every item.
  3. The result is a new sequence of transformed values.

Why the other options are wrong

  • A. That describes Where, not Select.
  • B. That describes Distinct.
  • C. That describes OrderBy.

Tip: Select maps, Where filters.

Question 3Medium

What happens when this code runs?

var values = new[] { 1, 2 };
Console.WriteLine(values.Single());
  1. It prints 1, the first element of the array
  2. It prints 2, the last element of the array
  3. It returns 0 as a default value when there is no single match
  4. It throws InvalidOperationException

Answer: D. It throws InvalidOperationException

Explanation

Single expects exactly one element. With two elements it throws InvalidOperationException, unlike FirstOrDefault.

Step-by-step solution

  1. values has two elements.
  2. Single() requires exactly one element.
  3. It throws InvalidOperationException.

Why the other options are wrong

  • A. Single does not take the first item; it needs exactly one element.
  • B. Single does not return the last item either.
  • C. Single never returns a default value; it throws when there is not exactly one item.

Tip: Use Single only when exactly one match is guaranteed; use First or FirstOrDefault otherwise.

Question 4Medium

What is printed?

var nums = new[] { 1, 2, 3, 4, 5, 6 };
Console.WriteLine(nums.Where(n => n % 2 == 0).Sum());
  1. 12
  2. 9, from adding 3 and 6 only
  3. 6, which is only 2 + 4 with the last even number left out
  4. 20, which is 2 + 4 + 6 + 8 and includes 8, which is not in the list

Answer: A. 12

Explanation

Where keeps the even numbers 2, 4 and 6. Sum adds them: 2 + 4 + 6 = 12.

Step-by-step solution

  1. Where(n % 2 == 0) keeps 2, 4 and 6.
  2. Sum() adds those three values.
  3. 2 + 4 + 6 = 12.

Why the other options are wrong

  • B. The sum 9 would need 3 and 6, which are not both even items here.
  • C. 6 is only 2 + 4, which leaves out the last even number, 6.
  • D. 20 is 2 + 4 + 6 + 8, and 8 is not in the list.

Tip: Filter first with Where, then aggregate with Sum.

Question 5Medium

What does this print?

var list = new List<int> { 1, 2, 3 };
var q = list.Where(x => x > 1);
list.Add(4);
Console.WriteLine(q.Count());
  1. 2
  2. 3
  3. 4
  4. 1

Answer: B. 3

Explanation

Where is deferred. The query runs at Count(), after 4 was added, so it sees 2, 3 and 4.

Step-by-step solution

  1. Query is only defined at Where.
  2. Add changes the list.
  3. Count executes the query on 1,2,3,4: matches 2,3,4.

Why the other options are wrong

  • A. Two would be correct only if the query ran before Add.
  • C. Four counts every element, but 1 fails the filter.
  • D. One is not the count of any step.

Tip: LINQ queries run when enumerated.

Question 6Medium

What is printed?

var words = new[] { "pear", "fig", "apple" };
Console.WriteLine(string.Join(",", words.OrderBy(w => w.Length).ThenBy(w => w)));
  1. pear,fig,apple
  2. apple,fig,pear
  3. fig,pear,apple
  4. fig,apple,pear

Answer: C. fig,pear,apple

Explanation

OrderBy sorts by length: fig (3), pear (4), apple (5). ThenBy only breaks ties, and there are no ties here.

Step-by-step solution

  1. The lengths are pear 4, fig 3, apple 5.
  2. OrderBy(w => w.Length) sorts from short to long.
  3. string.Join prints fig,pear,apple.

Why the other options are wrong

  • A. OrderBy by length puts fig (3 letters) before pear (4 letters).
  • B. apple is the longest word, so it comes last, not first.
  • D. apple has 5 letters, so it must come after pear (4 letters).

Tip: ThenBy only breaks ties that OrderBy leaves.

Question 7Hard

What is printed by the two WriteLine calls, in order?

int calls = 0;
var src = new[] { 1, 2, 3 };
var q = src.Select(v => { calls++; return v * 10; });
Console.WriteLine(calls);
var list = q.ToList();
Console.WriteLine(calls);
  1. 3, then 3
  2. 0, then 3
  3. 0, then 0
  4. 3, then 0

Answer: B. 0, then 3

Explanation

Select is lazy: the lambda runs only when the results are read. The first print happens before any enumeration, so it is 0. ToList() reads all three items, so the counter reaches 3.

Step-by-step solution

  1. Select only records the query; nothing runs yet.
  2. The first WriteLine prints 0.
  3. ToList() enumerates the source, which calls the lambda three times.
  4. The second WriteLine prints 3.

Why the other options are wrong

  • A. Select does not run when it is declared, so the first line is not 3.
  • C. ToList() runs the query once for each of the three items.
  • D. calls only goes up, so it cannot drop back to 0.

Tip: LINQ queries are deferred until you enumerate them, for example with ToList or foreach.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics