Tuesday 6 September 2011

SCJP Questions 181-190


                                   PREVIOUS                       NEXT

Question 181
Given:
1. public class Test {
2. public <T extends Comparable> T findLarger(T x, T y) {
3. if(x.compareTo(y) > 0) {
4. return x;
5. } else {
6. return y;
7. }
8. }
9. }
and:
22. Test t = new Test();
23. // insert code here
Which two will compile without errors when inserted at line 23?
(Choose two.)
A. Object x = t.findLarger(123, “456”);
B. int x = t.findLarger(123, new Double(456));
C. int x = t.findLarger(123, new Integer(456));
D. int x = (int) t.findLarger(new Double(123), new Double(456));
Answer: AC

Question 182
Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() <min.doubleValue())
17. min = added;
18. if (max == null ||added.doubleValue() > max.doubleValue())
19. max = added;
20. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose
two.)
A. public class MinMax<?> {
B. public class MinMax<? extends Number> {
C. public class MinMax<N extends Object> {
D. public class MinMax<N extends Number> {
E. public class MinMax<? extends Object> {
F. public class MinMax<N extends Integer> {
Answer: DF

Question 183
A programmer must create a generic class MinMax and the type
parameter of MinMax must implement Comparable. Which
implementation of MinMax will compile?
A. class MinMax<E extends Comparable<E>> {
E min=null;
E max=null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
B. class MinMax<E implements Comparable<E>> {
E min=null;
E max=null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
C. class MinMax<E extends Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() { }
public <E> void put(E value) { /* store min or max */ }
}
D. class MinMax<E implements Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() { }
public <E> void put(E value) { /* store min or max */ }
}
Answer: A

Question 184
Given:
1. public class Drink implements Comparable {
2. public String name;
3. public int compareTo(Object o) {
4. return 0;
5. }
6. }
and:
20. Drink one = new Drink();
21. Drink two = new Drink();
22. one.name= “Coffee”;
23. two.name= “Tea”;
23. TreeSet set = new TreeSet();
24. set.add(one);
25. set.add(two);
A programmer iterates over the TreeSet and prints the name of each
Drink object.
What is the result?
A. Tea
B. Coffee
C. Coffee
Tea
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: B

Question 185
Given:
11. List list = // more code here
12. Collections.sort(list, new MyComparator());
Which code will sort this list in the opposite order of the sort in line
12?
A. Collections.reverseSort(list, new MyComparator());
B. Collections.sort(list, new MyComparator());
list.reverse();
C. Collections.sort(list, new InverseComparator(
new MyComparator()));
D. Collections.sort(list, Collections.reverseOrder(
new MyComparator()));
Answer: D

Question 186
Given:
int[] myArray=newint[] {1, 2,3,4, 5};
What allows you to create a list from this array?
A. List myList = myArray.asList();
B. List myList = Arrays.asList(myArray);
C. List myList = new ArrayList(myArray);
D. List myList = Collections.fromArray(myArray);
Answer: B

Question 187
Given:
13. public static void search(List<String> list) {
14. list.clear();
15. list.add(”b”);
16. list.add(”a”);
17. list.add(”c”);
18. System.out.println(Collections.binarySearch(list, “a”));
19. }
What is the result of calling search with a valid List implementation?
A. 0
B. 1
C. 2
D. a
E. b
F. c
G. The result is undefined.
Answer: G

Question 188
Given:
1. import java.util.*;
2.
3. public class LetterASort {
4. public static void main(String[] args) {
5. ArrayList<String> strings = new ArrayList<String>();
6. strings.add(’aAaA”);
7. strings.add(”AaA”);
8. strings.add(’aAa”);
9. strings.add(”AAaa”);
10. Collections.sort(strings);
11. for (String s: strings) { System.out.print(s + “ “); }
12. }
13. }
What is the result?
A. Compilation fails.
B. aAaA aAa AAaa AaA
C. AAaa AaA aAa aAaA
D. AaA AAaa aAaA aAa
E. aAa AaA aAaA AAaa
F. An exception is thrown at runtime.
Answer: C

Question 189
Given:
ArrayList a = new ArrayList();
containing the values {“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”}
Which code will return 2?
A. Collections. sort(a, a.reverse());
int result = Collections.binarySearch(a, “6”);
B. Comparator c = Collections.reverseOrder();
Collections.sort(a, c);
int result = Collections.binarySearch(a, “6”);
C. Comparator c = Collections.reverseOrder();
Collections.sort(a, c);
int result = Collections.binarySearch(a, “6”,c);
D. Comparator c = Collections.reverseOrder(a);
Collections.sort(a, c);
int result = Collections.binarySearch(a, “6”,c);
E. Comparator c = new InverseComparator(new Comparator());
Collections.sort(a);
int result = Collections.binarySearch(a, “6”,c);
Answer: C

Question 190
Given:
34. HashMap props = new HashMap();
35. props.put(”key45”, “some value”);
36. props.put(”key12”, “some other value”);
37. props.put(”key39”, “yet another value”);
38. Set s = props.keySet();
39. // insert code here
What, inserted at line 39, will sort the keys in the props HashMap?
A. Arrays.sort(s);
B. s = new TreeSet(s);
C. Collections.sort(s);
D. s = new SortedSet(s);
Answer: B

                                      PREVIOUS                       NEXT

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...