ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
TreeSet<Integer> s = new TreeSet<>(List.of(5, 1, 9, 3, 7));
System.out.print(s.headSet(5) + " " + s.tailSet(5) + " "
+ s.descendingSet().first() + " " + s.pollFirst() + " " + s);- A. [1, 3] [5, 7, 9] 9 1 [3, 5, 7, 9]
- B. [1, 3, 5] [5, 7, 9] 9 1 [3, 5, 7, 9]
- C. [1, 3] [5, 7, 9] 1 1 [3, 5, 7, 9]
- D. [1, 3] [5, 7, 9] 9 1 [1, 3, 5, 7, 9]
Correct answer: A. [1, 3] [5, 7, 9] 9 1 [3, 5, 7, 9]
headSet(to) excludes the bound, tailSet(from) includes it. descendingSet().first() is the largest element. pollFirst removes 1, and since + evaluates left to right the final s is printed after that removal.
More OOP with C++ and Java questions
- What is printed? java Set<List<Integer>> a = new HashSet<>(); a.add(List.of(1, 2)); a.add(new ArrayList<>(List.of(1, 2))); Set<int[]> b =…
- What is printed? java char[] c = {'h', 'i'}; System.out.print(c); System.out.print(" " + String.valueOf(c) + " " + c.length);
- What is printed? java // static methods of the main class: static void up(String s) { s = s.concat("X"); } static void add(StringBuilder…
- What is printed? java Object a = new ArrayList<String>(); List<Integer> b = new LinkedList<>(); List<Integer> c = new ArrayList<>()…
- What happens when this interface is compiled? java interface Named { String name(); default boolean equals(Object o) { return o instanceof…
- Which of these is a valid functional interface (usable as a lambda target)?