1z0-851_formatted
Number
Passing Score
Time Limit
File Version
: 000-000
: 800
: 120 min
: 1.0
Java Standard Edition 6 Programmer Certified
Professional Exam
Practice Test
Version: 4.2
Oracle 1z0-851: Practice Exam
Exam A
QUESTION 1
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. }
What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose
three.)
这三个变化是什么让这个类可以使用泛型和避免未经检查的警告?(选择三。)
Remove line 14.
Replace line 14 with "int i = iter.next();".
Replace line 13 with "for (int i : intList) {".
Replace line 13 with "for (Iterator iter : intList) {".
Replace the method declaration with "sum(List intList)".
Replace the method declaration with "sum(List intList)".
A.
B.
C.
D.
E.
F.
Answer: ACF
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 2
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.
B.
C.
D.
java.util.Queue
java.util.ArrayList
java.util.LinearList
java.util.LinkedList
Answer: D
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 3
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.
B.
C.
D.
E.
F.
public class MinMax> {
public class MinMax extends Number> {
public class MinMax {
public class MinMax {
public class MinMax extends Object> {
public class MinMax {
Answer: DF
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 4
Given:
12. import java.util.*;
13. public class Explorer2 {
14. public static void main(String[] args) {
15. TreeSet s = new TreeSet();
16. TreeSet subs = new TreeSet();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. s.add(629);
21. System.out.println(s + " " + subs);
22. }
23. }
What is the result?
A.
B.
C.
D.
E.
F.
Compilation fails.
An exception is thrown at runtime.
[608, 610, 612, 629] [608, 610]
[608, 610, 612, 629] [608, 610, 629]
[606, 608, 610, 612, 629] [608, 610]
[606, 608, 610, 612, 629] [608, 610, 629]
Answer: E
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 5
Given:
1. public class Score implements Comparable {
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.
B.
C.
D.
public int compareTo(Object o){/*more code here*/}
public int compareTo(Score other){/*more code here*/}
public int compare(Score s1,Score s2){/*more code here*/}
public int compare(Object o1,Object o2){/*more code here*/}
Answer: B
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 6
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.
Answer: A
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 7
Given:
5. import java.util.*;
6. public class SortOf {
7. public static void main(String[] args) {
8. ArrayList a = new ArrayList();
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.
C.
D.
E.
F.
G.
[2, 1, 3, 5]
[2, 5, 3, 1]
[5, 3, 2, 1]
[1, 3, 5, 2]
Compilation fails.
An exception is thrown at runtime.
Answer: C
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 8
Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. } Which three are valid on line
12?
(Choose three.)
A.
B.
C.
D.
E.
F.
G.
final
static
native
public
private
abstract
protected
Answer: ABD
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 9
Given:
5. class Atom {
6. Atom() { System.out.print("atom "); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super("granite ");
14. new Rock("granite ");
15. }
16. public static void main(String[] a) { new Mountain(); }
17. }
What is the result?
A.
B.
C.
D.
E.
F.
Compilation fails.
atom granite
granite granite
atom granite granite
An exception is thrown at runtime.
atom granite atom granite
Answer: F
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 10
Click the Exhibit button. Which three statements are true? (Choose three.)
A.
B.
C.
D.
E.
F.
Compilation fails.
The code compiles and the output is 2.
If lines 16, 17 and 18 were removed, compilation would fail.
If lines 24, 25 and 26 were removed, compilation would fail.
If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.
Answer: BEF
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 11
Given:
10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A.
B.
C.
D.
Point p = Line.getPoint();
Line.Point p = Line.getPoint();
Point p = (new Line()).getPoint();
Line.Point p = (new Line()).getPoint();
Answer: D
Section: (none)
Explanation/Reference:
Explanation:
QUESTION 12
Given:
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {