Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Medium
Question 25
Given code of Test.java file:
- package com.udayankhattry.oca;
- import java.util.ArrayList;
- import java.util.List;
- public class Test {
- public static void main(String[] args) {
- List<StringBuilder> list = new ArrayList<>();
- list.add(new StringBuilder("AAA")); //Line n1
- list.add(new StringBuilder("BBB")); //Line n2
- list.add(new StringBuilder("AAA")); //Line n3
- list.removeIf(sb -> sb.equals(new StringBuilder("AAA"))); //Line n4
- System.out.println(list);
- }
- }
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.
