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"));- A. 3 false
- B. 2 false
- C. 2 true
- 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
- What is printed? java try { System.out.print("T"); throw new RuntimeException(); } catch (Exception e) { System.out.print("C"); } finally…
- Why must a C++ copy constructor take its parameter by reference (A(const A &other)) rather than by value?
- What is printed? java System.out.print("placement".substring(2, 5) + "placement".indexOf('e'));
- What is the time complexity of get(i) on a Java ArrayList and on a LinkedList?
- In C++, what does new do that malloc does not?
- What is printed? java class A { void show() { System.out.print("A"); } } class B extends A { void show() { super.show()…