Java OCA OCP Practice Question 2211

Question

Choose the correct option based on this program:

import java.util.function.ObjIntConsumer;

public class Main {
    public static void main(String []args) {
       ObjIntConsumer<String> charAt = (str, i) -> str.charAt(i); // #1
       System.out .println(charAt.accept("java", 2));              // #2
    }
}
  • A. this program results in a compiler error for the line marked with comment #1
  • B. this program results in a compiler error for the line marked with comment #2
  • C. this program prints: a
  • d. this program prints: v
  • e. this program prints: 2


d.

Note

ObjIntConsumer operates on the given String argument str and int argument i and returns nothing.

though the charAt method is declared to return the char at given index i, the accept method in ObjIntConsumer has return type void.

since System.out.println expects an argument to be passed, the call charAt.accept("java", 2) results in a compiler error because accept() method returns void.




PreviousNext

Related