ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
record Pt(int x, int y) { }
// in main:
Set<Pt> s = new HashSet<>();
s.add(new Pt(1, 2));
s.add(new Pt(1, 2));
s.add(new Pt(2, 1));
System.out.print(s.size() + " " + new Pt(1, 2).equals(new Pt(1, 2)));- A. 3 false
- B. 2 false
- C. 3 true
- D. 2 true
Correct answer: D. 2 true
A record gets equals and hashCode derived from all of its components, so the two Pt(1, 2) objects are equal and the set holds 2 elements. Pt(2, 1) differs in component order.
More OOP with C++ and Java questions
- Which line fails to compile? java class Animal { Animal make() { return new Animal(); } } class Dog extends Animal { Dog make() { return…
- What is printed? java PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder()); pq.addAll(List.of(3, 1, 4, 1, 5))…
- A Printer interface declares print(), scan(), fax() and staple(). A cheap printer class must implement all four and throws…
- What is printed? java String a = "Hi"; String b = new String("Hi"); String c = b.intern(); StringBuilder d = new StringBuilder("Hi")…
- What is printed? java abstract class Shape { Shape() { System.out.print(area() + " "); } abstract double area(); } class Sq extends Shape…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(5, 6, 7)); Iterator<Integer> it = l.iterator(); while (it.hasNext()) if…