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

Select clause projection in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Select Clause Master
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 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 + " ");
        }
    }
}
A1 2 3 4
BSystem.Linq.Enumerable+WhereSelectArrayIterator`2[System.Int32,System.Int32]
C1 4 9 16
DCompilation error
Attempts:
2 left
💡 Hint
Remember that Select projects each element to a new value.
Predict Output
intermediate
2: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 + "-");
        }
    }
}
Aa-b-c-
Ba b c
CCompilation error
Dapple-banana-cherry-
Attempts:
2 left
💡 Hint
Select projects each string to its first character.
Predict Output
advanced
2: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} ");
        }
    }
}
AAlice:30 Bob:25
BAlice:True Bob:True
CCompilation error
DSystem.Object System.Object
Attempts:
2 left
💡 Hint
Select creates new anonymous objects with Name and IsAdult properties.
Predict Output
advanced
2: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 + " ");
        }
    }
}
ASystem.NullReferenceException at runtime
B3 5
C3 0 5
DCompilation error
Attempts:
2 left
💡 Hint
One element is null, so accessing Length causes an error.
🧠 Conceptual
expert
2: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);
A4
B1
C3
D2
Attempts:
2 left
💡 Hint
First Select divides each number by 2, then Where filters those greater than 2.