nerdexam
Oracle

1Z0-809 · Question #199

Given the code fragment: final String str1 = "Java"; StringBuffer strBuf = new StringBuffer("Course"); UnaryOperator<String> s = (str2) -> str1.concat(str2); // line n1 UnaryOperator<String> c =…

The correct answer is A. Val:20 Val:40 Val:60. There appears to be an error in the provided correct answer. The code as written actually produces a compilation error (option C), not option A. The code has two distinct compilation problems: first, the variable u is never declared - only s and c are defined, so u.apply(...)…

Question

Given the code fragment: final String str1 = "Java"; StringBuffer strBuf = new StringBuffer("Course"); UnaryOperator<String> s = (str2) -> str1.concat(str2); // line n1 UnaryOperator<String> c = (str3) -> str3.toLowerCase(); System.out.println(u.apply(c.apply(strBuf))); // line n2 What is the result?

Options

  • AVal:20 Val:40 Val:60
  • BVal:10 Val:20 Val:30
  • CA compilation error occurs.
  • DVal: Val: Val:

How the community answered

(51 responses)
  • A
    78% (40)
  • B
    6% (3)
  • C
    14% (7)
  • D
    2% (1)

Explanation

There appears to be an error in the provided correct answer. The code as written actually produces a compilation error (option C), not option A.

The code has two distinct compilation problems: first, the variable u is never declared - only s and c are defined, so u.apply(...) on line n2 references an undefined symbol; second, c.apply(strBuf) passes a StringBuffer to a UnaryOperator<String>, but StringBuffer is not a String, causing a type mismatch - Java generics are not covariant here and no implicit conversion exists. Options A, B, and D all describe runtime output, which is impossible to reach when the code won't compile; option D might tempt you if you assume concat or toLowerCase could return empty strings, but that reasoning is moot. The memory tip: whenever you see a lambda chain, trace the declared variable names carefully - if the code uses a name that was never assigned, it's a compile error before any logic runs.

Bottom line: The correct answer for this code is C, not A. If this is from an official exam source, the question was likely transcribed with errors (either u should be s, or the correct answer label is wrong).

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice