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. } 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<int> intList)". Replace the method declaration with "sum(List<Integer> intList)". 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? java.util.Queue java.util.ArrayList java.util.LinearList java.util.LinkedList. 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. } public class MinMax<?> { public class MinMax<? extends Number> { public class MinMax<N extends Object> { public class MinMax<N extends Number> { public class MinMax<? extends Object> { public class MinMax<N extends Integer> {. 12. import java.util.*;
13. public class Explorer2 {
14. public static void main(String[] args) {
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
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? 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]. 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? 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*/}. 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? The time to find the value from HashMap with a Person key depends on the size of the map. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person. Inserting a second Person object into a HashSet will cause the first Person object to be
removed as a duplicate. 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. 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? [1, 2, 3, 5] [2, 1, 3, 5] [2, 5, 3, 1] [5, 3, 2, 1] [1, 3, 5, 2] Compilation fails. An exception is thrown at runtime. Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. } Which three are valid on line
12? final static native public private abstract protected. 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? Compilation fails. atom granite granite granite atom granite granite An exception is thrown at runtime. atom granite atom granite. 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 here17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
Point p = Line.getPoint(); Line.Point p = Line.getPoint(); Point p = (new Line()).getPoint(); Line.Point p = (new Line()).getPoint();.
|