ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
// Java 16 or later
record R(int[] a) { }
record S(List<Integer> l) { }
// in main:
System.out.print(new R(new int[]{1}).equals(new R(new int[]{1})) + " "
+ new S(List.of(1)).equals(new S(List.of(1))));- A. false true
- B. true true
- C. false false
- D. true false
Correct answer: A. false true
A record's generated equals compares each component with that component's own equals. Arrays inherit identity equals from Object, so two different arrays differ; Lists compare by content.
More OOP with C++ and Java questions
- What is printed? java enum Op { ADD { int apply(int a, int b) { return a + b; } }, MUL { int apply(int a, int b) { return a b; } }…
- What is printed? java enum Day { MON, TUE, WED } // in main: Map<Day, Integer> m = new EnumMap<>(Day.class); m.put(Day.WED, 3)…
- Which line fails to compile? java List<String>[] a = new List<String>[2]; // L1 List<?>[] b = new List<?>[2]; // L2 List<String>[] c = new…
- Which line fails to compile? java class Box<T extends Number> { double d(T t) { return t.doubleValue(); } } // in main: Box<Integer> a =…
- Which class compiles? java class K1 { static { throw new RuntimeException(); } } class K2 { static { if (true) throw new…
- What happens? java class W { { if (true) throw new java.io.IOException("io"); } W() throws java.io.IOException { } }