0
0
C Sharp (C#)programming~20 mins

LINQ performance considerations in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LINQ Performance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
LINQ Deferred Execution Output
What will be the output of this C# code snippet using LINQ deferred execution?
C Sharp (C#)
var numbers = new List<int> {1, 2, 3, 4};
var query = numbers.Where(n => n > 2);
numbers.Add(5);
foreach (var num in query) {
    Console.Write(num + " ");
}
A3 4 5
B1 2 3 4 5
C3 4
D2 3 4 5
Attempts:
2 left
💡 Hint
Remember that LINQ queries are not executed until you iterate over them.
🧠 Conceptual
intermediate
1:30remaining
LINQ Query Performance Impact
Which of the following statements about LINQ query performance is TRUE?
ALINQ queries that use deferred execution can reduce memory usage by processing items one at a time.
BUsing multiple LINQ queries on the same data source always improves performance.
CLINQ queries always execute immediately when declared.
DLINQ queries cannot be optimized by the compiler or runtime.
Attempts:
2 left
💡 Hint
Think about how deferred execution affects memory and processing.
Predict Output
advanced
2:30remaining
LINQ Query Performance with Multiple Enumerations
What is the output of this C# code and what performance issue does it illustrate?
C Sharp (C#)
var data = Enumerable.Range(1, 3);
var query = data.Select(x => {
    Console.WriteLine($"Processing {x}");
    return x * 2;
});
var list1 = query.ToList();
var list2 = query.ToList();
Console.WriteLine(list1.Sum());
A
Processing 1
Processing 2
Processing 3
Processing 1
Processing 2
Processing 3
6
B
Processing 1
Processing 2
Processing 3
Processing 1
Processing 2
Processing 3
12
C12
D
Processing 1
Processing 2
Processing 3
12
Attempts:
2 left
💡 Hint
Consider how many times the query is enumerated and what side effects happen during enumeration.
🔧 Debug
advanced
2:00remaining
Identifying LINQ Performance Bottleneck
This code runs slower than expected. What is the main cause of the performance issue?
C Sharp (C#)
var largeList = Enumerable.Range(1, 1000000).ToList();
var result = largeList.Where(x => x % 2 == 0).OrderByDescending(x => x).Take(10).ToList();
AUsing ToList() at the end causes the query to run multiple times.
BFiltering with Where is slow because it enumerates the entire list twice.
COrdering the entire filtered list before taking 10 items causes unnecessary processing.
DTake(10) should be placed before OrderByDescending to improve performance.
Attempts:
2 left
💡 Hint
Think about how OrderByDescending works on large collections.
🧠 Conceptual
expert
3:00remaining
Best Practice for LINQ Performance Optimization
Which approach is the best practice to improve LINQ query performance on large datasets?
AAvoid using LINQ and write all loops manually for better performance.
BAlways convert queries to lists immediately to avoid deferred execution overhead.
CUse nested LINQ queries extensively to reduce code size and improve speed.
DUse deferred execution and avoid multiple enumerations by caching results when needed.
Attempts:
2 left
💡 Hint
Consider how deferred execution and multiple enumerations affect performance.