Home »
MCQs
LINQ MCQs (Multiple-Choice Questions)
Practice LINQ MCQs (Multiple-Choice Questions) with answers and explanations. These LINQ questions cover Language Integrated Query, query syntax, method syntax, filtering, projection, sorting, grouping, joining, aggregation, deferred execution, LINQ to Objects, IEnumerable<T>, IQueryable<T>, lambda expressions, and commonly used LINQ operators.
LINQ MCQs
LINQ (Language Integrated Query) provides a consistent way to query and transform data in .NET applications. It supports operations such as filtering, projection, sorting, grouping, joining, aggregation, and set operations through query expressions and standard query operators.
List of LINQ MCQs
The following LINQ multiple-choice questions are useful for beginners, experienced developers, technical interviews, examinations, and .NET programming preparation.
1. What does LINQ stand for?
- Language Integrated Query
- Language Internet Query
- Logical Integrated Query
- Language Interface Query
Answer: A) Language Integrated Query
Explanation:
LINQ stands for Language Integrated Query. It provides query capabilities directly within .NET programming languages such as C#.
2. Which namespace contains the standard LINQ extension methods for in-memory collections?
- System.Data
- System.Linq
- System.Query
- System.Collections.Linq
Answer: B) System.Linq
Explanation:
The System.Linq namespace contains the standard LINQ operators and extension methods used with collections and other queryable data sources.
3. Which LINQ method is used to filter elements according to a condition?
- Select
- Where
- OrderBy
- GroupBy
Answer: B) Where
Explanation:
Where filters a sequence according to a predicate and returns the elements for which the predicate evaluates to true.
4. Which LINQ method is used to transform each element of a sequence?
- Where
- Select
- Take
- Contains
Answer: B) Select
Explanation:
Select performs projection. It transforms each source element into an output element based on the supplied selector.
5. Which LINQ method sorts a sequence in ascending order based on a key?
- Sort
- OrderBy
- Arrange
- OrderAscending
Answer: B) OrderBy
Explanation:
OrderBy sorts the elements of a sequence in ascending order according to a specified key.
6. Which LINQ method sorts a sequence in descending order?
- OrderBy
- SortDescending
- OrderByDescending
- Descending
Answer: C) OrderByDescending
Explanation:
OrderByDescending sorts a sequence in descending order according to a specified key.
7. Which LINQ method is used to apply a secondary sort after OrderBy?
- ThenBy
- NextBy
- SecondaryBy
- OrderNext
Answer: A) ThenBy
Explanation:
ThenBy applies a secondary ascending sort to an already ordered sequence. ThenByDescending performs the secondary sort in descending order.
8. Which LINQ method returns the number of elements in a sequence?
- Length
- Size
- Count
- Number
Answer: C) Count
Explanation:
Count returns the number of elements in a sequence. It can also accept a predicate to count only elements satisfying a condition.
9. Which LINQ method determines whether at least one element satisfies a condition?
- All
- Any
- Exists
- ContainsOnly
Answer: B) Any
Explanation:
Any returns true when a sequence contains at least one element, or when at least one element satisfies the supplied predicate.
10. Which LINQ method checks whether every element satisfies a specified condition?
- Any
- All
- Every
- Check
Answer: B) All
Explanation:
All returns true only when every element in the sequence satisfies the supplied predicate.
11. Which LINQ method checks whether a sequence contains a specified value?
- Contains
- Has
- Exists
- Includes
Answer: A) Contains
Explanation:
Contains determines whether a sequence contains a specified element using the appropriate equality comparison.
12. Which LINQ method returns the first element of a sequence?
- First
- Start
- Head
- Begin
Answer: A) First
Explanation:
First returns the first element of a sequence. If the sequence is empty, it throws an exception.
13. Which method returns the first matching element or the default value when no element matches?
- First
- FirstOrDefault
- Single
- DefaultFirst
Answer: B) FirstOrDefault
Explanation:
FirstOrDefault returns the first matching element. If no matching element exists, it returns the default value for the element type.
14. Which LINQ method requires exactly one matching element?
- First
- Single
- Take
- ElementAt
Answer: B) Single
Explanation:
Single expects exactly one matching element. It throws an exception if there are no matching elements or more than one matching element.
15. What is the main difference between SingleOrDefault and FirstOrDefault?
- SingleOrDefault sorts the sequence
- SingleOrDefault throws if multiple matching elements exist
- FirstOrDefault always throws on an empty sequence
- There is no difference
Answer: B) SingleOrDefault throws if multiple matching elements exist
Explanation:
SingleOrDefault permits zero or one matching element but throws if multiple elements match. FirstOrDefault simply returns the first matching element.
16. Which LINQ method returns the last element of a sequence?
- Final
- Last
- End
- LastElement
Answer: B) Last
Explanation:
Last returns the last element of a sequence and throws an exception if the sequence contains no elements.
17. Which LINQ method selects a specified number of elements from the beginning of a sequence?
- Skip
- Take
- Limit
- FirstN
Answer: B) Take
Explanation:
Take returns a specified number of contiguous elements from the beginning of a sequence.
18. Which LINQ method skips a specified number of elements from the beginning?
- Skip
- Ignore
- Offset
- Jump
Answer: A) Skip
Explanation:
Skip bypasses the specified number of elements and returns the remaining sequence.
19. Which LINQ method removes duplicate elements from a sequence?
- Unique
- Distinct
- RemoveDuplicates
- Different
Answer: B) Distinct
Explanation:
Distinct returns unique elements from a sequence according to equality comparison. Current .NET also provides DistinctBy for uniqueness based on a selected key.
20. Which LINQ method combines two sequences while retaining duplicates and preserving the sequence order?
- Union
- Concat
- Merge
- Combine
Answer: B) Concat
Explanation:
Concat appends the second sequence to the first. Unlike Union, it does not remove duplicate elements.
21. Which LINQ set operation combines two sequences and removes duplicate elements?
- Concat
- Union
- Join
- Append
Answer: B) Union
Explanation:
Union combines two sequences and returns the distinct elements from both sequences.
22. Which LINQ method returns elements that are present in both sequences?
- Union
- Intersect
- Join
- Common
Answer: B) Intersect
Explanation:
Intersect returns the distinct elements that occur in both input sequences.
23. Which LINQ method returns elements present in the first sequence but not in the second?
- Except
- Remove
- Subtract
- Exclude
Answer: A) Except
Explanation:
Except produces the set difference between two sequences by returning elements from the first sequence that are not in the second.
24. Which LINQ method combines corresponding elements from two sequences into a new sequence?
- Join
- Zip
- Concat
- Pair
Answer: B) Zip
Explanation:
Zip combines corresponding elements from two sequences by applying a result selector to each pair.
25. Which LINQ method groups elements according to a key?
- Group
- GroupBy
- PartitionBy
- Cluster
Answer: B) GroupBy
Explanation:
GroupBy groups elements that have the same key. Each resulting group contains the key and the elements associated with that key.
26. Which LINQ method performs an inner join between two sequences?
- Join
- GroupJoin
- InnerJoinOnly
- MergeJoin
Answer: A) Join
Explanation:
Join correlates elements from two sequences based on matching keys and produces a result for matching pairs.
27. What is the primary purpose of GroupJoin?
- To sort two sequences
- To associate each element of one sequence with a group of matching elements from another
- To remove duplicates
- To calculate averages
Answer: B) To associate each element of one sequence with a group of matching elements from another
Explanation:
GroupJoin correlates elements from two sequences and retains the related inner elements as a group for each outer element.
28. Which LINQ method can flatten nested collections into a single sequence?
- Select
- SelectMany
- Flatten
- Expand
Answer: B) SelectMany
Explanation:
SelectMany projects each element to a sequence and then combines those sequences into one flattened sequence.
29. Which LINQ method calculates the sum of numeric values?
- Total
- Sum
- Add
- AggregateSum
Answer: B) Sum
Explanation:
Sum calculates the arithmetic sum of the numeric elements in a sequence or of values selected from its elements.
30. Which LINQ method calculates the arithmetic mean of numeric values?
- Mean
- Average
- AvgValue
- Middle
Answer: B) Average
Explanation:
Average calculates the average of numeric elements or of values selected from the elements.
31. Which LINQ method returns the largest value in a sequence?
- Maximum
- Max
- Largest
- Top
Answer: B) Max
Explanation:
Max returns the maximum value in a sequence or the maximum selected value from the sequence.
32. Which LINQ method returns the smallest value in a sequence?
- Min
- Smallest
- Lowest
- MinimumValue
Answer: A) Min
Explanation:
Min returns the minimum value in a sequence or the minimum selected value.
33. Which LINQ method performs a custom accumulation over a sequence?
- Reduce
- Aggregate
- Accumulate
- Combine
Answer: B) Aggregate
Explanation:
Aggregate applies an accumulator function over a sequence, allowing custom aggregation logic beyond standard operations such as Sum or Count.
34. Which LINQ method creates a sequence containing a specified number of repetitions of one value?
- Repeat
- Range
- Generate
- Duplicate
Answer: A) Repeat
Explanation:
Enumerable.Repeat creates a sequence containing the same specified value repeated a specified number of times.
35. Which LINQ method generates a sequence of consecutive integers?
- Numbers
- Range
- Sequence
- GenerateRange
Answer: B) Range
Explanation:
Enumerable.Range generates a sequence of consecutive integers starting from a specified value and containing a specified number of elements.
36. Which method creates an empty sequence of a specified type?
- Empty
- None
- Blank
- DefaultSequence
Answer: A) Empty
Explanation:
Enumerable.Empty<T>() returns an empty sequence of the specified element type.
37. Which method returns a sequence containing the default value when the source sequence is empty?
- Default
- DefaultIfEmpty
- EmptyDefault
- IfEmpty
Answer: B) DefaultIfEmpty
Explanation:
DefaultIfEmpty returns the original sequence when it is non-empty. If it is empty, it returns a sequence containing the default value or a supplied default value.
38. Which LINQ method can convert an IEnumerable sequence into a strongly typed sequence by casting each element?
- OfType
- Cast
- Convert
- AsType
Answer: B) Cast
Explanation:
Cast<T> attempts to cast every element of the source sequence to type T. An incompatible element can cause an exception during enumeration.
39. Which LINQ method selects only elements that can be assigned to a specified type?
- Cast
- OfType
- WhereType
- TypeFilter
Answer: B) OfType
Explanation:
OfType<T> filters a non-generic sequence and returns only elements that can be cast to the requested type.
40. What is deferred execution in LINQ?
- The query is never executed
- The query executes when its results are enumerated
- The query executes only during compilation
- The query executes before it is created
Answer: B) The query executes when its results are enumerated
Explanation:
Many LINQ operators return a sequence whose query logic is evaluated later, typically when the sequence is enumerated. This behavior is known as deferred execution.
41. Which method can force a LINQ query to execute immediately and store the results in a list?
- Execute
- ToList
- Run
- Load
Answer: B) ToList
Explanation:
ToList enumerates the source and creates a List<T>, thereby materializing the query results at that point.
42. What is the main difference between IEnumerable<T> and IQueryable<T> in LINQ?
- IEnumerable is for integers only
- IQueryable cannot be filtered
- IQueryable can represent queries as expression trees for a provider to translate
- There is no difference
Answer: C) IQueryable can represent queries as expression trees for a provider to translate
Explanation:
IEnumerable<T> is commonly used for in-memory enumeration, while IQueryable<T> uses expression trees so a query provider can translate the query for another data source, such as a database.
43. Which LINQ query syntax keyword specifies the source sequence?
- from
- source
- in
- select
Answer: A) from
Explanation:
In C# query expression syntax, the from clause introduces the data source and a range variable.
44. Which query syntax keyword filters records according to a condition?
- filter
- where
- condition
- when
Answer: B) where
Explanation:
The where clause applies a Boolean condition to filter elements from the source sequence.
45. Which query syntax keyword specifies the values or objects returned by a LINQ query?
- return
- output
- select
- result
Answer: C) select
Explanation:
The select clause defines the projection of the query and specifies the elements that the query returns.
46. Which query syntax keyword is used to sort query results?
- sortby
- orderby
- order
- arrange
Answer: B) orderby
Explanation:
The orderby clause sorts query results. The descending keyword can be used to specify descending order.
47. What does the following LINQ query return?
int[] numbers = { 2, 5, 8, 11, 14 };
var result = numbers
.Where(n => n % 2 == 0)
.Select(n => n * 10);
{2, 8, 14}
{20, 80, 140}
{50, 110}
{10, 20, 30}
Answer: B) {20, 80, 140}
Explanation:
Where first selects the even numbers 2, 8, and 14. Select then multiplies each selected value by 10, producing 20, 80, and 140.
48. What is the result of the following LINQ expression?
int[] numbers = { 5, 2, 9, 2, 7 };
var result = numbers
.Distinct()
.OrderByDescending(n => n);
{2, 5, 7, 9}
{9, 7, 5, 2}
{9, 5, 2, 2, 7}
{2, 7, 9, 5}
Answer: B) {9, 7, 5, 2}
Explanation:
Distinct removes the duplicate 2. OrderByDescending then sorts the remaining values from largest to smallest.
49. What does the following LINQ expression return?
var numbers = new[] { 10, 20, 30, 40 };
var result = numbers
.Skip(1)
.Take(2)
.ToList();
{10, 20}
{20, 30}
{30, 40}
{20, 30, 40}
Answer: B) {20, 30}
Explanation:
Skip(1) removes the first element, leaving 20, 30, 40. Take(2) then selects the first two remaining elements, resulting in 20, 30. ToList() materializes the result.
50. What is the output of the following LINQ query?
var students = new[]
{
new { Name = "Amit", Marks = 85 },
new { Name = "Neha", Marks = 72 },
new { Name = "Raj", Marks = 91 },
new { Name = "Priya", Marks = 68 }
};
var result = students
.Where(s => s.Marks >= 75)
.OrderByDescending(s => s.Marks)
.Select(s => s.Name)
.ToList();
{ "Amit", "Raj" }
{ "Raj", "Amit" }
{ "Raj", "Neha", "Amit" }
{ "Amit", "Neha", "Raj" }
Answer: B) { "Raj", "Amit" }
Explanation:
The Where method keeps only students whose marks are greater than or equal to 75. Therefore, Amit (85) and Raj (91) are included, while Neha (72) and Priya (68) are excluded. Next, OrderByDescending sorts the remaining students by their marks from highest to lowest, placing Raj (91) before Amit (85). Finally, Select returns only their names, and ToList() materializes the result as a list.
Advertisement
Advertisement