ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Set<List<Integer>> a = new HashSet<>();
a.add(List.of(1, 2));
a.add(new ArrayList<>(List.of(1, 2)));
Set<int[]> b = new HashSet<>();
b.add(new int[]{1, 2});
b.add(new int[]{1, 2});
System.out.print(a.size() + " " + b.size());- A. 2 2
- B. 1 1
- C. 2 1
- D. 1 2
Correct answer: D. 1 2
All List implementations define equals/hashCode by content, so the immutable list and the ArrayList are equal. Arrays use identity equals and hashCode, so two separate arrays are distinct.
More OOP with C++ and Java questions
- 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)?
- What is printed? java class T { String name = "outer"; void run() { Runnable a = () -> System.out.print(this.name + " "); Runnable b = new…