Java OCA OCP Practice Question 756

Question

How many lines does this code output?

import java.util.*; 
import java.util.function.*; 
? 
public class Main { 
   public static void main(String[] args) { 
      List<String> list = new ArrayList<>(); 
      list.add("-5"); 
      list.add("0"); 
      list.add("5"); 
      print(list, e -> e < 0); //from w  ww. j  av a2 s  . c  o  m
   } 
   public static void print(List<String> list, Predicate<Integer> p) { 
      for (String num : list) 
         if (p.test(num)) 
            System.out.println(num); 
   } 
} 
  • A. One
  • B. Two
  • C. None. The code does not compile.
  • D. None. The code throws an exception at runtime.


C.

Note

Pay attention to the data types.

The print() method is looping through a list of String objects.

The Predicate expects an Integer.

Since these don't match, the if statement does not compile.




PreviousNext

Related