nerdexam
Oracle

1Z0-851 · Question #167

Given: import java.util.*; public class WrappedString { private String s; public WrappedString(String s) { this.s = s; } public static void main(String[] args) { HashSet<Object> hs = new…

The correct answer is D. 3. See the full explanation below for the reasoning.

Question

Given: import java.util.*; public class WrappedString { private String s; public WrappedString(String s) { this.s = s; } public static void main(String[] args) { HashSet<Object> hs = new HashSet<Object>(); WrappedString ws1 = new WrappedString("aardvark"); WrappedString ws2 = new WrappedString("aardvark"); String s1 = new String("aardvark"); String s2 = new String("aardvark"); hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2); System.out.println(hs.size()); } } What is the result?

Options

  • A0
  • B1
  • C2
  • D3
  • E4
  • FCompilation fails.
  • GAn exception is thrown at runtime.

How the community answered

(37 responses)
  • B
    11% (4)
  • D
    81% (30)
  • E
    5% (2)
  • G
    3% (1)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice