Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Medium

Question 51

Consider below code:

What will be the result of compiling and executing Test class?

  • A

    Student[James, 27]
    Student[James, 25]
    Student[James, 25]

  • B

    Student[James, 27]

  • C

    Student[James, 25]
    Student[James, 27]
    Student[James, 25]
    Student[James, 25]

  • D

    Student[James, 25]
    Student[James, 27]
    Student[James, 25]

Reveal correct answer

Correct answer: C

Explanation

Before you answer this, you must know that there are 5 different Student object created in the memory (4 at the time of adding to the list and 1 at the time of removing from the list). This means these 5 Student objects will be stored at different memory addresses.


remove(Object) method removes the first occurrence of matching object and equals(Object) method decides whether 2 objects are equal or not. equals(Object) method has NOT been overridden by the Student class. In fact, equals(Student) is overloaded. But overloaded version is not invoked while equating the Student objects.


equals(Object) method defined in Object class is invoked and equals(Object) method defined in Object class uses == operator to check the equality and in this case as all the Student objects are stored at different memory location, hence not equal.


Nothing is removed from the students list, all the 4 Student objects are printed in the insertion order.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need