ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
System.out.print((List.of(1, 2).hashCode() == List.of(2, 1).hashCode()) + " "
+ (Set.of(1, 2).hashCode() == Set.of(2, 1).hashCode()));- A. false true
- B. true true
- C. false false
- D. true false
Correct answer: A. false true
List.hashCode is specified as an order-sensitive polynomial (31·h + element), so [1, 2] (994) and [2, 1] (1024) differ. Set.hashCode is the SUM of element hashes, independent of order, consistent with set equality.
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Object[] a) {…
- What is printed? java class Range implements Iterable<Integer> { public Iterator<Integer> iterator() { return List.of(1, 2, 3).iterator()…
- Which line fails to compile (C++17)? cpp struct A { A() = default; A(const A&) = delete; A(A&&) = default; }; A make() { A a; return a; }…
- What is printed? cpp int main() { const string s = "x"; string t = std::move(s); cout << s << t; }
- Which line fails to compile? cpp int main() { int x = 1; const int& r = x; auto a = r; // L1 a++; // L2 decltype(r) b = x; // L3 b++; //…
- Which line fails to compile? cpp struct A { virtual ~A() { } }; struct B { virtual ~B() { } }; struct C : A, B { }; int main() { A a = new…