ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
System.out.print(",x,".split(",").length + " " + "".split(",").length);
  1. A. 1 0
  2. B. 3 1
  3. C. 2 1
  4. D. 2 0

Correct answer: C. 2 1

A leading empty string produced by a real (non-zero-width) match is kept, the trailing one is dropped: ["", "x"]. If the regex never matches, the result is the whole input as one element, so "".split(",") has length 1.

More OOP with C++ and Java questions

Exams that ask this topic