ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class Converter<T, R> {
    R convert(T input, java.util.function.Function<T, R> fn) {
        return fn.apply(input);
    }
}
// in main:
Converter<String, Integer> c = new Converter<>();
Integer result = c.convert("123", Integer::parseInt);
System.out.println(result + 1);
  1. A. 124
  2. B. 1231
  3. C. Compilation error: Integer::parseInt is not a valid Function<String, Integer>
  4. D. 123

Correct answer: A. 124

Integer::parseInt is a static method reference matching Function<String, Integer>'s functional signature (String -> int, autoboxed to Integer); convert() applies it to "123", yielding 123, and result + 1 (auto-unboxed for the arithmetic) is 124.

More OOP with C++ and Java questions

Exams that ask this topic