1) Does a TreeSet allow duplicate values to be added to the Set? For example, wh
ID: 3826077 • Letter: 1
Question
1) Does a TreeSet allow duplicate values to be added to the Set?
For example, what happens if you try to execute the following code?
TreeSet myTree = new TreeSet();
myTree.add("alice");
myTree.add("alice");
a)This code is illegal and will cause a compile-time error message.
B)This code will not have any compiler error message, but when you execute this code the program will "crash" and throw an Exception and the program will halt.
c) This code is legal. The second (duplicate) add method will simply quietly ignore and discard the duplicate value. The value of myTree.size() will be 1 after this code is executed.
d) This code is legal. The TreeSet will have two items in the Set (i.e., myTree.size() will be equal to 2), with the identical values.
2) Does a TreeSet allow the null value to be added into the Set?
For example, what happens if you try to execute the following code?
TreeSet myTree = new TreeSet ();
myTree.add("alice");
myTree.add(null);
a)This code is illegal and will cause a compile-time error message.
b)This code will not have any compiler error message, but when you execute this code the program will "crash" and throw an Exception and the program will halt.
c)This code is legal. The second add method will simply quietly ignore and discard the null value. The value of myTree.size() will be 1 after this code is executed.
d)This code is legal. The TreeSet will have two items in the Set (i.e., myTree.size() will be equal to 2).
Explanation / Answer
Question 1
Answer:
c) This code is legal. The second (duplicate) add method will simply quietly ignore and discard the duplicate value. The value of myTree.size() will be 1 after this code is executed.
Question 2
Answer:
b)This code will not have any compiler error message, but when you execute this code the program will "crash" and throw an Exception and the program will halt.
It will throw an Exeption "NullPointerException"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.