ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
System.out.print("a.b.c".split(".").length + " " + "a.b.c".split("\\.").length);- A. 3 3
- B. 0 3
- C. 5 3
- 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
- What is printed? java System.out.print("a.b".replace(".", "x") + " " + "a.b".replaceAll(".", "x"));
- What is printed? java List<Integer> src = List.of(1, 2); List<Integer> dst = new ArrayList<>(5); Collections.copy(dst, src)…
- What is printed? java enum Size { S; public String toString() { return "small"; } } // in main: System.out.print(Size.S + " " +…
- What is printed? java Object o = new int[]{1}; System.out.print((o instanceof int[]) + " " + (o instanceof Object[]));
- Which line fails to compile? java class P { void run() throws java.io.IOException { } } class C extends P { @Override void run() { } } //…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.put("a", 1); m.keySet().remove("a"); System.out.print(m.size() + " ")…