ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
String s = "hello";
System.out.print("[" + s.substring(5) + "]");
System.out.print(s.substring(6));- A. "[o]" is printed, then StringIndexOutOfBoundsException is thrown
- B. "[]" is printed and nothing else
- C. "[]" is printed, then StringIndexOutOfBoundsException is thrown
- D. StringIndexOutOfBoundsException is thrown before anything is printed
Correct answer: C. "[]" is printed, then StringIndexOutOfBoundsException is thrown
substring(beginIndex) allows beginIndex == length() and returns the empty string; beginIndex 6 > length 5 is out of range and throws.
More OOP with C++ and Java questions
- What is printed? java Integer a = 1000; Integer b = a; a++; System.out.print(a + " " + b + " " + (a == b));
- What is printed? java class P { int id; P(int id) { this.id = id; } public boolean equals(P o) { return o != null && id == o.id; } } // in…
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi", "date"))…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…
- What is printed? java // static method of the main class: static void f(Object... a) { System.out.print(a == null ? "N " : a.length + "…
- What is printed? java interface Shape { double area(); } class Sq implements Shape { double area() { return 4; } } // in main: Shape s =…