Flipkart Interview Question for Software Engineer / Developers






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

Good implementation of equals method should have o.getClass().getName().equals(this.getClass().getName())
equals should be reflexive, symmetric and transitive.
In case you add property to Student2 you'll break symmetric of relation so that student2.equals(student1) will be false and student1.equals(student2) will be true;

- Vitaly.Arbuzov January 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. This is what was expected by interviewer. He wanted to focus on the fact that we should use classname comparison instead of instanceof comparison while comparing objects in equals.

- prit January 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right prit...ideally it shud be on the basis of class name not on the basis of instanceOf ...but most of the java standard books suggest only instanceOf thing..

- sauravmanit March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

s1.equals(s2) false

- vvg January 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming you meant
Student s2 = new Student2("firstName", "lastName", 23 true);

s1.equals(s2) will return true. Generally speaking, if some value, in this case, last name, is part of the identity of an object, it should not be added via inheritance.

import java.lang.*;

public class TestEquals {
    static class Student {
        protected String firstName;
        Student(String aFirstName) {
            firstName = aFirstName;
        }
        public String getFirstName() {
            return firstName;
        }
        public boolean equals(Student s) {
            return firstName.equals(s.getFirstName());
        }
    }
    static class Student2 extends Student {
        protected String lastName;
        Student2(String aFirstName, String aLastName) {
            super(aFirstName);
            lastName = aLastName;
        }
    }
    public static void main(String args[]) {
        System.out.println("Testing equals...");
        Student s1 = new Student("First");
        Student s2 = new Student2("First", "Last");
        if (s1.equals(s2)) {
            System.out.println("Students are equal");
        } else {
            System.out.println("Students are not equal");
        }
    }
}

- Kevin January 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it really depends on how the equals() is implemented. as the implementation above, then s1.equals(s2) is true.
if it is implemented as follow:

bool equals(Student s)
{
    if (s == this)
       return true;
    else
       return false;
}

in this case s1.equals(s2) is false.

- waterfall.zhang January 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The correct implementation should be resulting as follows I guess....
if s1.equals(s2) is asked it should return true
Since all the fields in s1 are there in s2
but at the same time
if s2.equals(s1) is asked it should return false.
Since s2 has an extra field "LastName"

- pyagnambh January 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No.. Objects of two different class should never be equal though their fields can be equal.

- prit January 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

s1.equals(s2)
calls the equals method of the super class

s2.equals(s1)
also calls equals method of the super class

thus both return true

Ideally equals implementation for Student should be like this

public boolean equals( Student a) {
      if(this.getClass().getName().equals(a.getClass().getName())) {
      if(this.firstname.equals(a.firstname)) {
        if(this.isboy == a.isboy) {
          if(this.age == a.age) {
            return true;
          }
        }
      }
      }
      return false;
    }

then for both the above statements it will return false

- Meg March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it uses default implementation of equals() method, it will return false. Verified on a compiler.

- Anonymous August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you post me the complete working solution of this question.

- Deepak August 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you post me the complete working solution of this question.

- dum August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

freinds its not even to be thought of
s1.equals(s2);
returns true only if both are pointing to same object
rest in no case ...it will return a true..
hence
answer is false....

- shreyans June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

freinds its not even to be thought of
s1.equals(s2);
returns true only if both are pointing to same object
rest in no case ...it will return a true..
hence
answer is false....

- shreyans June 25, 2012 | 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