SCJP : NavigableSet Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public class Test {
public static void main(String... args) {
List lst = new ArrayList();
lst.add(34);
lst.add(6);
lst.add(6);
lst.add(6);
lst.add(6);
lst.add(5);
NavigableSet nvset = new TreeSet(lst);
System.out.println(nvset.tailSet(6));
}
}
options
A)Compile error : No method name like tailSet()
B)6 34
C)5
D)5 6 34
Correct answer is : B
Explanations : tailSet(6) Returns elements are greater than or equal to 6.
Questions no -2
What is the output for the below code ?
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class Test {
public static void main(String... args) {
List lst = new ArrayList();
lst.add(34);
lst.add(6);
lst.add(6);
lst.add(6);
lst.add(6);
NavigableSet nvset = new TreeSet(lst);
nvset.pollFirst();
nvset.pollLast();
System.out.println(nvset.size());
}
}
options
A)Compile error : No method name like pollFirst() or pollLast()
B)0
C)3
D)5
Correct answer is : B
Explanations :
pollFirst() Retrieves and removes the first (lowest) element, or returns null if this set is empty.
pollLast() Retrieves and removes the last (highest) element, or returns null if this set is empty.
Questions no -3
What is the output for the below code ?
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class Test {
public static void main(String... args) {
List lst = new ArrayList();
lst.add(34);
lst.add(6);
lst.add(2);
lst.add(8);
lst.add(7);
lst.add(10);
NavigableSet nvset = new TreeSet(lst);
System.out.println(nvset.lower(6)+" "+nvset.higher(6)+ " "+ nvset.lower(2));
}
}
options
A)1 2 7 10 34 null
B)2 7 null
C)2 7 34
D)1 2 7 10 34
Correct answer is : B
Explanations :
lower() Returns the greatest element in this set strictly less than the given element, or null if there is no such element.
higher() Returns the least element in this set strictly greater than the given element, or null if there is no such element.
Questions no -4
What is the output for the below code ?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class Test {
public static void main(String... args) {
List lst = new ArrayList();
lst.add(34);
lst.add(6);
lst.add(2);
lst.add(8);
lst.add(7);
lst.add(10);
NavigableSet nvset = new TreeSet(lst);
System.out.println(nvset.headSet(10));
}
}
options
A)Compile error : No method name like headSet()
B)2, 6, 7, 8, 10
C)2, 6, 7, 8
D)34
Correct answer is : C
Explanations :
headSet(10) Returns the elements elements are strictly less than 10.
headSet(10,false) Returns the elements elements are strictly less than 10.
headSet(10,true) Returns the elements elements are strictly less than or equal to 10.
|