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

LINQ method syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LINQ Mastery Badge
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 method syntax code?
Consider the following C# code using LINQ method syntax. What will be printed to the console?
C Sharp (C#)
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var result = numbers.Where(n => n % 2 == 0).Select(n => n * n);
foreach(var num in result) {
    Console.Write(num + " ");
}
A2 4
B1 9 25
C4 16
D1 2 3 4 5
Attempts:
2 left
💡 Hint
Think about which numbers are even and what happens to them in the Select step.
🧠 Conceptual
intermediate
1:30remaining
Which LINQ method syntax expression counts elements greater than 10?
You have a list of integers. Which of the following LINQ method syntax expressions correctly counts how many elements are greater than 10?
Anumbers.Select(n => n > 10).Count()
Bnumbers.Count(n => n > 10)
Cnumbers.Where(n => n > 10).Count()
Dnumbers.Count().Where(n => n > 10)
Attempts:
2 left
💡 Hint
Count can take a condition directly or you can filter first then count.
🔧 Debug
advanced
2:00remaining
What error does this LINQ method syntax code produce?
Examine the code below. What error will it cause when compiled or run?
C Sharp (C#)
var words = new List<string> { "apple", "banana", "cherry" };
var result = words.Select(word => word.Length > 5 ? word : );
foreach(var w in result) {
    Console.WriteLine(w);
}
AInvalidOperationException: Sequence contains no elements
BNullReferenceException at runtime
CNo error, prints all words
DSyntaxError: Missing expression after ':' in ternary operator
Attempts:
2 left
💡 Hint
Look carefully at the ternary operator syntax inside Select.
Predict Output
advanced
2:30remaining
What is the output of this LINQ method syntax with grouping?
Given the code below, what will be printed to the console?
C Sharp (C#)
var fruits = new List<string> { "apple", "apricot", "banana", "blueberry", "cherry" };
var groups = fruits.GroupBy(f => f[0]);
foreach(var group in groups) {
    Console.Write(group.Key + ":");
    Console.WriteLine(string.Join(",", group));
}
A
a:apple,apricot
b:banana,blueberry
c:cherry
B
apple:apricot
banana:blueberry
cherry:
C
a:apple
apricot
b:banana
blueberry
c:cherry
D
a:apple,banana
b:apricot,blueberry
c:cherry
Attempts:
2 left
💡 Hint
GroupBy groups by the first letter of each fruit.
Predict Output
expert
3:00remaining
What is the value of 'result' after this LINQ method syntax chain?
Analyze the code below. What is the value of the variable 'result' after execution?
C Sharp (C#)
var data = new List<int> { 3, 6, 9, 12, 15 };
var result = data.Aggregate(1, (acc, val) => val % 3 == 0 ? acc * val : acc);
A29160
B0
C1
D45
Attempts:
2 left
💡 Hint
Aggregate starts with 1 and multiplies values divisible by 3.