Tuesday, 6 September 2011

SCJP Questions 171-180


                                     PREVIOUS                       NEXT

Question 171
Given:
1. public class Person {
2. private String name;
3. public Person(String name) { this.name = name; }
4. public boolean equals(Person p) {
5. return p.name.equals(this.name);
6. }
7. }
Which is true?
A. The equals method does NOT properly override the Object.equals
method.
B. Compilation fails because the private attribute p.name cannot be
accessed in line 5.
C. To work correctly with hash-based data structures, this class must
also implement the hashCode method.
D. When adding Person objects to a java.util.Set collection, the equals
method in line 4 will prevent duplicates.
Answer: A

Question 172
Which two statements are true about the hashCode method? (Choose
two.)
A. The hashCode method for a given class can be used to test for
object equality and object inequality for that class.
B. The hashCode method is used by the java.util.SortedSet collection
class to order the elements within that set.
C. The hashCode method for a given class can be used to test for
object inequality, but NOT object equality, for that class.
D. The only important characteristic of the values returned by a
hashCode method is that the distribution of values must follow a
Gaussian distribution.
E. The hashCode method is used by the java.util.HashSet collection
class to group the elements within that set into hash buckets for
swift retrieval.
Answer: CE

Question 173
Given:
enum Example { ONE, TWO, THREE }
Which is true?
A. The expressions (ONE == ONE) and ONE.equals(ONE) are both
guaranteed to be true.
B. The expression (ONE < TWO) is guaranteed to be true and
ONE.compareTo(TWO) is guaranteed to be less than one.
C. The Example values cannot be used in a raw java.util.HashMap;
instead, the programmer must use a java.util.EnumMap.
D. The Example values can be used in a java.util.SortedSet, but the
set will NOT be sorted because enumerated types do NOT implement
java.lang.Comparable.
Answer: A

Question 174
Click the Exhibit button.
1. import java.util.*;
2. class KeyMaster {
3. public int i;
4. public KeyMaster(int i) { this.i = i; }
5. public boolean equals(Object o) { return i == ((KeyMaster)o).i; }
6. public int hashCode() { return i; }
7. }
8. public class MapIt {
9. public static void main(String[] args) {
10. Set<KeyMaster> set = new HashSet<KeyMaster>();
11. KeyMaster k1 = new KeyMaster(1);
12. KeyMaster k2 = new KeyMaster(2);
13. set.add(k1); set.add(k1);
14. set.add(k2); set.add(k2);
15. System.out.print(set.size() + “:”);
16. k2.i = 1;
17. System.out.print(set.size() + “:”);
18. set.remove(k1);
19. System.out.print(set.size() + “:”);
20. set.remove(k2);
21. System.out.print(set.size());
22. }
23. }
What is the result?
A. 4:4:2:2
B. 4:4:3:2
C. 2:2:1:0
D. 2:2:0:0
E. 2:1:0:0
F. 2:2:1:1
G. 4:3:2:1
Answer: F

Question 175
Given:
1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. List<String> strings = new ArrayList<String>();
5. // insert code here
6. }
7. }
Which four, inserted at line 5, will allow compilation to succeed?
(Choose four.)
A. String s = strings.get(0);
B. Iterator i1 = strings.iterator();
C. String[] array1 = strings.toArray();
D. Iterator<String> i2 = strings.iterator();
E. String[] array2 = strings.toArray(new String[1]);
F. Iterator<String> i3 = strings.iterator<String>();
Answer: ABDE

Question 176
Given:
1. import java.util.*;
2. public class Old {
3. public static Object get()(List list) {
4. return list.get(0);
5. }
6. }
Which three will compile successfully? (Choose three.)
A. Object o = Old.get0(new LinkedList());
B. Object o = Old.get0(new LinkedList<?>());
C. String s = Old.getfl(new LinkedList<String>());
D. Object o = Old.get0(new LinkedList<Object>());
E. String s = (String)Old.get0(new LinkedList<String>());
Answer: ADE

Question 177
Given:
11. public static void append(List list) { list.add(”0042”); }
12. public static void main(String[] args) {
13. List<Integer> intList = new ArrayList<Integer>();
14. append(intList);
15. System.out.println(intList.get(0));
16. }
‘What is the result?
A. 42
B. 0042
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
E. Compilation fails because of an error in line 14.
Answer: B

Question 178
Given a pre-generics implementation of a method:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
Which three changes must be made to the method sum to use
generics? (Choose three.)
A. remove line 14
B. replace line 14 with “int i = iter.next();”
C. replace line 13 with “for (int i : intList) {“
D. replace line 13 with “for (Iterator iter : intList) {“
E. replace the method declaration with “sum(List<int> intList)”
F. replace the method declaration with “sum(List<Integer> intList)”
Answer: ACF

Question 179
Given:
classA {}
class B extends A {}
class C extends A {}
class D extends B {}
Which three statements are true? (Choose three.)
A. The type List<A> is assignable to List.
B. The type List<B> is assignable to List<A>.
C. The type List<Object> is assignable to List<?>.
D. The type List<D> is assignable to List<? extends B>.
E. The type List<? extends A> is assignable to List<A>.
F. The type List<Object> is assignable to any List reference.
G. The type List<? extends B> is assignable to List<? extends A>.
Answer: CDG

Question 180
Given:
11. public void addStrings(List list) {
12. list.add(”foo”);
13. list.add(”bar”);
14. }
What must you change in this method to compile without warnings?
A. add this code after line 11:
list = (List<String>) list;
B. change lines 12 and 13 to:
list.add<String>(”foo”);
list.add<String>(”bar”);
C. change the method signature on line 11 to:
public void addStrings(List<? extends String> list) {
D. change the method signature on line 11 to:
public void addStrings(List<? super String> list) {
E. No changes are necessary. This method compiles without warnings.
Answer: D
                       
                                             PREVIOUS                       NEXT

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...