Examen 01 Java Oracle
Examen 01 Java Oracle
Test Question 1
1. packagecom.sun;
2. public class PkgAccess {
3. public static int tiger = 1414;
4. }
And:
1. import static com.sun.PkgAccess.*;
2.
3. public class PkgAccess2 {
4.
5. int x1 = PkgAccess.tiger;
6. int x2 = tiger;
7. int x3 = com.sun.PkgAccess.tiger;
8. int x4 = sun.PkgAccess.tiger;
9. }
Bottom of Form
Test Question 2
1. package com.sun2;
2. public enum Seasons {SUMMER, FALL, WINTER, SPRING}
And:
1. import com.sun2.Seasons;
2. import static com.sun2.Seasons.*;
3. class Enum3a {
4. Seasons s = FALL;
5. }
And:
1. import com.sun2.*;
2. import static com.sun2.Seasons.FALL;
3. class Enum3b {
4. Seasons s = FALL;
5. }
Which is true?
Top of Form
Bottom of Form
Option A is correct. Both import styles are valid for enum types.
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.2
Test Question 3
Given:
Bottom of Form
JLS 3.0
Option A is correct. It is legal for an interface to extend many interfaces, and it's
okay to declare variables and overrides and overloads.
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.3
Test Question 4
Given:
3. enum Animals {
4. DOG,
5. BIRD { publicintgetLegs() { return 2; } },
6. HORSE;
7. publicintgetLegs() { return 4; }
8. }
9. public class Zookeeper {
10. public static void main(String[] args) {
11. System.out.println(Animals.DOG.getLegs() + " " +
Animals.BIRD.getLegs());
12. }
13. }
A The output is 4 2
.
B. The output is 4 4
Bottom of Form
Option A is correct. It is legal for an enum to have methods and to override those
methods for a specific constant.
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.4
Test Question 5
Given:
A 1 2 3
.
B 1 4 7
.
C 2 5 8
.
D 4 5 6
.
E Compilation fails.
.
F. An exception is thrown at runtime.
Bottom of Form
Option C is correct. It is legal to use var-args to pass arrays, and arrays use zero
based indexes
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.4
Test Question 6
Given:
1. classDoStuff {
2. public static void main(String [] args) {
3. doIt(1);
4. doIt(1,2);
5. }
6. // insert code here
7. }
Bottom of Form
Options A, D and E are correct. Options B and C are incorrect because a var-args
argument must be a method's last argument.
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.5
Test Question 7
Given:
1. classSuperFoo {
2. SuperFoodoStuff(int x) {
3. return new SuperFoo();
4. }
5. }
6.
7. class Foo extends SuperFoo {
8. // insert code here
9. }
E SuperFoodoStuff(int x) { return
. new Foo(); }
Bottom of Form
Options A, C, and E are correct. Options B and D are incorrect because an override
cannot change the return type unless it's a valid covariant return. Option C is a valid
covariant return.
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.5
Test Question 8
Given:
1. class FWD {
2. intdoMud(int x) { return 1; }
3. }
4. class Subaru extends FWD {
5. intdoMud(int... y) { return 2; }
6. intdoMud(long z) { return 3; }
7. }
8. class Race {
9. public static void main(String [] args) {
10. FWD f = new Subaru();
11. System.out.println(f.doMud(7));
12. }
13. }
A 1
.
B 2
.
C 3
.
D 7
.
E Compilation fails.
.
F. The output is NOT predictable.
Bottom of Form
Option A is correct. The method on line 2 matches the call made on line 11. The
method on line 5 has a different signature.
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.6
Test Question 9
Given:
6. class Tack {
7. static short s = 17;
8. public Tack(short ss) {
9. new Tack();
10. s *= ss;
11. }
12. public Tack() { ; }
13. }
14. public class Bridle extends Tack {
15. public Bridle(int s) { System.out.println(s + 1); }
16. public static void main(String[] args) {
17. Bridle b = new Bridle(3);
18. }
19. }
A 3
.
B. 4
C 31
.
D 51
.
E. 52
F. Compilation fails.
Bottom of Form
Option B is correct. The Tack constructor with one argument is never invoked in this
code
Section 2: 2: Flow Control - Objective 2.1
Test Question 10
Given:
1. class Test4 {
2. public static void main(String [] args) {
3. boolean x = true;
4. boolean y = false;
5. short z = 42;
6.
7. if((z++ == 42) && (y = true)) z++;
8. if((x = false) || (++z == 45)) z++;
9.
10. System.out.println("z = " + z);
11. }
12. }
A z = 42
.
B z = 44
.
C z = 45
.
D z = 46
.
E Compilation fails.
.
F. An exception is thrown at runtime.
Bottom of Form
Option D is correct. Line 7 does NOT test y, it sets it to true. Line 8 pre-increments
z in the if test.
Section 2: 2: Flow Control - Objective 2.1
Test Question 11
Given:
A x = 10
.
B x = 110
.
C x = 1110
.
D Compilation fails.
.
E An exception is thrown at runtime.
.
Bottom of Form
Option C is correct. This is the standard switch fall-through logic, and as of Java 1.5
enums can be used in a switch.
Section 2: 2: Flow Control - Objective 2.2
Test Question 12
Given:
23. int x = 7;
24. switch (x) {
25. case 8: System.out.print("8");
26. case 7: System.out.print("7");
27. case 6: System.out.print("6");
28. default: System.out.print("def");
29. case 9: System.out.print("9");
30. }
A 7
.
B 789
.
C 76def
.
D 76def9
.
E Compilation fails.
.
Bottom of Form
Option C is correct. This is the standard switch fall-through logic, and as of Java 1.5
enums can be used in a switch.
Section 2: 2: Flow Control - Objective 2.2
Test Question 13
Given:
4. class Breakfast {
5. public static void main(String[] args) {
6. free:
7. for(int j = 3; j > 0; j--) {
8. for(int k = 0; k < 4; k++) {
9. if(j == 1) break free;
10. if(k == 2) break;
11. System.out.print(" " + j + k);
12. }
13. }
14. }
15. }
A 30 31 20 21
.
B 30 31 32 20 21 22
.
C 30 31 32 33 20 21 22 23
.
D 30 31 32 20 21 22 10 11 12
.
E 30 31 32 33 20 21 22 23 10
. 11 12 13
F. Compilation fails.
Bottom of Form
Option A is correct. The break halts the inner loop, the labeled break halts the outer
loop.
Section 2: 2: Flow Control - Objective 2.3
Test Question 14
Given:
7. class Bold {
8. public static void main(String[] args) {
9. Boolean boo = true;
10. assert(!boo): "yee ";
11. System.out.println("haw ");
12. }
13. }
Bottom of Form
Option D is correct. The command-line enables assertions, and yee is added to the
stack trace before the program ends with an AssertionError.
Section 2: 2: Flow Control - Objective 2.3
Test Question 15
Given:
1. classMoreAsserts {
2. staticint x = 5;
3. public static void main(String [] args) {
4. assert(doStuff(42));
5. if(x < 40) ;
6. else assert(false);
7. }
8. public static booleandoStuff(intarg) {
9. assert(arg< x++);
10. return false;
11. }
12. }
Which is true?
Top of Form
Bottom of Form
Test Question 16
Given:
A 42
.
B Exc
.
C 42Exc
.
D Compilation fails.
.
E An exception is thrown at runtime.
.
Bottom of Form
Option A is correct. It is legal for an overriding method to NOT throw the overridden
method's exception.
Section 2: 2: Flow Control - Objective 2.5
Test Question 17
Given:
1. class Flow {
2. public static void main(String [] args) {
3. try {
4. System.out.print("before ");
5. doRiskyThing();
6. System.out.print("after ");
7. } catch (Exception fe) {
8. System.out.print("catch ");
9. }
10. System.out.println("done ");
11. }
12. public static void doRiskyThing() throws Exception {
13. // this code returns unless it throws an Exception
14. } }
A before
.
B before catch
.
C before after done
.
D before catch done
.
E before after catch
.
F. before after catch done
Bottom of Form
Test Question 18
Given:
1. class Birds {
2. public static void main(String [] args) {
3. try {
4. throw new Exception();
5. } catch (Exception e) {
6. try {
7. throw new Exception();
8. } catch (Exception e2) { System.out.print("inner
"); }
9. System.out.print("middle ");
10. }
11. System.out.print("outer ");
12. }
13. }
A inner
.
B. inner outer
C middle outer
.
D inner middle outer
.
E. middle inner outer
F. Compilation fails.
Bottom of Form
Option D is correct. It is legal to nest try/catches and normal flow rules apply.
Section 2: 2: Flow Control - Objective 2.6
Test Question 19
Given:
1. class Adder {
2. static Short s1,s2;
3. public static void main(String [] args) {
4. int x;
5. s1 = 4;
6. x = s1 + s2;
7. System.out.print(x);
8. }
9. }
A 4
.
B Compilation fails.
.
C A
. java.lang.ClassCastException
is thrown.
D A
. java.lang.NullPointerExcepti
on is thrown.
E A
. java.lang.IllegalStateExcept
ion is thrown.
Bottom of Form
Test Question 20
Given:
1. classCalc {
2. public static void main(String [] args) {
3. try {
4. int x = Integer.parseInt("42a");
5. // insert code here
6. System.out.print("oops ");
7. }
8. }
9. }
Which two, inserted independently at line 5, cause the output to be "oops "?
(Choose two.)
Top of Form
A } catch (ClassCastException
. c) {
B } catch
. (IllegalStateException c) {
C } catch
. (NumberFormatException n) {
D } catch
. (IllegalArgumentException e)
{
E } catch
. (ExceptionInInitializerError
e) {
Bottom of Form
Test Question 21
Given:
1. classWideLoad {
2. public static void main(String [] args) {
3. float f = 3.14f;
4. newWideLoad().doIt(f);
5. }
6. voiddoIt(Float f) {
7. System.out.println("Float");
8. }
9. voiddoIt(double d) {
10. System.out.println("double");
11. }
12. }
A Float
.
B double
.
C Compilation fails.
.
D The output is not predictable.
.
E An exception is thrown at runtime.
.
Bottom of Form
Option B is correct. The JVM will widen before it boxes, so the method on line 9 is
invoked.
Section 3: 3: API Contents - Objective 3.1
Test Question 22
Given:
Bottom of Form
Options A is correct. Fragment I would require both widening and boxing. Fragment
IV would require widening from one wrapper type to another. Fragment III would
compile if 7 had been 7L.
Section 3: 3: API Contents - Objective 3.2
Test Question 23
Which two are true about the java.io.Console class? (Choose two.)
Top of Form
Bottom of Form
Options A and D are correct. B, C, and E are true only for some of the class's
methods.
Section 3: 3: API Contents - Objective 3.2
Test Question 24
Given:
And:
34. String line = null;
35.
36. // insert code here
37. System.out.println(line);
38. }
Which code, inserted at line 36, will loop through a text file and output a line
at a time from the text field?
Top of Form
A while((line = f.read()) !=
. null) {
B while((line = fr.read()) !=
. null) {
C while((line = br.read()) !=
. null) {
D while((line =
. f.readLine()) != null) {
E while((line = fr.readLine())
. != null) {
F. while((line = br.readLine())
!= null) {
Bottom of Form
Test Question 25
Bottom of Form
Test Question 26
Given:
A 765.43
.
B 65.4321
.
C 765.4321
.
D Compilation fails.
.
E An exception is thrown at runtime.
.
Bottom of Form
Test Question 27
What is true?
Top of Form
Bottom of Form
Test Question 28
Which regex pattern finds both 0x4A and 0X5 from within a source file?
Top of Form
A 0[xX][a-fA-F0-9]
.
B 0[xX](a-fA-F0-9)
.
C 0[xX]([a-fA-F0-9])
.
D 0[xX]([a-fA-F0-9])+
.
E 0[xX]([a-fA-F0-9])?
.
Bottom of Form
Test Question 29
Given:
1. importjava.util.*;
2. classScanStuff {
3. public static void main(String [] args) {
4. String s = "x,yy,123";
5. Scanner sc = new Scanner(s);
6. while (sc.hasNext())
7. System.out.print(sc.next() + " ");
8. }
9. }
A x yy
.
B x,yy
.
C x yy 123
.
D x,yy,123
.
E Compilation fails.
.
F. An exception is thrown at runtime.
Bottom of Form
Test Question 30
Given:
A x
.
B xx
.
C xxx
.
D Compilation fails.
.
E The code runs with no output.
.
F. An exception is thrown at runtime.
Bottom of Form
Option F is correct. Multiple threads can be created using the same Runnable
object, but a given thread can be started only once.
Section 4: 4: Concurrency - Objective 4.2
Test Question 31
Given:
What would be the most likely impact to the output if line 7 was removed?
Top of Form
Bottom of Form
Option F is correct. As the code stands the yield() will tend to give priority to the
main thread, which will tend to place more "2" characters near the end of the
output.
Section 4: 4: Concurrency - Objective 4.2
Test Question 32
Given:
A in pre
.
B pre in
.
C in post pre
.
D in pre post
.
E pre in post
.
F. pre post in
Bottom of Form
Options D and E are correct. The join assures that the post will be last, and the
order lines 8 and 13 are NOT absolutely predictable.
Section 4: 4: Concurrency - Objective 4.3
Test Question 33
Given:
Bottom of Form
Option B is correct. Two different Lockdown objects are using the locked() method.
Section 4: 4: Concurrency - Objective 4.3
Test Question 34
Given:
Which is true?
Top of Form
Bottom of Form
Option E is correct. To make sure that line 16 is never executed when an invocation
of the run method is partially complete, it (line 16) should be placed inside the run
method.
Section 4: 4: Concurrency - Objective 4.4
Test Question 35
1. class Waiting implements Runnable {
2. boolean flag = false;
3. public synchronized void run() {
4. if (flag) {
5. flag = false;
6. System.out.print("1 ");
7. try { this.wait(); } catch (Exception e) { }
8. System.out.print("2 ");
9. }
10. else {
11. flag = true;
12. System.out.print("3 ");
13. try { Thread.sleep(2000); } catch (Exception e) { }
14. System.out.print("4 ");
15. notify();
16. }
17. }
18. public static void main(String [] args) {
19. Waiting w = new Waiting();
20. new Thread(w).start();
21. new Thread(w).start();
22. }
23. }
Bottom of Form
Options B and E are correct. Because run is synchronized, the first thread
completes the entire else block before the second thread can enter run. The
notify is issued before the wait, so the code hangs.
Section 5: 5: OO Concepts -
Test Question 36
Bottom of Form
Option B is correct. Low coupling (indicating that classes know the least possible
about each other), is preferable to tight coupling. High cohesion (indicating that a
system's classes each have well focused responsibilities) is preferable to low
cohesion.
Section 5: 5: OO Concepts -
Test Question 37
Given:
Bottom of Form
Options B and C are correct. The setAddress method makes sense in terms of a
customer, however a customer would be unlikely to check inventory.
Section 5: 5: OO Concepts - Objective 5.2
Test Question 38
Given:
4. class Slug {
5. static void crawl() { System.out.print("crawling "); }
6. }
7. public class BananaSlug extends Slug {
8. public static void main(String[] args) {
9. Slug[] sa = { new Slug(), new BananaSlug() };
10. for(Slug s: sa)
11. crawl();
12. }
13. static void crawl() { System.out.print("shuffling "); }
14. }
A crawling crawling
.
B crawling shuffling
.
C shuffling shuffling
.
D Compilation fails
.
E An exception is thrown at runtime.
.
Bottom of Form
Test Question 39
Given:
A Only line 3a
.
B Only line 3b
.
C Either line 3a or 3b
.
D Both line 3a and 3b added together.
.
Bottom of Form
Option C is correct. Polymorphic calls and co-variant returns can work together
Section 5: 5: OO Concepts - Objective 5.3
Test Question 40
Given:
A 7 5
.
B 7 7
.
C 7 12
.
D Compilation fails
.
E An exception is thrown at runtime
.
Bottom of Form
Test Question 41
Given:
6. class Bird {
7. protected void talk() { System.out.print("chirp "); }
8. }
9. public class Macaw extends Bird {
10. public static void main(String [] args) {
11. Bird [] birds = {new Bird(), new Macaw()};
12. for( Bird b : birds)
13. b.talk();
14. }
15. void talk() { System.out.print("hello "); }
16. }
A chirp chirp
.
B chirp hello
.
C hello hello
.
D hello chirp
.
E Compilation fails
.
F. An exception is thrown at runtime
Bottom of Form
Option E is correct. The overriding talk() method has a weaker access modifier.
Section 5: 5: OO Concepts - Objective 5.4
Test Question 42
Given:
4. class Gadget {
5. Gadget openStuff() { s = s + "g "; return this; }
6. static String s = "";
7. }
8. public class Opener extends Gadget {
9. public static void main(String[] args) {
10. Gadget g1 = new Gadget();
11. Gadget g2 = new Opener();
12. g1.openStuff();
13. g2.openStuff();
14. System.out.println(s);
15. }
16. Opener openStuff() { s = s + "o "; return this; }
17. }
A g g
.
B g o
.
C o g
.
D o o
.
E Compilation fails
.
F. An exception is thrown at runtime
Bottom of Form
Test Question 43
Given:
2. class Cat {
3. Cat(int c) { System.out.print("cat" + c + " "); }
4. }
5. classSubCat extends Cat {
6. SubCat(int c) { super(5); System.out.print("cable "); }
7. SubCat() { this(4); }
8. public static void main(String [] args) {
9. SubCat s = new SubCat();
10. }
11. }
A cat5
.
B cable
.
C cable cat5
.
D cat5 cable
.
E Compilation fails.
.
F. An exception is thrown at runtime.
Bottom of Form
Option D is correct. Type Cat does NOT need a no-argument constructer because
SubCat's no-argument constructor calls SubCat's one argument constructor.
Section 5: 5: OO Concepts - Objective 5.5
Test Question 44
A class Wingit { }
. class Fizzler extends Oompah
implements Whoosh { }
interface Whoosh {
Wingits [] w;
}
class Oompah { }
B class Wingit { }
. class Fizzler extends Whoosh
implements Oompah { }
class Whoosh {
Wingits [] w;
}
interface Oompah { }
C class Fizzler { }
. class Wingit extends Fizzler
implements Oompah { }
class Whoosh {
Wingits [] w;
}
interface Oompah { }
D interface Wingit { }
. class Fizzler extends Whoosh
implements Wingit { }
class Wingit {
Whoosh [] w;
}
class Whoosh { }
Bottom of Form
Test Question 45
Given:
2. importjava.util.*;
3. classMyInfo implements Comparable {
4. MyInfo(String s) { desc = s; }
5. String desc;
6. // insert code here
7. public String toString() { return desc; }
8. }
A int compare(MyInfo m) {
. return desc.compareTo(m.desc); }
B intcompareTo(MyInfo m) {
. return desc.compareTo(m.desc); }
C intcompareable(MyInfo m) {
. return desc.compareTo(m.desc); }
D int compare(Object o) {
. return
desc.compareTo(o.toString()); }
E intcompareTo(Object o) {
. return
desc.compareTo(o.toString()); }
F. intcompareable(Object o) {
return
desc.compareTo(o.toString()); }
Bottom of Form
Test Question 46
A Invoke javac
. -classpathjarDir/MyJar.jar
Test.java
B Invoke javac -CLASSPATH
. jarDir/MyJar.jar Test.java
C Add jarDir to the CLASSPATH and
. invoke javac Test.java
D Add jarDir/MyJar.jar to the
. CLASSPATH and invoke javac
Test.java
Bottom of Form
Options A and D are correct. These two approaches correctly match the javac
command with the classpath information it needs.
Section 6: 6: Collections / Generics - Objective 7.5
Test Question 47
Bottom of Form
Test Question 48
Given:
A 1st
.
B. 2nd
C 3rd
.
D 1st 2nd
.
E. 1st 3rd
F. 2nd 3rd
G No output is produced
.
Bottom of Form
Option E is correct. The ^ operator returns true when exactly one operand is true.
Section 6: 6: Collections / Generics - Objective 6.2
Test Question 49
Bottom of Form
Options A and C are correct. Option E is incorrect because the equals() method
must take an Object.
Section 6: 6: Collections / Generics - Objective 6.3
Test Question 50
Given:
3. importjava.util.*;
4. public class States {
5. public static void main(String[] args) {
6. TreeMap<String, Integer>myMap = new TreeMap<String,
Integer>();
7. myMap.put("ak", 50);
8. myMap.put("co", 60);
9. myMap.put("ca", 70);
10. myMap.put("ar", 80);
11. // insert code here
12. System.out.println(myMap2.get("ak") + " " +
myMap2.get("co"));
13. }
14. }
Which two, inserted independently at line 11, produce the output 50 60?
(Choose two.)
Top of Form
A NavigableMap<String,
. Integer> myMap2 =
myMap.headMap("b", true);
B NavigableMap<String,
. Integer> myMap2 =
myMap.headMap("z", true);
C NavigableMap<String,
. Integer> myMap2 =
myMap.headMap("a", true);
D NavigableMap<String,
. Integer> myMap2 =
myMap.tailMap("z", true);
E NavigableMap<String,
. Integer> myMap2 =
myMap.tailMap("b", true);
F. NavigableMap<String,
Integer> myMap2 =
myMap.tailMap("a", true);
Bottom of Form
Answer: B and F are correct. headMap() returns keys that are less than the
argument, and tailMap() returns keys that are greater than the argument.
Section 6: 6: Collections / Generics - Objective 6.3
Test Question 51
Given:
1. importjava.util.*;
2. classSubGen {
3. public static void main(String [] args) {
4. // insert code here
5. }
6. }
7. class Alpha { }
8. class Beta extends Alpha { }
9. class Gamma extends Beta { }
A Only s1
.
B Only s3
.
C Only s1 and s3
.
D Only s1 and s4
.
E Only s1, s3, and s4
.
F. All of the codes will compile.
Bottom of Form
Option D is correct. Options s1 and s4 use valid syntax.
Section 6: 6: Collections / Generics - Objective 6.3
Test Question 52
Given:
5. importjava.util.*;
6. public class Cartesian {
7. public static void main(String[] args) {
8. TreeMap<String, Integer>myMap = new TreeMap<String,
Integer>();
9. myMap.put("ak", 50); myMap.put("co", 60);
10. myMap.put("ca", 70); myMap.put("ar", 80);
11. NavigableMap<String, Integer> myMap2 =
myMap.headMap("d", true);
12. myMap.put("fl", 90);
13. myMap2.put("al", 110);
14. System.out.println(myMap.size() + " " +
myMap2.size());
15. }
16. }
A 5 5
.
B 5 6
.
C 6 5
.
D 6 6
.
E Compilation fails.
.
F. An exception is thrown at runtime.
Bottom of Form
C is correct. myMap2 is backed by myMap, but the fl key is out of range for myMap2.
Section 6: 6: Collections / Generics - Objective 6.4
Test Question 53
Given:
2. class Marjoram {
3. public static void main(String [] args) {
4. Marjoram g = new Marjoram();
5. g.go(1);
6. }
7. <A extends Alpha> Alpha go(int i) {
8. if (i == 1) return new Alpha();
9. else return new Beta();
10. }
11. }
12. class Alpha { }
13. class Beta extends Alpha { }
Bottom of Form
Option A is correct. Line 7 is valid syntax for a method with a generic return type.
Section 6: 6: Collections / Generics - Objective 6.4
Test Question 54
Given:
2. importjava.util.*;
3. class Beta extends Alpha {
4. public static void go(Set<Alpha> set) { }
5. public static void main(String [] args) {
6. Set<Alpha>setA = new TreeSet<Alpha>();
7. Set<Beta>setB = new TreeSet<Beta>();
8. Set<Object>setO = new TreeSet<Object>();
9. // insert code here
10. }
11. }
12. class Alpha { }
A only s1
.
B only s2
.
C only s3
.
D only s1 and s2
.
E only s1 and s3
.
F. All the codes will compile.
Bottom of Form
Option A is correct. Codes s2 and s3 attempt to make an illegal invocation of the go
method that takes only a Set of Alphas.
Section 6: 6: Collections / Generics - Objective 6.5
Test Question 55
Given:
4. importjava.util.*;
5. class Looking {
6. public static void main(String[] args) {
7. String[] sa = {"d", "c", "a", "b" };
8. int x = Arrays.binarySearch(sa, "b");
9. Arrays.sort(sa);
10. int y = Arrays.binarySearch(sa, "b");
11. System.out.println(x + " " + y);
12. }
13. }
A 7 0
.
B 7 1
.
C 7 3
.
D -1 0
.
E -1 1
.
F. -1 3
Bottom of Form
Options B and E are correct. The results of searching an unsorted array are
undefined, and sorted arrays have zero-based indexes. Note: Answer e is almost
certainly the most common output that would be produced.
Section 6: 6: Collections / Generics - Objective 6.5
Test Question 56
Given:
1. importjava.util.*;
2. classDumpMap {
3. public static void main(String [] args) {
4. HashMap h = new HashMap();
5. h.put("a","aa"); h.put("b","bb"); h.put("c","cc");
6. Set ks = h.keySet();
7. Object [] ka1 = ks.toArray();
8. ks = new TreeSet(ks);
9. Object [] ka2 = ks.toArray();
10. System.out.println(Arrays.equals(ka1, ka2));
11. }
12. }
A true
.
B false
.
C Compilation fails due to an error on line 8.
.
D The output depends on how the hashing
. function is implemented.
Bottom of Form
Option D is correct. ka2 is sorted, but there is no guarantee what order ka1's
elements are in
Section 6: 6: Collections / Generics - Objective 7.1
Test Question 57
1. package x;
2. public class X {
3. // insert code here
4. }
7. package x;
8. public class Find4 {
9. public static void main(String [] args) {
10. X myX = new X();
11. myX.doX();
12. }
13. }
A Only method I.
.
B Only methods I and II.
.
C Only methods I and III.
.
D Only methods II and III.
.
E Only methods I, II, and III
.
F. All all the methods will compile.
Bottom of Form
Option E is correct. Only the private doX method would be inaccessible to class
Find4.
Section 6: 6: Collections / Generics - Objective 7.2
Test Question 58
Given:
1. importjava.util.*;
2. classPow {
3. static String [] wow = {"Bamm", "Biff"};
4. public static void main(String [] yikes) {
5. if(Arrays.equals(yikes,wow))
6. System.out.print("got a match? ");
7. if(yikes == wow)
8. System.out.println("sure chief");
9. }
10. }
A got a match?
.
B Compilation fails.
.
C No output is produced.
.
D got a match? sure chief
.
E An exception is thrown at runtime.
.
Bottom of Form
Option A is correct. Two different array objects can never be ==, even if their
contents are the same.
Section 6: 6: Collections / Generics - Objective 7.3
Test Question 59
Given:
1. classFlibitz {
2. public static void main(String [] args) {
3. intgrop = 7;
4. newFlibitz().go(grop);
5. System.out.print(grop);
6. }
7. void go(intgrop) {
8. if(++grop> 7) grop++;
9. System.out.print(grop);
10. }
11. }
A 77
.
B 79
.
C 97
.
D 99
.
E Compilation fails.
.
F. An exception is thrown at runtime.
Bottom of Form
Option C is correct. The grop on line 7 is different from the grop on line 9.
Section 6: 6: Collections / Generics - Objective 7.4
Test Question 60
Bottom of Form
Options A and D are correct. An object can be collected even if it has a valid
reference, if no live thread has access to the object; objects reside in the heap; and
the finalize method can send an object reference to another object, thereby
saving the otherwise doomed object.