Interview Question for Software Trainees


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

You must override hashcode method, whenever you override equals method, as the best practice (though your program will get compiled & run, even without this). May be that's what the interviewer was expecting to hear back, as part of response to this question.

- ruharish October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

implement the comparable interface!

- EOF October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

comapre interface,comparator class, equals and hashcode methods also used for this purpose. Finally you can use == operator to compare references by identity.

- gstepanov@griddynamics.com October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

comapre interface,comparator class, equals and hashcode methods also used for this purpose. Finally you can use == operator to compare references by identity.

- gstepanov@griddynamics.com October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can override equals method to compare two objective

- leeang1214 October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

class A{
  int i;
  A(){
    this.i=0;
	}
  A(int i){
    this.i=i;
	}
	public String toString(){
	  return "i="+i;
	  }
	  }
	  
public class Manager1{
  public static void main(String [] args){
    A a1=new A(10);
	A a2=new A(10);
	A a3=a1;
	System.out.println(a1);
	System.out.println(a2);
	
	System.out.println(a1==a2);
	System.out.println(a1.i==a2.i);
	
	System.out.println(a1.equals(a2));
	System.out.println(a1.equals(a3));
	System.out.println((new A()).equals(new A()));
	}
	}

is it like that.???

- gautam October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can override equals method to compare two objective

- leeang1214 October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can implement Comparable or use a Comparator. Following is an example where we want to compare two accounts based on balance.

public class Account implements Comparable<Account>{
    @Override
    public int compareTo(Account account) {
        return (this.balance < account.balance ) ? -1: (this.balance > account.balance ) ? 1:0 ;
    }
}

public class AccountBalanceComparator implements Comparator<Account>{

    @Override
    public int compare(Account accountA, Account accountB) {
        
        return (accountA.getBalance() < accountB.getBalance() ) ? -1: (accountA.getBalance() > accountB.getBalance() ) ? 1:0 ;
    }
}

- Expressions October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sometimes usual equals() method may work. If the objects have tree sort of structure and we need to compare all fields in methods, then 'java reflection' may work.

- Prakhar December 29, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More