ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
System.out.print("a,b,,c,,".split(",").length + " " + ",a".split(",").length + " " + "a,b,,c,,".split(",", -1).length);
  1. A. 6 2 6
  2. B. 4 1 6
  3. C. 4 2 6
  4. D. 4 2 4

Correct answer: C. 4 2 6

split(regex) drops TRAILING empty strings only, so a,b,,c,, gives [a, b, , c] (4) and ,a keeps its leading empty string (2). A negative limit keeps trailing empties: 6.

More OOP with C++ and Java questions

Exams that ask this topic