Java OCA OCP Practice Question 2223

Question

Choose the best option based on this program:

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class Main {
     public static void main(String args[]) {
         maxMarks(IntStream.of(52,60,99,80,76));            // #1
     }/*  ww  w. j a  v  a  2 s .  c  om*/
     public static void maxMarks(IntStream marks) {
                OptionalInt max = marks.max();              // #2
             if(max.ifPresent()) {                          // #3
                     System.out.print(max.getAsInt());
             }
     }
}
  • A. this program results in a compiler error in line marked with comment #1
  • B. this program results in a compiler error in line marked with comment #2
  • C. this program results in a compiler error in line marked with comment #3
  • D. this program prints: 99


C.

Note

the ifPresent() method in Optional takes a Consumer<T> as the argument.

this program uses ifPresent() without passing an argument and hence it results in a compiler error.

If the method isPresent() were used instead of ifPresent() in this program, it will compile cleanly and print 99 on the console.




PreviousNext

Related