The lambda (x, y) => x + y takes two inputs and returns their sum, matching the delegate.
Final Answer:
Func<int, int, int> add = (x, y) => x + y; -> Option B
Quick Check:
Two inputs + one output = Func<int, int, int> [OK]
Hint: Count inputs, last type is output in Func<> [OK]
Common Mistakes:
Using wrong number of generic parameters
Mixing input and output types order
Incorrect lambda parameter count
3. What is the output of the following code?
Func<int, int, int> multiply = (a, b) => a * b;
int result = multiply(3, 4);
Console.WriteLine(result);
medium
A. 12
B. 7
C. 34
D. Error
Solution
Step 1: Understand the Func delegate and lambda
The delegate multiply takes two integers and returns their product.
Step 2: Calculate the result of multiply(3, 4)
3 multiplied by 4 equals 12.
Final Answer:
12 -> Option A
Quick Check:
3 * 4 = 12 [OK]
Hint: Multiply inputs as lambda defines [OK]
Common Mistakes:
Adding instead of multiplying
Concatenating numbers as strings
Expecting syntax error
4. Identify the error in this code snippet:
Func<int, int> square = x => x * x;
int result = square(5, 2);
Console.WriteLine(result);
medium
A. Syntax error in lambda expression
B. Missing return statement in lambda
C. Incorrect delegate type for lambda
D. Too many arguments passed to the Func delegate
Solution
Step 1: Check Func delegate signature
Func<int, int> expects one integer input and returns an integer.
Step 2: Check method call arguments
The call square(5, 2) passes two arguments, but the delegate expects only one.
Final Answer:
Too many arguments passed to the Func delegate -> Option D
Quick Check:
One input expected, two given = error [OK]
Hint: Match argument count to Func input count [OK]
Common Mistakes:
Passing wrong number of arguments
Confusing input and output types
Assuming lambda needs explicit return
5. You want to create a Func delegate that takes a string and returns its length only if the string is not null or empty; otherwise, it returns zero. Which of the following implementations correctly achieves this?
hard
A. Func lengthFunc = s => { if (s == null) return 0; else return s.Length; };
B. Func lengthFunc = s => s.Length;
C. Func lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length;
D. Func lengthFunc = s => s == "" ? s.Length : 0;
Solution
Step 1: Understand the requirement
The function must return 0 if the string is null or empty, else return its length.
Step 2: Analyze each option
Func lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length; uses string.IsNullOrEmpty to check both conditions and returns 0 or length accordingly, which is correct. Func lengthFunc = s => s.Length; does not check for null or empty and may throw an error. Func lengthFunc = s => { if (s == null) return 0; else return s.Length; }; checks only for null, not empty. Func lengthFunc = s => s == "" ? s.Length : 0; returns length only if string is empty, which is opposite.
Final Answer:
Func<string, int> lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length; -> Option C
Quick Check:
Check null or empty with IsNullOrEmpty = Func lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length; [OK]
Hint: Use string.IsNullOrEmpty to check safely [OK]
Common Mistakes:
Not checking empty string
Only checking null but not empty
Returning wrong value for empty string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.