Java Predicate MCQ

Java Predicate MCQ | We have discussed in detail regarding Java Predicate Functional Interface. Now let us see some questions about it.

Q1. Which of the following abstract method present in the Predicate interface?
A. test()
B. apply()
C. get()
D. accept()

View Answer Answer:- A. test()

Explanation: Predicate functional interface contains only one abstract method: test()

Q2. Which of the following is the static method present in the Predicate interface?
A. test()
B. and()
C. or()
D. isEqual()

View Answer Answer:- D

Explanation: Predicate functional interface contains only one static method: isEqual()

Q3. Which of the following default methods are present in the Predicate interface?
A. and()
B. or()
C. negate()
D. All the above

View Answer Answer:- D

Explanation: Predicate Functional interface contains the following 3 default methods: and(), or(), and not().

Q4. Which of the following is a Predicate interface declaration?

interface Predicate<T>
{
  public boolean test(T t);
}
interface Predicate<T>
{
   public boolean apply(T t);
}
interface Predicate<T,R>
{
   public R test(T t);
}
interface Predicate<T,R>
{
   public R apply(T t);
}
View Answer Answer: A

Explanation: Predicate interface can take only one Type parameter which represents only input type. We are not required to specify return type because return type is always boolean type.

interface Predicate
{
  public boolean test(T t);
}

Q5. Which of the following is a valid Predicate to check whether the given Integer is divisible by 10 or not?
A. Predicate<Integer> p = i -> i%10 == 10;
B. Predicate<Integer, Boolean> p =i->i%10==0;
C. Predicate<Boolean, Integer> p =i->i%10==0;
D. None of the above

View Answer Answer: A

Q6. Which of the following is valid regarding Predicate functional interface?
A. Predicate Functional interface present in java.util.function package.
B. It is introduced in Java 1.8 version.
C. We can use Predicate to implement conditional checks.
D. It is possible to join 2 predicates into a single predicate also.
E. All the above

View Answer Answer: E

Q7. Which of the following is a valid Predicate to check whether the given user is admin or not?
A. Predicate<User> p=user->user.getRole().equals(“Admin”);
B. Predicate<Boolean> p=user->user.getRole().equals(“Admin”);
C. Predicate<User> p=(user,s=”admin”)->user.getRole().equals(s);
D. None of the above

View Answer Answer: A

Predicate interface can take only one Type parameter which represents only input type. We are not required to specify return type because return type is always boolean type.

Q8. Consider the following Predicates.
Predicate<Integer> p1=i->i%2==0;
Predicate<Integer> p1=i->i>10;

Which of the following is invalid?
A. p1.and(p2)
B. p1.or(p2)
C. p1.negate(p2)
D. p1.negate()

View Answer Answer: C

Explanation: negate() method don’t take any argument.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *