LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Predict the Output · Question 12

Hash codes of strings and Objects.hash Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links