Quizzes
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
equals method
:
Content language: English
Русский
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.
equals method
Which of the following statements are true given the following code: String a = new String("hello"); String b = new String(a); String c = a; char[] d = { 'h', 'e', 'l', 'l', 'o' };
equals method
Select all the statements that are correct regarding the following code: public class TestClazz { public static void main(String[] args) { final long Byte = 0; // 1 if ( Byte.equals(0) ) { // 2 System.out.print("=="); } else { System.out.print("!="); } } }
equals method
What will be the output of the following code? class Main { public static void main (String[] args) { String str = "hello"; StringBuffer sb = new StringBuffer(str); System.out.print(sb.equals(str) + "," + str.equals(sb)); } }
equals method
What will be displayed ? public class Tst { public static class Foo { private final Integer x; public Foo(Integer x) { this.x = x; } public boolean equals(Foo f) { return f.x.equals(this.x); } public int hashCode() { return x.intValue(); } } public static void main(String[] args) { final Foo f1 = new Foo(1); final Foo f2 = new Foo(1); final Set<Foo> set = new HashSet<Foo>(); set.add(f1); set.add(f2); System.out.println(set.size()); } }
equals method
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 } }
equals method
What will be the output of the following program? class Person { public String name; public int age; public Person(String name, int age) { this.name = name; this.age = age; } public boolean equals(Object obj) { return obj instanceof Person && this.age == ((Person)obj).age && this.name.equals(((Person)obj).name); } } public class Quizful { public static void main(String[] args){ Person p1 = new Person(null, 10); Person p2 = new Person("Alex", 22); System.out.println(p1.equals(p2)); } }
equals method
What will be written to the console by the following code: 01: public class Clazz { 02: public static void main(String[] args) { 03: Integer integer = new Integer(1024); 04: Long aLong = new Long(1024); 05: if (integer.equals(aLong)) 06: System.out.println("eq"); 07: if (integer.intValue() == aLong.longValue()) 08: System.out.println("=="); 09: } 10: }
equals method
What will be the result of compilation and execution of the following code: class User { private int id = 0; private String name; private String surname; public User () {} public User (int id, String name, String surname) { this.id = id; this.name = name; this.surname = surname; } Override public boolean equals (Object obj) { if (this == obj) return true; if (obj == null ||! getClass (). equals (obj.getClass ())) return false; User u = (User) obj; return id == u.id & Amp; & amp; name == u.name || (name! = null & amp; & amp; name.equals (u.name)) & Amp; & amp; surname == u.surname || (surname! = null & amp; & amp; surname.equals (u.surname)); } Override public int hashCode () { return id + name == null? 0: name.hashCode () + surname == null? 0: surname.hashCode (); } public static void main (String args []) { User u1 = new User (12, "name", null); User u2 = new User (12, null, "name"); System.out.println (u1.hashCode () == u2.hashCode ()); System.out.println (u1.equals (u2)); } } </ Code> </ pre>
equals method
What is the result of compilation and execution of the following code? enum Fish { GOLDFISH, ANGELFISH, GUPPY; } public class EnumTest2{ public static void main(String[] args){ Fish f = Fish.valueOf("GUPPY"); if (f == Fish.GUPPY) System.out.println("Are equal"); if (f.equals(Fish.GUPPY)) System.out.println("Are equal"); } }
equals method
← Prev
1
2
3
Next →
Sign Up Now
or
Subscribe for future quizzes