SCJP MATERIAL
  <<Back To SCJP Material Main Page
Page: 12

12. Collections and Generics

55. Given:
import java.util.*;
public class Group extends HashSet<Person> {
public static void main(String[] args) {
Group g = new Group();
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total: " + g.size());
}
public boolean add(Object o) {
System.out.println("Adding: " + o);
return super.add(o);
}
}
class Person {
private final String name;
public Person(String name) { this.name = name; }
public String toString() { return name; }
}
Which of the following occur at least once when the code is compiled and run?
(Choose all that apply.)
A. Adding Hans                  B. Adding Lotte
C. Adding Jane                   D. Total: 3
E. Total: 5                            F. The code does not compile.
G. An exception is thrown at runtime.
Answer:
-> F is correct. The problem here is in Group's add() method—it should have been
add(Person), since the class extends HashSet<Person>. So this doesn't compile.
Pop Quiz: What would happen if you fixed this code, changing add(Object) to
add(Person)? Try running the code to see if the results match what you thought.
-> A, B, C, D, E, and G are incorrect based on the above.

56. Given:
import java.util.*;
class AlgaeDiesel {
public static void main(String[] args) {
String[] sa = {"foo", "bar", "baz" };
// insert method invocations here
}
}
What java.util.Arrays and/or java.util.Collections methods could you use to convert sa to a List and then search the List to find the index of the element whose value is "foo"? (Choose from one to three methods.)
A. sort()                             B. asList()
C. toList()                          D. search()
E. sortList()                        F. contains()
G. binarySearch()            
Answer:
-> A, B, and G are required. The as List() method converts an array to a List. You can find the index of an element in a List with the binarySearch() method, but before you do that you must sort the list using sort().
-> F is incorrect because contains() returns a boolean, not an index. C, D, and E are incorrect, because these methods are not defined in the List interface. (Objective 6.5)

57. Given that String implements java.lang.CharSequence, and:
import java.util.*;
public class LongWordFinder {
public static void main(String[] args) {
String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
} }
Which declarations could be inserted at // INSERT DECLARATION HERE so that the program will compile and run? (Choose all that apply.)
A. public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<E> coll)
B. public static <E extends CharSequence> List<E>
getLongWords(Collection<E> coll)
C. public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
D. public static List<CharSequence>
getLongWords(Collection<CharSequence> coll)
E. public static List<? extends CharSequence>
getLongWords(Collection<? extends CharSequence> coll)
F. static public <E extends CharSequence> Collection<E>
getLongWords(Collection<E> coll)
G. static public <E super CharSequence> Collection<E>
getLongWords(Collection<E> coll)

Answer:
->   F is correct.
->   A is close, but it's wrong because the return value is too vague. The last line of the
method expects the return value to be Collection<String>, not
Collection<? extends CharSequence>. B is wrong because longWords has been declared as a Collection<E>, and that can't be implicitly converted to a List<E> to match the declared return value. (Even though we know that longWords is really an ArrayList<E>, the compiler only know what it's been declared as.) C, D, and E are wrong because they do not declare a type variable E (there's no <> before the return value) so the getLongWords() method body will not compile. G is wrong because E super CharSequence makes no sense—super could be used in conjunction with a wildcard but not a type variable like E.

58. Given:
12. TreeSet map = new TreeSet();
13. map.add("one");
14. map.add("two");
15. map.add("three");
16. map.add("four");
17. map.add("one");
18. Iterator it = map.iterator();
19. while (it.hasNext() ) {
20. System.out.print( it.next() + " " );
21. }
What is the result?
A. Compilation fails.
B. one two three four
C. four three two one
D. four one three two
E. one two three four one
F. one four three two one
G. An exception is thrown at runtime.
H. The print order is not guaranteed.

Answer:
-> D is correct. TreeSet assures no duplicate entries; also, when it is accessed it
will return elements in natural order, which for Strings means alphabetical.
-> A, B, C, E, F, G, and H are incorrect based on the logic described above. Note, even
though as of Java 5 you don't have to use an Iterator, you still can.

59. Given a method declared as:

public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)

 

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
B. ArrayList<Integer> input = null;
List<Integer> output = null;
C. ArrayList<Integer> input = null;
List<Number> output = null;
D. List<Number> input = null;
ArrayList<Integer> output = null;
E. List<Number> input = null;
List<Number> output = null;
F. List<Integer> input = null;
List<Integer> output = null;
G. None of the above.

Answer:
->B, E, and F are correct.
-> The return type of process is definitely declared as a List, not an ArrayList, so A and D are wrong. C is wrong because the return type evaluates to List<Integer>, and that can't be assigned to a variable of type List<Number>. Of course all these would probably cause a NullPointerException since the variables are still null—but the question only asked us to get the code to compile.

