ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Stream<Integer> s = Stream.of(1, 2, 3); System.out.print(s.count()); System.out.print(s.count());
- A. 33
- B. 30
- C. IllegalStateException before anything is printed
- D. 3 is printed, then IllegalStateException is thrown
Correct answer: D. 3 is printed, then IllegalStateException is thrown
A stream can be consumed only once; after a terminal operation it is closed and any further operation throws IllegalStateException.
More OOP with C++ and Java questions
- What happens when this method is compiled (Java 7 or later)? java static void f() throws java.io.IOException { try { if (Math.random() <…
- What is printed? java try { System.out.print("T"); System.exit(0); } finally { System.out.print("F"); }
- What is printed? java class N { public Object clone() throws CloneNotSupportedException { return super.clone(); } } // in main: try { new…
- What is printed? java System.out.print((List.of(1, 2).hashCode() == List.of(2, 1).hashCode()) + " " + (Set.of(1, 2).hashCode() ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Object[] a) {…
- What is printed? java class Range implements Iterable<Integer> { public Iterator<Integer> iterator() { return List.of(1, 2, 3).iterator()…