ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
System.out.print((Double.NaN == Double.NaN) + " "
+ Double.valueOf(Double.NaN).equals(Double.NaN) + " "
+ (0.0 == -0.0) + " "
+ Double.valueOf(0.0).equals(-0.0));- A. false false true true
- B. true true true true
- C. false true false false
- D. false true true false
Correct answer: D. false true true false
Primitive == follows IEEE 754 (NaN ≠ NaN, 0.0 = −0.0). Double.equals compares bit patterns instead, so NaN equals itself and 0.0 differs from −0.0, which keeps equals consistent for hash-based collections.
More OOP with C++ and Java questions
- A class overrides equals correctly, but its hashCode() returns 42 for every object. What happens when many such objects are stored in a…
- What happens? java class P { int v; P(int v) { this.v = v; } } // in main: Set<P> s = new TreeSet<>(); s.add(new P(1)); s.add(new P(2))…
- Which constructors cause compile-time errors? java class F { final int x; F() { } // constructor 1 F(int v) { x = v; x = v + 1; } //…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.compute("a", (k, v) -> v == null ? 1 : v + 1); m.compute("a", (k, v) ->…
- What is printed? java Map<String, String> h = new HashMap<>(); h.put("k", null); System.out.print(h.size() + " "); Map<String, String> c =…
- What is printed? java List<Integer> e = Collections.emptyList(); System.out.print(e.size() + " "); e.add(1);