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

Predicate delegate type in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Predicate 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 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));
    }
}
ATrue\nFalse
BFalse\nTrue
C10\n7
DCompilation error
Attempts:
2 left
💡 Hint
Predicate returns a boolean indicating if the condition is met.
Predict Output
intermediate
2: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 + " ");
        }
    }
}
A3 4 5
B1 2 3
C4 5
DCompilation error
Attempts:
2 left
💡 Hint
Predicate filters elements where the condition is true.
Predict Output
advanced
2: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));
    }
}
ATrue\nFalse\nTrue
BFalse\nFalse\nTrue
CFalse\nTrue\nTrue
DCompilation error
Attempts:
2 left
💡 Hint
The method returns true only if the number is greater than zero.
Predict Output
advanced
2: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));
    }
}
ASystem.NullReferenceException at runtime
BCompilation error: cannot convert lambda
COutput: False
DOutput: True
Attempts:
2 left
💡 Hint
Calling Length on null causes an error.
🧠 Conceptual
expert
2: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);
    }
}
A6
B4
C5
D3
Attempts:
2 left
💡 Hint
Check which numbers squared are greater than 1.