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);- A. 6 2 6
- B. 4 1 6
- C. 4 2 6
- 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
- What is printed? java StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1, 3); sb.insert(2, "XY"); sb.reverse()…
- What is printed? java String s = "b"; int r = 0; switch (s) { case "a": r += 1; case "b": r += 2; case "c": r += 4; break; default: r +=…
- What is printed? java class R implements AutoCloseable { public void close() { throw new RuntimeException("C"); } } // in main: try { try…
- Which line fails to compile? java void run() { try { int x = 1; } catch (RuntimeException e) { } // line 1 try { int y = 2; } catch…
- What is printed? java class P { static { System.out.print("1"); } { System.out.print("2"); } P() { System.out.print("3"); } } class C…
- Which line fails to compile? java int base = 10; int step = 1; step = 2; Function<Integer, Integer> f = x -> x + base; // line 1…