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

Why advanced LINQ matters in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced LINQ Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this LINQ query with grouping?

Consider the following C# code using LINQ to group numbers by their remainder when divided by 3. What will be printed?

C Sharp (C#)
var numbers = new[] {1, 2, 3, 4, 5, 6};
var groups = numbers.GroupBy(n => n % 3).OrderBy(g => g.Key);
foreach (var group in groups)
{
    Console.Write($"{group.Key}: ");
    foreach (var num in group)
        Console.Write(num + " ");
    Console.WriteLine();
}
A
0: 3 6 
1: 1 4 
2: 2 5 
B
0: 3 6 
1: 1 2 
2: 4 5 
C
0: 6 3 
1: 1 4 
2: 2 5 
D
0: 3 6 
1: 4 1 
2: 5 2 
Attempts:
2 left
💡 Hint

Remember that GroupBy preserves the order of elements within each group.

🧠 Conceptual
intermediate
1:30remaining
Why use deferred execution in LINQ?

Which of the following best explains the advantage of deferred execution in LINQ?

AIt forces all data to be loaded from the database at once to avoid multiple calls.
BIt allows queries to be executed immediately and results stored in memory.
CIt converts LINQ queries into SQL statements automatically.
DIt delays query execution until the results are actually needed, improving performance and resource use.
Attempts:
2 left
💡 Hint

Think about when the data is actually fetched or processed.

Predict Output
advanced
2:00remaining
What is the output of this LINQ query with SelectMany?

Given the following code, what will be printed?

C Sharp (C#)
var data = new[] { new[] {1, 2}, new[] {3, 4}, new[] {5} };
var result = data.SelectMany(arr => arr.Where(x => x % 2 == 0));
foreach (var num in result)
{
    Console.Write(num + " ");
}
A2 4 5
B1 2 3 4 5
C2 4
D1 3 5
Attempts:
2 left
💡 Hint

SelectMany flattens collections and Where filters elements.

🔧 Debug
advanced
1:30remaining
What error does this LINQ code produce?

Examine the following code snippet. What error will it cause when run?

C Sharp (C#)
var list = new List<int> {1, 2, 3};
var query = list.Select(x => x / (x - 2));
foreach (var val in query)
{
    Console.Write(val + " ");
}
ANullReferenceException
BDivideByZeroException
CInvalidOperationException
DNo error, outputs: 1 2 3
Attempts:
2 left
💡 Hint

Check what happens when x equals 2.

🚀 Application
expert
2:00remaining
How many items are in the resulting dictionary?

Consider this LINQ query that creates a dictionary from a list of strings. How many items will the dictionary contain?

C Sharp (C#)
var words = new List<string> {"apple", "banana", "apricot", "blueberry", "avocado"};
var dict = words.GroupBy(w => w[0])
                .ToDictionary(g => g.Key, g => g.Count());
Console.WriteLine(dict.Count);
A3
B4
C5
D2
Attempts:
2 left
💡 Hint

Look at the first letters of each word and count unique letters.