LINQ and Lambda Expressions in C#: Fundamentals Quiz Quiz

Challenge your understanding of LINQ and lambda expressions in C# with this quiz covering essential concepts, syntax, and common operations. Perfect for beginners looking to assess their foundational knowledge in querying and manipulating data collections using LINQ features and lambda syntax.

  1. Filtering Numbers with LINQ

    Which LINQ method would you use to filter out only even numbers from an integer array in C#?

    1. Select
    2. Where
    3. Sum
    4. OrderBy

    Explanation: The Where method is used to filter collections based on a specified condition, such as checking if a number is even. Select projects elements without filtering, so it cannot filter values. OrderBy is used for sorting, not filtering. Sum adds numeric values together and does not filter items.

  2. Lambda Expression Syntax

    How is a lambda expression with one parameter and multiplication written in C#?

    1. x =u003E x * 2
    2. x u003C = x * 2
    3. x - u003E x * 2
    4. x u003C= x * 2

    Explanation: The correct lambda syntax for 'parameter goes to expression' uses '=u003E', making 'x =u003E x * 2' correct. 'x u003C = x * 2' and 'x u003C= x * 2' both use the wrong operators, which do not exist for lambda expressions. 'x - u003E x * 2' has a typographical error with spaces and the wrong symbol.

  3. Finding Elements with LINQ

    Which method returns the first element matching a condition or a default value if none is found?

    1. FirstOrDefault
    2. Average
    3. Count
    4. Select

    Explanation: FirstOrDefault retrieves the first element matching the condition or returns a default value if none exists. Select only transforms each item and does not search for a match. Average calculates the average and Count returns the number of elements, which does not relate to element retrieval.

  4. Projecting Data with LINQ

    What does the Select method do in a LINQ query?

    1. Sorts elements
    2. Projects each element into a new form
    3. Removes duplicates
    4. Filters elements

    Explanation: Select is used to project each element in a sequence into a new form, such as selecting specific properties or transforming data. Filtering is the purpose of Where, not Select. OrderBy sorts elements, while Distinct is used to remove duplicates.

  5. Lambda in the OrderBy Method

    Which lambda expression sorts a list of strings by their length using OrderBy?

    1. x u003C- x.Length
    2. x -u003E x.Length
    3. x == x.Length
    4. x =u003E x.Length

    Explanation: To sort strings by length, OrderBy can use the lambda 'x =u003E x.Length'. The operator '=u003E' is correct for lambdas. 'x u003C- x.Length' uses a non-existent syntax. 'x -u003E x.Length' is incorrect as it uses the wrong arrow, and 'x == x.Length' checks equality, not sorting criteria.

  6. LINQ Query Result Type

    What does a LINQ query return when executed against a Listu003Cintu003E collection?

    1. DataTable
    2. IEnumerableu003Cintu003E
    3. Listu003Cstringu003E
    4. int

    Explanation: LINQ queries typically return IEnumerableu003CTu003E, allowing for deferred execution and iteration. 'int' would mean a single value, which is not returned by the query. 'Listu003Cstringu003E' is a type mismatch, and 'DataTable' is unrelated to basic LINQ queries over collections.

  7. Using Lambda for Counting

    How would you count all words longer than five letters in a list with LINQ and lambda?

    1. words.Count(w =u003E w.Length u003E 5)
    2. words.Count(w == w.Length u003E 5)
    3. words.Count(w u003C w.Length u003E 5)
    4. words.Count(w =u003E w.Length u003C 5)

    Explanation: The first option, 'words.Count(w =u003E w.Length u003E 5)', passes a lambda that checks the condition correctly. 'w u003C w.Length u003E 5' and 'w == w.Length u003E 5' use wrong or invalid comparators. 'w.Length u003C 5' counts words shorter than five, not longer.

  8. LINQ Query Syntax vs. Method Syntax

    Which of the following is TRUE about LINQ query syntax and method syntax?

    1. Only query syntax is valid for collections.
    2. Method syntax does not support lambda expressions.
    3. Query syntax cannot perform filtering.
    4. Both can be used to perform similar data queries.

    Explanation: Both query syntax and method syntax can perform most LINQ operations, and method syntax often uses lambdas for flexibility. Collections can use both syntaxes. Method syntax fully supports lambdas. Query syntax can perform filtering through constructs like 'where'.

  9. Combining LINQ Operators

    Which LINQ method chain filters even numbers and then returns them sorted ascending?

    1. Count(...).Select(...)
    2. Select(...).First(...)
    3. Where(...).OrderBy(...)
    4. Sum(...).Where(...)

    Explanation: Applying Where followed by OrderBy first filters the items, then sorts the filtered results. Select(...) projects items, but does not filter and sort. Sum(...) and Count(...) return numerical results and do not produce a sorted collection. Changing the order would not yield the desired outcome.

  10. Lambda Expression Return Type

    What is the return type of the lambda expression: x =u003E x + 1 where x is an int?

    1. int
    2. float
    3. bool
    4. string

    Explanation: The given lambda receives an int and adds 1, so it returns an int value. 'string' is not returned by this operation unless the result is converted. 'bool' would result from a comparison, not addition, and 'float' could occur with explicit conversion, but that's not shown here.