Challenge - 5 Problems
Select Clause Master
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 Select projection?
Consider the following C# code using LINQ to project a list of numbers to their squares. 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}; var squares = numbers.Select(x => x * x); foreach(var sq in squares) { Console.Write(sq + " "); } } }
Attempts:
2 left
💡 Hint
Remember that Select projects each element to a new value.
✗ Incorrect
The Select method applies the lambda x => x * x to each element, producing their squares: 1, 4, 9, 16.
❓ Predict Output
intermediate2:00remaining
What does this Select projection produce?
Given this code snippet, what will be the output?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { string[] words = {"apple", "banana", "cherry"}; var firstChars = words.Select(w => w[0]); foreach(var ch in firstChars) { Console.Write(ch + "-"); } } }
Attempts:
2 left
💡 Hint
Select projects each string to its first character.
✗ Incorrect
Each word is projected to its first character: 'a', 'b', 'c', printed with dashes.
❓ Predict Output
advanced2:00remaining
What is the output of this Select projection with anonymous types?
What will this program print?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { var people = new[] { new { Name = "Alice", Age = 30 }, new { Name = "Bob", Age = 25 } }; var names = people.Select(p => new { p.Name, IsAdult = p.Age >= 18 }); foreach(var n in names) { Console.Write($"{n.Name}:{n.IsAdult} "); } } }
Attempts:
2 left
💡 Hint
Select creates new anonymous objects with Name and IsAdult properties.
✗ Incorrect
Each person is projected to an anonymous object with Name and a boolean IsAdult indicating if Age >= 18.
❓ Predict Output
advanced2:00remaining
What error does this Select projection cause?
What happens when you run this code?
C Sharp (C#)
using System; using System.Linq; class Program { static void Main() { string[] words = {"one", null, "three"}; var lengths = words.Select(w => w.Length); foreach(var len in lengths) { Console.Write(len + " "); } } }
Attempts:
2 left
💡 Hint
One element is null, so accessing Length causes an error.
✗ Incorrect
The Select tries to access Length on a null string, causing a NullReferenceException at runtime.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting sequence after this Select projection?
Given this code, how many elements will the 'result' sequence contain?
C Sharp (C#)
using System.Linq; var numbers = new int[] { 2, 4, 6, 8 }; var result = numbers.Select(x => x / 2).Where(x => x > 2);
Attempts:
2 left
💡 Hint
First Select divides each number by 2, then Where filters those greater than 2.
✗ Incorrect
Dividing numbers by 2 gives {1, 2, 3, 4}. Filtering > 2 leaves {3, 4}, so 2 elements remain.