ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
Set<String> s = new HashSet<>();
System.out.print(s.add("a") + " " + s.add("a") + " " + s.remove("b") + " " + s.size());- A. true false false 1
- B. true true false 2
- C. true false true 1
- D. false false false 1
Correct answer: A. true false false 1
add returns whether the set changed (false for a duplicate) and remove returns whether the element was present.
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = Map.of("a", 1, "a", 2); System.out.print(m);
- What is printed? java interface A { default String hi() { return "A"; } } interface B extends A { default String hi() { return "B"; } }…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Number n) {…
- A base type must hold common fields, have a constructor that validates them, and share some method bodies, while subclasses form a strict…
- java.io.BufferedReader wraps another Reader, is itself a Reader, and adds buffering on top of it; such wrappers can be stacked at run…
- Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any…