Java
Select all ...
Site Language: English
Українська
English
Русский
Programming Tests
Login
Sign Up
Programming Tests
Theory
Snippets
Papers
Landing
Android
Prices
FAQ
Cosmo Story
Terms and Conditions
Privacy Policy
Cookies Policy
Send Feedback
Select all correct statements regarding the concept of relationship between hashCode() and equals(Object o) methods
If you are comparing two objects equals method returns the value true, the value returned by hashCode () of these objects must match.
If you are comparing two objects equals method returns the value true, the value returned by hashCode () of these objects can not be the same.
If you are comparing two objects equals method returns the value false, the value returned by hashCode () of these objects must be different.
If you are comparing two objects equals method returns the value false, the value returned by hashCode () of these objects can be the same.
None of the statements above
Explanation
The key concept of the relationship methods hashCode () and equals (Object o) suggests the return of the same hash value in case of an equality of objects, but not the obligation to return a different value in the case of inequality objects.
equals method
hashcode
1
(1)
Like
Login in
to like
Comment
Login in
to comment
Share
Tweet
Related Content
In accordance with the contract, the hashCode() method should return the same integer value for two objects that are equal according to the equals() method. Is that true?
What will be the result of the following code execution? import java.util.*; class Employee { private String name; public Employee(String name) { this.name = name; } public String toString() { return name; } public boolean equals(Object obj) { HashCodeTest.count++; if (obj == null) return false; if (obj.getClass() != getClass()) return false; Employee emp = (Employee) obj; if (this.name == emp.name) { return true; } return false; } public int hashCode(){ return name.hashCode(); } } public class HashCodeTest { static int count = 0; public static void main(String[] args) { Map employees = new HashMap(); employees.put(new Employee("Joe"), new Integer("1")); employees.put(new Employee("Chandler"), new Integer("2")); employees.put(new Employee("Chandler"), new Integer("2")); employees.put(new Employee("Ross"), new Integer("3")); Iterator iterator = employees.keySet().iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println(count); } } P.S. For experts - hashtags of the lines "Joe", "Chandler" and "Ross" are different.
What will be printed after execution of this code? class User { private String name; public User( String name ) { this.name = name; } public boolean equals( Object obj ) { User user = (User) obj; return user.name.equals( name ); } public String toString() { return name; } } class Foo { public static void main(String...arguments) { //1 User user1 = new User( "John" ); User user2 = new User( "Bill" ); User user3 = new User( "John" ); Set<User> userSet = new HashSet<User>(); userSet.add( user1 ); userSet.add( user2 ); userSet.add( user3 ); //2 System.out.println( "Count of users: " + userSet.size() ); //3 } }
Which data structure that implements a Map interface uses an == operator, rather than the equals method for objects comparison?
What is the result of the following code execution? Integer i = 5000; System.out.println(i.hashCode());
J
ava
Quiz
Login to learn Java
or
Read more about
Java Quizzes
Follow CodeGalaxy
Mobile Beta
Send Feedback
Keep exploring
Java quizzes
What will be printed out as a result of the following code execution / compilation? class A { public A() { System.out.print("A"); } } class B { public B() { System.out.print("B"); } } class C { public C() { System.out.print("C"); } } public class D extends C { private A objA = new A(); private static B objB = new B(); public D() { System.out.print("D"); } public static void main(String[] args){ new D(); } }
What will be printed out as a result of the following code execution / compilation? public class Test { private String name; Test(String name) { this.name = name; } public void test(final Test test) { test = new Test("three"); } public String toString() { return name; } public static void main(String[] args) { Test t1 = new Test("one"); Test t2 = new Test("two"); t1.test(t2); System.out.println(t2); } }
What will be printed out as a result of the following code execution / compilation? public class Test { static void m(int ... a) { System.out.println("1"); } static void m(Integer ... a) { System.out.println("2"); } public static void main(String[] args) { m(1, 2); } }
What will be printed out as a result of the following code execution / compilation? public class Test { public static void main(String[] args) { ElectricInverter inverter = new ElectricInverter(); int AC = 220; System.out.println(inverter.invert(AC)); } } class ElectricInverter { public static final int AC = ~220; public static final int DC = -110; public static final int GROUND = 0; int invert(int type) { if (type == AC) { return DC; } else if (type == DC) { return AC; } return GROUND; } }
int i = Integer.MAX_VALUE + 10; What is the result of the given line execution?
What happens after the following code is compiled and run: 01: interface TheInterface { 02: void print(); 03: } 04: 05: class TheClass implements TheInterface { 06: public void print() { 07: System.out.println("TheClass"); 08: } 09: } 10: 11: public class ClassConversion { 12: public static void main(String[] args) { 13: TheClass c = new TheClass(); 14: TheInterface i = c; 15: i.print(); 16: } 17: }
Sign Up Now
or
Subscribe for future quizzes
Login in to like
Login in to comment