ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
// static method of the main class:
static String make() { System.out.print("M"); return "y"; }
// in main:
System.out.print(Optional.of("x").orElse(make()));
System.out.print(Optional.of("x").orElseGet(() -> make()));
  1. A. xx
  2. B. MxMx
  3. C. Mxx
  4. D. My x

Correct answer: C. Mxx

orElse takes an already-evaluated argument, so make() runs even though the Optional has a value. orElseGet takes a Supplier and calls it only when the Optional is empty.

More OOP with C++ and Java questions

Exams that ask this topic