ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
List<Integer> e = Collections.emptyList(); System.out.print(e.size() + " "); e.add(1);
- A. 0 then [1] is stored
- B. "0 " is printed, then UnsupportedOperationException is thrown
- C. Compile-time error: emptyList cannot be assigned to List<Integer>
- D. "0 " is printed, then NullPointerException is thrown
Correct answer: B. "0 " is printed, then UnsupportedOperationException is thrown
Collections.emptyList() returns a shared immutable empty list; any modification throws UnsupportedOperationException.
More OOP with C++ and Java questions
- What is printed? java System.out.print(String.valueOf((Object) null) + " "); System.out.print(String.valueOf(null));
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(Integer a…
- What is printed? java // static methods of the main class: static void f(double d) { System.out.print("D"); } static void f(Object o) {…
- Which line fails to compile? java // static methods of the main class: static void g(long l) { } static void h(Long l) { } // in main…
- Which lines fail to compile? java String s = "x"; Number n = 1; Runnable r = () -> { }; Object o = s; boolean a = s instanceof Integer; //…
- What is printed? java // Java 16 or later record R(int[] a) { } record S(List<Integer> l) { } // in main: System.out.print(new R(new…