Page: 12

1 2 3 4 5 6 7 8 9 10 11 12
  <<Back To SCJP Material Main Page



freshers jobs | fresher jobs | freshers job | fresher job | fresher it jobs | sap fresher jobs | jobs for fresher | jobs for freshers | it jobs for freshers | abap fresher jobs | net jobs for freshers | net fresher jobs | net freshers jobs | freshers job sites | fresher job openings | software jobs for freshers | fresher jobs bangalore | hr jobs for freshers | software testing jobs for freshers | fresher jobs india | fresher jobs chennai | fresher it job | freshers it jobs | mba fresher jobs | hr fresher jobs | fresher jobs in india | networking jobs for freshers | testing jobs for freshers | job for fresher | oracle fresher jobs | mca fresher jobs | sap freshers jobs | jobs for mca freshers | fresher jobs hyd | freshers software jobs | it job for freshers | fresher job sites | jobs for mba freshers | freshers job it | hot jobs for freshers | job for freshers | testing fresher jobs | mainframe fresher jobs | jobs for freshers sap | fresher jobs at | fresher jobs usa | jobs for freshers at | jobs for freshers usa | freshers jobs at | freshers jobs usa | tech freshers jobs | net freshers job | software fresher jobs | job opportunities for freshers | jobs for freshers in usa | job vacancies for freshers | jobs mumbai fresher | it jobs for fresher | freshers job openings | job openings for freshers | it jobs for freshers in | freshers jobs bangalore | freshers job india | freshers jobs india | jobs in pune for freshers | jobs for fresher in | fresher jobs in usa | net fresher jobs in | fresher jobs in | fresher job in usa | jobs for freshers in | qa jobs for freshers | fresher jobs in dubai | freshers jobs in usa | job opening for freshers | hr freshers job | freshers job in usa | job openings for freshers in | fresher it jobs in | freshers it jobs in | job for fresher in | job for freshers in | testing jobs for freshers in | sap jobs for freshers in | sap freshers jobs in | sap fresher jobs in | mba freshers jobs | jobs for freshers in delhi | jobs for freshers in mumbai | freshers jobs in delhi | fresher jobs in mumbai | fresher jobs in bangalore | fresher jobs in delhi | net jobs for freshers in | fresher jobs in hyderabad | mba freshers job | freshers job in bangalore | fresher hr jobs in | fresher job in | jobs freshers in | finance jobs for freshers | hr jobs for freshers in | jobs in dubai for freshers | fresher it jobs in bangalore | freshers job in india | freshers job in | software jobs for freshers in | mba fresher job | fresher software jobs in | software testing jobs for freshers in | freshers jobs in mumbai | fresher jobs in chennai | mba fresher jobs in | mba finance fresher jobs | jobs for mba freshers in | jobs for mba fresher | mba freshers jobs in | fresher jobs hyderabad | hr freshers jobs | fresher jobs in pune | freshers jobs in pune | freshers jobs in chennai | jobs for mba finance fresher | freshers job in chennai | jobs for mba finance freshers | finance fresher jobs | jobs for freshers in hyderabad | freshers jobs in hyderabad | jobs for freshers in bangalore | freshers jobs in india | jobs for freshers in chennai | jobs for freshers in india | instrumentation jobs for freshers | freshers jobs in bangalore | engineering jobs for freshers in | job for mba fresher | mechanical fresher jobs | mca freshers jobs | jobs for bca freshers | b tech freshers jobs | sap jobs for fresher | sap abap fresher jobs | job sites for freshers | fresher | jobs | bpo jobs | freshers | careers freshers | freshers it | it jobs | fresher world | mba jobs | j2ee jobs | fresher recruitment | call center jobs | testing jobs | finance jobs | call centre jobs | networking jobs | wipro jobs | hyderabad jobs | chennai jobs | fresher opening | pune jobs | telecom jobs | fresher openings | banglore jobs | bangalore jobs | mca jobs | tcs jobs | kolkata jobs | it fresher | mba fresher | fresher india | gurgaon jobs | hr jobs | noida jobs | india jobs | mumbai jobs | fresh graduates | delhi jobs | freshers recruitment | consultant jobs | engineer jobs | engineering jobs | sap jobs | parttime job | walkin jobs | internet jobs | data entry jobs | jobs career | local jobs | online jobs | parttime jobs | online part time job | technical support jobs | net jobs | jobs at | software jobs | website jobs

Home   |   About Us   |   Contact Us
Copyright @ durgasoft.com