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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LINQ Query 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 query?

Consider the following C# code using LINQ query syntax. What will be printed?

C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 };
        var query = from n in numbers
                    where n % 2 == 0
                    select n * n;
        foreach (var item in query) {
            Console.Write(item + " ");
        }
    }
}
A2 4
B1 9 25
C4 16
D1 4 9 16 25
Attempts:
2 left
💡 Hint

Look at the where clause filtering even numbers, then the select projects squares.

🧠 Conceptual
intermediate
2:00remaining
Which LINQ query syntax correctly selects names starting with 'J'?

Given a list of names, which LINQ query syntax correctly selects only those starting with the letter 'J'?

C Sharp (C#)
string[] names = { "John", "Alice", "James", "Bob" };
Avar result = from name in names select name where name.StartsWith("J");
Bvar result = from name in names where name.StartsWith("J") select name;
Cvar result = from name in names select name if name.StartsWith("J");
Dvar result = from name in names where name.Contains("J") select name;
Attempts:
2 left
💡 Hint

Remember the where clause comes before select in query syntax.

🔧 Debug
advanced
2:00remaining
What error does this LINQ query produce?

Examine the following LINQ query code. What error will it cause when compiled?

C Sharp (C#)
int[] nums = {1, 2, 3};
var q = from n in nums
        where n > 1
        select n > 2 ? n : ;
ASyntaxError: incomplete ternary operator
BTypeError: cannot convert bool to int
CRuntime NullReferenceException
DNo error, outputs 3
Attempts:
2 left
💡 Hint

Look carefully at the ternary operator syntax in the select clause.

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

What will be printed by this C# program using LINQ query syntax with ordering?

C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        string[] fruits = { "apple", "banana", "cherry", "date" };
        var query = from f in fruits
                    orderby f.Length descending, f
                    select f;
        foreach (var fruit in query) {
            Console.Write(fruit + " ");
        }
    }
}
Adate apple banana cherry
Bbanana cherry date apple
Ccherry banana apple date
Dbanana cherry apple date
Attempts:
2 left
💡 Hint

Order by length descending, then alphabetically ascending.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting collection?

Given this LINQ query, how many items will the result collection contain?

C Sharp (C#)
int[] values = { 2, 4, 6, 8, 10 };
var result = from v in values
             where v % 4 == 0
             select v / 2;
A2
B3
C4
D5
Attempts:
2 left
💡 Hint

Check which numbers in the array are divisible by 4.