ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
Set<Short> s = new HashSet<>();
for (short i = 0; i < 5; i++) {
s.add(i);
s.remove(i - 1);
}
System.out.print(s.size());- A. 1
- B. 5
- C. 0
- D. 4
Correct answer: B. 5
i - 1 is an int expression, so remove receives an autoboxed Integer. An Integer is never equal to a Short, so nothing is ever removed.
More OOP with C++ and Java questions
- What is printed? java Map<Long, String> m = new HashMap<>(); m.put(1L, "one"); System.out.print(m.get(1) + " " + m.get(1L) + " " +…
- What is printed? java List<Character> l = new ArrayList<>(List.of('a', 'b')); System.out.print(l.contains('a') + " " + l.contains(97) + "…
- What is printed? java Function<Integer, Integer> f = x -> x + 1; Function<Integer, Integer> g = x -> x 2…
- What is printed? java List<String> l = new ArrayList<>(List.of("ccc", "ee", "a", "bb", "d"))…
- What is printed? java int[] c = {0}; Runnable r = () -> c[0]++; r.run(); r.run(); System.out.print(c[0]);
- What is printed? java List<Integer> a = new ArrayList<>(List.of(1, 2)); List<Integer> b = new LinkedList<>(List.of(1, 2))…