Question 12
Back to Exercise
What is the output of the following Java program?
import java.util.Objects;
public class Main {
public static void main(String[] args) {
System.out.println("A".hashCode() + " " + "AB".hashCode() + " " + Objects.hash(1, 2) + " " + Objects.hash() + " " + Integer.valueOf(77).hashCode() + " " + Boolean.TRUE.hashCode());
}
}
- A 65 2081 994 1 77 1231 Correct
- B 65 131 3 0 77 1
- C 65 2081 994 0 77 1231
- D 65 2080 993 1 77 1
Correct Answer: Option A
String hash: 65 × 31 + 66 = 2081. Objects.hash starts at 1: (31 + 1) × 31 + 2 = 994, and with no arguments it is 1. Integer's hash is its value; Boolean.TRUE's is 1231.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!