ProgrammingModerateIBM · Deloitte pattern

What is printed?

What is printed?
Set<String> s = new HashSet<>();
s.add("a");
s.add("b");
s.add("a");
System.out.print(s.size() + " " + s.add("b"));
  1. A. 3 false
  2. B. 2 false
  3. C. 2 true
  4. D. 3 true

Correct answer: B. 2 false

A Set keeps no duplicates, so the second "a" is ignored and the size is 2. add returns false when the element is already present, as it is for "b".

More OOP with C++ and Java questions

Exams that ask this topic