nerdexam
Oracle

1Z0-809 · Question #76

Given: public class Case { public static void main(String[] args) { String product = "Pen"; product.toLowerCase(); product.concat(" " + product.toLowerCase()); System.out.print(product.substring(4…

E is correct because Java's String type is immutable - methods like toLowerCase() and concat() return new String objects but do not modify the original. Since the results are never reassigned (e.g., product = product.concat(...)), product remains "Pen" (length 3) throughout…

Question

Given: public class Case { public static void main(String[] args) { String product = "Pen"; product.toLowerCase(); product.concat(" " + product.toLowerCase()); System.out.print(product.substring(4, 6)); } } What is the result?

Options

  • Abox
  • Bnbo
  • Cen
  • Dnb
  • EAn exception is thrown at runtime

Explanation

E is correct because Java's String type is immutable - methods like toLowerCase() and concat() return new String objects but do not modify the original. Since the results are never reassigned (e.g., product = product.concat(...)), product remains "Pen" (length 3) throughout. Calling product.substring(4, 6) on a 3-character string attempts to start at index 4, which doesn't exist, throwing a StringIndexOutOfBoundsException at runtime.

A, B, C, and D are all wrong because they assume the discarded method calls actually changed product - for example, some choices reflect results you'd get from "Pen pen" or "pen pen", which is what product would contain if the concat result had been saved. C ("en") is especially tempting because "Pen".substring(1, 3) would yield "en", but substring(4, 6) is a completely different call.

Memory tip: In Java, treat String methods like a blender that creates a new smoothie but leaves the original fruit untouched - if you don't catch (reassign) the output, it's gone. Always ask: "Was the return value stored?" If not, the original string is unchanged.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice