Tuesday 6 September 2011

SCJP Questions 161-170


                                  PREVIOUS                       NEXT

Question 161
Given:
10. interface Jumper { public void jump(); }
......
20. class Animal {}
......
30. class Dog extends Animal {
31. Tail tail;
32. }
......
40. class Beagle extends Dog implements Jumper {
41. public void jump() { }
42. }
.......
50. class Cat implements Jumper {
51. public void jump() { }
52. }
Which three are true? (Choose three.)
A. Cat is-a Animal
B. Cat is-a Jumper
C. Dog is-a Animal
D. Dog is-a Jumper
E. Cat has-a Animal
F. Beagle has-a Tail
G. Beagle has-a Jumper
Answer: BCF

Question 162
Given:
1. import java.util.*;
2. public class Example {
3. public static void main(String[] args) {
4. // insert code here
5. set.add(new integer(2));
6. set.add(new integer(l));
7. System.out.println(set);
8. }
9. }
Which code, inserted at line 4, guarantees that this program will
output [1, 2]?
A. Set set = new TreeSet();
B. Set set = new HashSet();
C. Set set = new SortedSet();
D. List set = new SortedList();
E. Set set = new LinkedHashSet();
Answer: A

Question 163
Given:
1. import java.util.*;
2. public class PQ {
3. public static void main(String[] args) {
4. PriorityQueue<String> pq = new PriorityQueue<String>();
5. pq.add(”carrot”);
6. pq.add(”apple”);
7. pq.add(”banana”);
8. System.out.println(pq.poll() +”:” + pq.peek());
9. }
10. }
What is the result?
A. apple:apple
B. carrot:apple
C. apple:banana
D. banana:apple
E. carrot:carrot
F. carrot:banana
Answer: C

Question 164
Given:
1. import java.util.*;
2. public class WrappedString {
3. private String s;
4. public WrappedString(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet<Object> hs = new HashSet<Object>();
7. WrappedString ws1 = new WrappedString(”aardvark”);
8. WrappedString ws2 = new WrappedString(”aardvark”);
9. String s1 = new String(”aardvark”);
10. String s2 = new String(”aardvark”);
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12. System.out.println(hs.size()); } }
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D

Question 165
Click the Exhibit button.
1. import java.util.*;
2. public class TestSet {
3. enum Example { ONE, TWO, THREE }
4. public static void main(String[] args) {
5. Collection coll = new ArrayList();
6. coll.add(Example.THREE);
7. coll.add(Example.THREE);
8. coll.add(Example.THREE);
9. coll.add(Example.TWO);
10. coll.add(Example.TWO);
11. coll.add(Example.ONE);
12. Set set = new HashSet(coll);
13. }
14. }
Which statement is true about the set variable on line 12?
A. The set variable contains all six elements from the coll collection,
and the order is guaranteed to be preserved.
B. The set variable contains only three elements from the coll
collection, and the order is guaranteed to be preserved.
C. The set variable contains all six elements from the coil collection,
but the order is NOT guaranteed to be preserved.
D. The set variable contains only three elements from the coil
collection, but the order is NOT guaranteed to be preserved.
Answer: D

Question 166
Given:
1. public class Score implements Comparable<Score> {
2. private int wins, losses;
3. public Score(int w, int 1) { wins = w; losses = 1; }
4. public int getWins() { return wins; }
5. public int getLosses() { return losses; }
6. public String toString() {
7. return “<“ + wins + “,“ + losses + “>”;
8. }
9. // insert code here
10. }
Which method will complete this class?
A. public int compareTo(Object o) {/*mode code here*/}
B. public int compareTo(Score other) {/*more code here*/}
C. public int compare(Score s1,Score s2){/*more code here*/}
D. public int compare(Object o1,Object o2){/*more code here*/}
Answer: B

Question 167
A programmer has an algorithm that requires a java.util.List that
provides an efficient implementation of add(0,object), but does
NOT need to support quick random access. What supports these
requirements?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D

Question 168
Given:
11. public class Person {
12. private String name, comment;
13. private int age;
14. public Person(String n, int a, String c) {
15. name = n; age = a; comment = c;
16. }
17. public boolean equals(Object o) {
18. if(! (o instanceof Person)) return false;
19, Person p = (Person)o;
20. return age == p.age && name.equals(p.name);
21. }
22. }
What is the appropriate definition of the hashCode method in class
Person?
A. return super.hashCode();
B. return name.hashCode() + age * 7;
C. return name.hashCode() + comment.hashCode() /2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;
Answer: B

Question 169
Given:
11. public class Key {
12. private long id1;
13. private long 1d2;
14.
15. // class Key methods
16. }
A programmer is developing a class Key, that will be used as a key in
a standard java.util.HashMap. Which two methods should be
overridden to assure that Key works correctly as a key? (Choose two.)
A. public int hashCode()
B. public boolean equals(Key k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(Key k)
Answer: AD

Question 170
Given:
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public boolean equals(Object o) {
17. if( !o instanceof Person ) return false;
18. Person p = (Person) o;
19. return p.name.equals(this.name);
20. }
21. }
Which is true?
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same
name.
C. All Person objects will have the same hash code because the
hashCode method is not overridden.
D. If a HashSet contains more than one Person object with
name=”Fred”, then removing another Person, also with name=”Fred”,
will remove them all.
Answer: B

                                         PREVIOUS                       NEXT

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...