A Predicate delegate is a special type of delegate that represents a method that takes one input parameter and returns a boolean value (true or false). It is often used to test conditions.
Click to reveal answer
beginner
How do you declare a Predicate delegate for integers?
You declare it as Predicate<int>. For example: Predicate<int> isEven = x => x % 2 == 0; This means isEven is a method that checks if an integer is even.
Click to reveal answer
beginner
What is the return type of a Predicate delegate method?
The return type is always bool. It returns true if the condition is met, otherwise false.
Click to reveal answer
intermediate
Can Predicate delegates be used with List<T> methods? Give an example.
Yes, Predicate delegates are commonly used with List<T> methods like Find or RemoveAll. Example: list.RemoveAll(x => x < 0); removes all negative numbers from the list.
Click to reveal answer
intermediate
What is the difference between Predicate<T> and Func?
Predicate<T> is a delegate specifically for methods that take one parameter and return bool. Func can do the same but is more general and can have multiple parameters. Predicate<T> is clearer when you want a boolean test.
Click to reveal answer
What does a Predicate delegate return?
Aint
Bstring
Cbool
Dvoid
✗ Incorrect
A Predicate delegate always returns a boolean value (true or false).
Which of these is a valid Predicate declaration for strings?
APredicate<string> isEmpty = s => s.Length == 0;
BPredicate<string> isEmpty = s => s + "";
CPredicate<string> isEmpty = s => Console.WriteLine(s);
DPredicate<string> isEmpty = s => s.Length;
✗ Incorrect
Predicate must return a bool. s => s.Length == 0 returns true if the string is empty.
Which List<T> method commonly uses Predicate<T>?
AAdd
BFind
CSort
DClear
✗ Incorrect
List.Find uses a Predicate to locate the first element matching the condition.
What parameter count does a Predicate delegate accept?
AOne
BMultiple
CTwo
DZero
✗ Incorrect
Predicate delegates accept exactly one input parameter.
Which is a key difference between Predicate<T> and Func?
BPredicate<T> is a class, Func<T, bool> is a delegate
CPredicate<T> can only be used with strings
DPredicate<T> only takes one parameter, Func<T, bool> can take multiple
✗ Incorrect
Predicate takes one parameter and returns bool; Func can take multiple parameters and returns bool.
Explain what a Predicate delegate is and how it is used in C#.
Think about how you test conditions with a method that returns true or false.
You got /4 concepts.
Describe the difference between Predicate and Func delegates.
Focus on parameter count and clarity of intent.
You got /5 concepts.
Practice
(1/5)
1.
What does a Predicate<T> delegate represent in C#?
easy
A. A method that takes a parameter of type T and returns a boolean indicating a condition.
B. A method that takes no parameters and returns void.
C. A method that takes two parameters and returns an integer.
D. A method that returns a string representation of an object.
Solution
Step 1: Understand the Predicate delegate signature
A Predicate delegate always takes one parameter of type T and returns a boolean value.
Step 2: Match the description to the options
A method that takes a parameter of type T and returns a boolean indicating a condition correctly describes this signature as a method taking T and returning bool.
Final Answer:
A method that takes a parameter of type T and returns a boolean indicating a condition. -> Option A
Quick Check:
Predicate<T> = method(T) returns bool [OK]
Hint: Predicate always returns true or false for a given input [OK]
Common Mistakes:
Confusing Predicate with Action or Func delegates
Thinking Predicate returns void or other types
Assuming Predicate takes multiple parameters
2.
Which of the following is the correct syntax to declare a Predicate delegate for integers?
Predicate<int> expects a method returning bool. Predicate<int> isPositive = x => x > 0; returns x > 0, which is bool.
Step 2: Verify parameter and return correctness
Predicate<int> isPositive = (int x) => { return x; }; returns x (int), not bool. Predicate<int> isPositive = x => x + 1; returns x + 1 (int), not bool. Predicate<int> isPositive = () => true; has no parameter but Predicate requires one.
Final Answer:
Predicate<int> isPositive = x => x > 0; -> Option A
Quick Check:
Predicate<int> = int input => bool output [OK]
Hint: Predicate must return bool, not int or void [OK]
Common Mistakes:
Returning int instead of bool
Missing parameter in lambda
Using incorrect lambda syntax
3.
What will be the output of the following C# code?
using System;
class Program {
static void Main() {
Predicate<string> isLong = s => s.Length > 5;
string[] words = {"apple", "banana", "pear"};
int count = 0;
foreach (var word in words) {
if (isLong(word)) count++;
}
Console.WriteLine(count);
}
}
medium
A. 3
B. 2
C. 1
D. 0
Solution
Step 1: Understand the Predicate condition
The predicate checks if the string length is greater than 5.
Words with length > 5 are "banana" only, count = 1 [Incorrect]
Hint: Count items where predicate returns true [OK]
Common Mistakes:
Counting words with length equal to 5
Misreading predicate condition
Assuming all words pass the test
4.
Identify the error in this code using a Predicate delegate:
Predicate<int> isEven = n => n % 2 == 0;
int[] numbers = {1, 2, 3, 4};
int evenCount = 0;
foreach (int num in numbers) {
if (isEven) evenCount++;
}
Console.WriteLine(evenCount);
medium
A. The Predicate delegate is not assigned correctly.
B. The foreach loop variable should be declared as var.
C. The if condition should call the delegate with an argument.
D. The Console.WriteLine statement is missing parentheses.
Solution
Step 1: Check how Predicate is used in if condition
The code uses if (isEven) which checks the delegate itself, not its result.
Step 2: Correct usage requires calling the delegate with an argument
It should be if (isEven(num)) to evaluate the condition for each number.
Final Answer:
The if condition should call the delegate with an argument. -> Option C
Quick Check:
Call Predicate with input: isEven(num) [OK]
Hint: Always call Predicate with an argument inside if [OK]
Common Mistakes:
Using delegate name without parentheses
Confusing delegate variable with method call
Ignoring parameter requirement
5.
You want to filter a list of integers to keep only positive even numbers using a Predicate delegate. Which of the following implementations correctly combines these conditions?
hard
A. Predicate<int> isPositiveEven = x => x % 2 == 0;
B. Predicate<int> isPositiveEven = x => x > 0 || x % 2 == 0;
C. Predicate<int> isPositiveEven = x => x < 0 && x % 2 == 0;
D. Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0;
Solution
Step 1: Understand the filtering requirement
We want numbers that are both positive and even.
Step 2: Analyze each option's condition
Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0; uses AND (&&) to require both conditions true. Predicate<int> isPositiveEven = x => x > 0 || x % 2 == 0; uses OR (||), allowing positives or evens, which is incorrect. Predicate<int> isPositiveEven = x => x < 0 && x % 2 == 0; checks for negative and even, wrong sign. Predicate<int> isPositiveEven = x => x % 2 == 0; checks only even, missing positivity.
Final Answer:
Predicate<int> isPositiveEven = x => x > 0 && x % 2 == 0; -> Option D
Quick Check:
Use AND to combine conditions for filtering [OK]
Hint: Use && to combine multiple conditions in Predicate [OK]
Common Mistakes:
Using || instead of && for combined conditions
Checking wrong sign for positivity
Ignoring one of the required conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.