Challenge - 5 Problems
Predicate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Predicate delegate example?
Consider the following C# code using a Predicate delegate. What will be printed to the console?
C Sharp (C#)
using System; class Program { static void Main() { Predicate<int> isEven = x => x % 2 == 0; Console.WriteLine(isEven(10)); Console.WriteLine(isEven(7)); } }
Attempts:
2 left
💡 Hint
Predicate returns a boolean indicating if the condition is met.
✗ Incorrect
The Predicate delegate checks if the number is even. 10 is even, so True; 7 is odd, so False.
❓ Predict Output
intermediate2:00remaining
What does this Predicate delegate filter produce?
Given the following code, what is the output of the filtered list printed?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new() {1, 2, 3, 4, 5}; Predicate<int> isGreaterThanThree = n => n > 3; List<int> filtered = numbers.FindAll(isGreaterThanThree); foreach (var num in filtered) { Console.Write(num + " "); } } }
Attempts:
2 left
💡 Hint
Predicate filters elements where the condition is true.
✗ Incorrect
Only numbers greater than 3 are included: 4 and 5.
❓ Predict Output
advanced2:00remaining
What is the output of this Predicate delegate with method group?
Analyze the code below. What will be printed when running this program?
C Sharp (C#)
using System; class Program { static bool IsPositive(int x) => x > 0; static void Main() { Predicate<int> check = IsPositive; Console.WriteLine(check(-1)); Console.WriteLine(check(0)); Console.WriteLine(check(5)); } }
Attempts:
2 left
💡 Hint
The method returns true only if the number is greater than zero.
✗ Incorrect
Negative and zero are not positive, so False; 5 is positive, so True.
❓ Predict Output
advanced2:00remaining
What error does this Predicate delegate code produce?
What error will occur when compiling or running this code?
C Sharp (C#)
using System; class Program { static void Main() { Predicate<string> isLong = s => s.Length > 5; Console.WriteLine(isLong(null)); } }
Attempts:
2 left
💡 Hint
Calling Length on null causes an error.
✗ Incorrect
The lambda tries to access Length of null, causing a NullReferenceException at runtime.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting list after applying this Predicate?
Given the code below, how many integers remain in the list after filtering with the Predicate?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { List<int> nums = new() { -2, -1, 0, 1, 2, 3 }; Predicate<int> pred = x => x * x > 1; List<int> result = nums.FindAll(pred); Console.WriteLine(result.Count); } }
Attempts:
2 left
💡 Hint
Check which numbers squared are greater than 1.
✗ Incorrect
Numbers with squares > 1 are -2(4), -1(1), 0(0), 1(1), 2(4), 3(9). Only -2, 2, 3 satisfy x*x > 1, but -1 and 1 do not (1 is not > 1). So count is 3.