Challenge - 5 Problems
LINQ Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this LINQ query?
Consider the following C# code using LINQ. What will be printed to the console?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { int[] numbers = {1, 2, 3, 4, 5}; var evens = numbers.Where(n => n % 2 == 0); foreach(var num in evens) { Console.Write(num + " "); } } }
Attempts:
2 left
💡 Hint
Think about what the Where method does with the condition n % 2 == 0.
✗ Incorrect
The Where method filters the array to include only numbers divisible by 2, which are 2 and 4.
🧠 Conceptual
intermediate2:00remaining
Why is LINQ useful compared to traditional loops?
Which of the following best explains why LINQ is needed in C# programming?
Attempts:
2 left
💡 Hint
Think about how LINQ changes the way you write queries on data.
✗ Incorrect
LINQ allows writing queries in a clear and declarative style, reducing boilerplate code compared to loops.
❓ Predict Output
advanced2:00remaining
What is the output of this LINQ query with projection?
What will this C# program print when run?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { string[] fruits = {"apple", "banana", "cherry"}; var query = fruits.Select(f => f.ToUpper()); foreach(var fruit in query) { Console.Write(fruit + " "); } } }
Attempts:
2 left
💡 Hint
The Select method transforms each element in the collection.
✗ Incorrect
Select applies the ToUpper method to each string, converting all to uppercase.
❓ Predict Output
advanced2:00remaining
What error does this LINQ code produce?
What error will this code cause when compiled or run?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { int[] numbers = {1, 2, 3}; var result = numbers.Where(n => n > 1).Select(n => n / 0); foreach(var r in result) { Console.WriteLine(r); } } }
Attempts:
2 left
💡 Hint
Look at the division operation inside Select.
✗ Incorrect
Dividing by zero causes a runtime DivideByZeroException when the query is enumerated.
🧠 Conceptual
expert2:00remaining
Which statement best describes deferred execution in LINQ?
Why does LINQ use deferred execution for queries?
Attempts:
2 left
💡 Hint
Think about when LINQ queries actually run.
✗ Incorrect
LINQ queries run only when enumerated, allowing efficient chaining and avoiding unnecessary work.