ProgrammingHardProduct-company placement · Java

What is printed?

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

Correct answer: C. 4

split(regex) is split(regex, 0), which keeps empty strings in the middle but drops TRAILING empty strings: "a", "b", "", "c" remain, so the length is 4.

More OOP with C++ and Java questions

Exams that ask this topic