Saturday, December 29, 2012

OCJP Tutorials - 3


Given:
1. public class Score implements Comparable<Score> {
2. private int wins, losses;
3. public Score(int w, int l) { wins = w; losses = l; }
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){/*more 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

*******************************************************************************


Given:
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19. }

Which statement is true?

A. The time to find the value from HashMap with a Person key depends on the size of the map.
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first Person object to be removed
as a duplicate.
D. The time to determine whether a Person object is contained in a HashSet is constant and does
NOT depend on the size of the map.


Ans: A

******************************************************************************


Given:
5. import java.util.*;
6. public class SortOf {
7. public static void main(String[] args) {
8. ArrayList<Integer> a = new ArrayList<Integer>();
9. a.add(1); a.add(5); a.add(3);
11. Collections.sort(a);
12. a.add(2);
13. Collections.reverse(a);
14. System.out.println(a);
15. }
16. }

What is the result?

A. [1, 2, 3, 5]
B. [2, 1, 3, 5]
C. [2, 5, 3, 1]
D. [5, 3, 2, 1]
E. [1, 3, 5, 2]
F. Compilation fails.
G. An exception is thrown at runtime.

Answer: C

********************************************************************************


Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. } Which three are valid on line
12?
(Choose three.)
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected

Answer: A, B, D

*********************************************************************************


Which two code fragments correctly create and initialize a static array of int elements? (Choose
two.)


A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }

Answer: A, B




1 comment: