Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/inserting-into-a-non-generic-treeset-in-java
Question 211 / 297:  What is the result of compiling and running the following code?
import java.util.*;

public class Test {

public static void main(String[] args) {

TreeSet s = new TreeSet();

s.add(1);

s.add(99.9);

s.add(99.9);

s.add(96.9);

for (int i = 0; i < s.size(); i++) {

System.out.print(s.pollFirst()+" ");

}

}

}

A  1 96.9 99.9
B  1 96.9 99.9 99.9
C  1
D  compilation error
E  an exception is thrown at run time
<< First < Previous Next > Last >>
Explanation:

The compiler will not complain, since TreeSet "s" is a non generic collection , any type is allowed to be inserted. TreeSet sorts its objects while inserting as long they are comparable to each other but since they are not, a ClassCastException is thrown at run time.

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Nicole Bartels
Start Quiz
Madison Christian
Start Test
Madison Christian
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>