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

Question 25

Given code of Test.java file:


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

  • A [BBB, AAA]
  • B [AAA, BBB, AAA]
  • C None of the other options
  • D []
  • E [BBB]
Reveal correct answer

Correct answer: B

Explanation

ArrayList instance referred by 'list' stores 3 StringBuilder instances.


removeIf(Predicate<? super E> filter) method was added as a default method in Collection<E> interface in JDK 8 and it removes all the elements of this collection that satisfy the given predicate.

StringBuilder class doesn't override equals(Object) method. So Object version is invoked, which uses == operator, hence sb.equals(new StringBuilder("AAA")) would return false as all 4 StringBuilder instances have been created at four different memory locations.

None of the StringBuilder instances are removed from the list.


StringBuilder class overrides toString() method, which returns the containing String and that is why [AAA, BBB, AAA] will be printed on to the console.

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