ProgrammingVery hardProduct-company placement · Java

What is printed?

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

Correct answer: B. 0 3

split takes a REGEX, and an unescaped . matches every character, so every piece is empty; trailing empty strings are removed, leaving length 0. Escaping it as \\. splits on literal dots.

More OOP with C++ and Java questions

Exams that ask this topic