Java OCA OCP Practice Question 795

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<Integer> list= new ArrayList<>(); 
     list.add(-5); /*from   w w w . j  av a  2 s. com*/
     list.add(0); 
     list.add(5); 
     print(list, e -> e < 0); 
  } 
? 
  public static void print(List<Integer> list, Predicate<Integer> p) { 
      for (Integer num : list) 
         if (p.test(num)) 
            System.out.println(num); 
  } 
} 
  • A. One
  • B. Two
  • C. Three
  • D. None. It doesn't compile.
  • E. None. It throws an exception at runtime.


A.

Note

This is a correct example of using lambdas.

The code creates an ArrayList with three elements.

The print() method loops through and checks for negative numbers.

Option A is correct.




PreviousNext

Related