Você está na página 1de 31

Copyright 2002 _ McGraw-Hill December 8, 2002

(ct) MasterExam3

Q1: Keywords 1
Which two are keywords? (Choose two.)
A. method
B. Long
C. throw
D. finalize
E. case
Hint: Which keywords are capitalized?
Reference: Chapter 1: Keywords
Objective: Java Programming Language Keywords (Objective 4.4)

Q2: Keywords 2

2. Which is a keyword? (Choose one.)


A. array
B. default
C. subclass
D. FALSE
E. extend
Hint: Think declarations, not concepts
Reference: Chapter 1: Keywords
Objective: Java Programming Language Keywords (Objective 4.4)

Q3: Declaring a String

3. Which two are valid declarations of a String? (Choose two.)


A. String s1 = '\ubeef';
B. String s2 = 'beef';
C. String s3 = (String) "abc";
D. String s4 = 'null';
E. String s5 = (String) 'abc';
F. String s6 = null;
Hint: Remember the difference between single quotes and double quotes.
Reference: Chapter 1: Literals and Ranges of Primitive
Objective: Primitive Ranges (Objective 4.6)

Q4: Declaring a boolean

4. Which two are valid declarations of a boolean? (Choose two.)


A. boolean b1 = 'false';
B. boolean b2 = 1;
C. boolean b3 = (5 > 7);
D. boolean b4 = Boolean.false();
E. boolean b5 = YES;
F. boolean b6 = false;
Hint: Remember that Java is not like C in this way.
Reference: Chapter 1: Literal Values for Primitives
Objective: Primitive Ranges (Objective 4.6)

Q5: Declare long arrays

5. Given the following,


1. public class Test {
2. public static void main(String [] args) {
3. long [] [] a1;
4. long [] a2 [];
5. a2 = new long[3][];
6. a1 = a2;
7. System.out.println(a1[1][0]);
8. }
9. }
what is the result? (Choose one.)
A. 0
B. null
C. Compilation error at line 5
D. Compilation error at line 6
E. Compilation error at line 7
F. An exception is thrown at runtime
Hint: Multi-dimensional arrays are actually arrays of arrays.
Reference: Chapter 1: Array Reference Assignments for Multi-dimensional Arrays
Objective: Array Construction and Initialization (Objective 1.1)

Q6: Declare and Initialize an array

6. Which one will legally declare, construct, and initialize an array? (Choose one.)
A. int x [] = (1,0,2,0);
B. int [] a = new array(3);
C. int [4] x;
D. int array [] = new int [4];
E. int a [] = new int(4);
F. array a = new int{1,2,3};
Hint: Arrays use [], and array isn't a keyword.
Reference: Chapter 1: Legal and illegal array assignments
Objective: Array construction and initialization (Objective 1.1)

Q7: Array assignment

7. Given the following,


1. public class Test {
2. public static double [] d = new double[3];
3. public static void main(String [] argh) {
4. d[1] = 7;
5. System.out.println(d[0]+d[1]+d[2]);
6. }
7. }
8.
what is the result? (Choose one.)
A. 0
B. 0.0
C. 7
D. 7.0
E. Compilation fails
F. An exception is thrown at runtime
Hint: Remember array defaults
Reference: Chapter 1: Array Instance Variables
Objective: Array Construction and initialization (Objective 1.1)

Q8: Command line arguments

8. Given the following,


1. public class CommandArgs {
2. public static void main(String [] argh) {
3. String [] s = new String[4];
4. s[1] = argh[0];
5. s[2] = argh[1];
6. s[3] = argh[2];
7. System.out.print(s[2]);
8. }
9. }
and the command-line invocation,
java CommandArgs 1 2 3
what is the result?
A. 0
B. 1
C. 2
D. 3
E. Compilation fails
F. An exception is thrown at runtime
Hint: Remember what main()'s argument really is.
Reference: Chapter 1: Command-Line Arguments
Objective: Command-Line Arguments to main (Objective 4.3)

Q9: Restrictive Access Modifier


9. What is the most restrictive method access modifier that will allow classes in the
same package to have access to the method?
A. native
B. public
C. private
D. protected
E. default access
Hint: Not all modifiers are access modifiers.
Reference: Chapter 2: Class Declarations and Modifiers
Objective: Declarations and Modifiers (Objective 1.2)

Q10: Restrictive Access modifiers - 2

10. Which of the following is the most restrictive access modifier that allows a class to
have access to members of another class in the same package? (Choose one.)
A. public
B. static
C. private
D. protected
E. default access
Hint: Think about the order of restrictiveness.
Reference: Chapter 2: Class Declarations and Modifiers
Objective: Declarations and Modifiers (Objective 1.2)

Q11. Import

11. Given the following,


1.
2. public class Test extends java.util.HashMap {
3. public static void main(String [] args) {
4. java.util.HashMap hm;
5. hm = new java.util.HashMap();
6. hm.size();
7. }
8. public int size() {
9. HashSet hs = new HashSet();
10. return 343;
11. }
12. }
which statement, added at line 1, allows the code to compile?
A. No statement is required
B. import java.util;
C. import java.util.Collections;
D. import java.util.Hash*;
E. import java.util.HashMap;
F. import java.util.HashSet;
Hint: What does importing really do?
Reference: Chapter 2: Declarations and Modifiers
Objective: Declarations (Objective 4.1)

Q12: Interface Declarations

12. Which two are valid declarations within an interface? (Choose two.)
A. protected boolean logic = true;
B. abstract short timer = 22;
C. long haul = 23;
D. double [] moveIt(short stack);
E. public static void doMore(long tooth);
F. boolean final doMore(short change);
Hint: Remember the defaults.
Reference: Chapter 2: Declaring an Interface
Objective: Declarations (Objective 4.1)

Q13: Interface Methods

13. Which is the required declaration in a class that implements the java.lang.Runnable
interface?
A. static run()
B. static start()
C. public void run()
D. public void start()
E. void run()
F. void start()
Hint: Remember what happens to a thread when this method ends.
Reference: Chapter 2: Declaring an Interface and Chapter 9 Threads
Objective: Declarations (Objective 4.1)

Q14: Classes and Interfaces

14. Given the following,


1. interface Face1 {
2. int m1(int a1);
3. }
4.
5.
6.
which code fragment inserted at lines 5 and 6 will compile? (Choose one.)
A. class Concrete2 extends Face1 {
public int m1(int r); }
B. interface Concrete2 implements Face1 {
public int m1(int y) { return 7; } }
C. abstract class Concrete2 implements Face1 {
public void m1(int l) { return 7; } }
D. class Concrete2 implements Face1 {
public long m1(int r) { return 7; } }
E. abstract class Concrete2 implements Face1 {
public int m4() { return 7; } }
Hint: Each option may test a different concept.
Reference: Chapter 2: Declaring an Interface
Objective: Declarations (Object 4.1)

Q15: Operators and shorts

15. Given the following,


1. public class Bar {
2. public static void main(String [] args) {
3. short s1 = 32000;
4. short s2 = 31000;
5. short s3 = s1 - s2;
6. System.out.println(s3);
7. }
8. }
what is the output when the code is compiled and executed?
A. 0
B. 1000
C. -1000
D. Compilation fails
E. An exception is thrown at runtime
Hint: What operation is being performed?
Reference: Chapter 3: Equality Operators
Objective: Operators (Objective 5.1)

Q16: Bit Twiddling

16. Given the following,


1. public class Bitwise2 {
2. public static void main(String [] args) {
3. int x = ~10;
4. int y = x ^ 12;
5. System.out.println( y & 7 );
6. }
7. }
what is the result?
A. 0
B. 1
C. 5
D. 14
E. 15
Hint: Remember OR vs. Exclusive OR
Reference: Chapter 3: Bitwise Operators
Objective: Operators (Objective 5.1)

Q17: Shifting

17. Which two are equal? (Choose two.)


A. 67 >> 2;
B. (10 >> 1) << 2;
C. 2 ^ 3;
D. 261 >>> 3;
E. (3 << 2) / (2 << 1);
F. 8 >> 3;
Hint: Remember binary and powers of two.
Reference: Chapter 3: Shift Operators
Objective: Operators (Objective 5.1)

Q18: Operators 2

18. Given the following,


1. public class Test {
2. public static void main(String [] args) {
3. int x= 0;
4. int y= 10;
5. for (int z = 0; z < 4; z++) {
6. if ((++x > 2) || ( y-- > 7 )) {
7. y--;
8. }
9. }
10. System.out.println(y + " " + x);
11. }
12. }
what is the result?
A. 2 4
B. 3 5
C. 4 3
D. 4 4
E. 5 0
F. 5 3
Hint: To short circuit or not short circuit?
Reference: Chapter 3: Short Circuit Logical Operators
Objective: Operators (Objective 5.3)

Q19: Logical Operators


19. Given the following,
1. public class Test {
2. public static void main(String [] args) {
3. boolean b1 = true;
4. boolean b2 = false;
5. boolean b3 = (b1 == false);
6. boolean b4 = ((b2 = false) | (b1 = false));
7. System.out.println(b3 + " " + b4);
8. }
9. }
what is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation error
F. An exception is thrown at runtime
Hint: What operations are really happening here?
Reference: Chapter 3: Logical Operators
Objective: Operators (Objective 5.3)

Q20: Pass array

20. Given the following,


1. public class Test {
2. public static void main(String [] args) {
3. Test t = new Test();
4. t.start();
5. }
6.
7. void start() {
8. long [] a1 = {1,2,3};
9. adjust(a1);
10. System.out.print(a1[2]);
11.
12. }
13.
14. void adjust(long [] a3) {
15. a3[2] = 4;
16. }
17. }
what is the result?
A. 0
B. 2
C. 3
D. 4
E. Compilation fails
F. An exception is thrown at runtime
Hint: What kind of thing is being passed?
Reference: Chapter 3: Passing Object Reference Variables
Objective: Passing Variables (Objective 5.4)

Q21: Pass int

21. Given the following,


1. public class Test {
2. public static void main(String [] args) {
3. Test t = new Test();
4. t.start();
5. }
6.
7. void start() {
8. int x = 7;
9. alter(x);
10. System.out.println(x);
11. }
12.
13. void alter(int x) {
14. x = x + 2;
15. }
16. }
what is the result?
A. 0
B. 2
C. 7
D. 9
E. Compilation fails
F. An exception is thrown at runtime
Hint: What's really being passed?
Reference: Chapter 3: Passing Object Reference Variables
Objective: Passing Variables (Objective 5.4)

Q22: Switch1 (chapter 4)

22. Given the following,


1. public class Switch2 {
2. public static short x = 1;
3. static final int y = 3;
4. public static void main(String [] args) {
5. for (int z=3; z > 0; --z) {
6. switch (z) {
7. case y: System.out.print("0 ");
8. case y-1: System.out.print("1 ");
9. case x: z--;
10. }
11. }
12. }
13. }
what is the result?
A. 0 1
B. 0 1 2
C. Compilation fails at line 7
D. Compilation fails at line 8
E. Compilation fails at line 9
F. An exception is thrown at runtime
Hint: Is this about fall-through logic?
Reference: Chapter 4: switch Statements
Objective: Flow Control (Objective 2.1)

Q23: Switch 2

23. Given the following,


1. public class Switch2 {
2. public static void main(String [] args) {
3. int y = 0;
4.
5. for (int x=3; x >= 0; x--) {
6. switch (x) {
7. case 3: y = y + 100;
8. case 2: y = y + 10;
9. case 1: break;
10. case 0: y = y + 1;
11. }
12. }
13. System.out.println(y);
14. }
15. }
what is the result?
A. 120
B. 121
C. 122
D. 123
E. Compilation fails
F. An exception is thrown at runtime
Hint: How does fall through work?
Reference: Chapter 4: switch Statements
Objective: Flow Control (Objective 2.1)
Q24: Loop 1

24 Given the following,

1. public class While {


2. public void loop() {
3. int x = 4;
4. while ( x ) {
5. x--;
6. }
7. }
8. }
Which statement is true?
A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 4.
C. There are syntax errors on lines 1, 4, and 5.
D. There is a syntax error on line 4.
E. There are syntax errors on lines 4 and 5.
F. There is a syntax error on line 5.
Hint: What are proper loop constructs?
Reference: Chapter 4: Using while loops
Objective: Flow Control (Objective 2.2)

Q25: Loops 3

25 Given the following,


11. int x = 2;
12. for (int y = 2; y > 0; y--) {
13. System.out.print(x + " " + y + " ");
14. x++;
15. }
16. System.out.println(x + " " + y);
what is the result?
A. 2 1 3 0
B. 2 2 3 1
C. 2 1 3 0 4 0
D. 2 2 3 1 4 0
E. Compilation fails
F. An exception is thrown at runtime
Hint: What's a code block?
Reference: Chapter 4: Using break and Continue
Objective: Flow Control (Objective 2.2)

Q26: Exceptions 1
26. Given the following,
1. public class Test {
2. public static void main(String [] args) {
3. try {
4. m1();
5. m2();
6. } catch (Exception e1) {
7. System.out.print(" except ");
8. }
9. }
10.
11. static void m1() throws Exception {
12. try {
13. throw new Exception();
14. } catch (Exception e2) {
15. System.out.print(" m1catch ");
16. }
17. }
18. static void m2() throws Exception {
19. try {
20. throw new Exception();
21. } finally {
22. System.out.print(" m2finally ");
23. }
24. }
25. }

what is the result?


A. m1catch except
B. m1catch except m2finally
C. m1catch m2finally except
D. Compilation fails at line 2
E. Compilation fails at line 21
F. An exception is thrown and not handled at runtime
Hint: Remember handle or declare.
Reference: Chapter 4: Handling Exceptions
Objective: Exceptions ( Objectives 2.3 and 2.4)

Q27: Exceptions 2

27. Given the following,


1. public class Test {
2. public static void m3 () {
3. throw new RuntimeException();
4. }
5.
6. public static void main(String [] args) {
7. try {
8. m3();
9. System.out.print(" done ");
10. }
11. catch (Exception e ) {
12. System.out.print(" except ");
13. }
14.
15. System.out.print(" really done ");
16. }
17. }
what is the output?
A. except really done
B. except done really done
C. RuntimeException except really done
D. RuntimeException except done really done
E. done really done
Hint: Who's handling what?
Reference: Chapter 4: Handling Exceptions
Objective: Exceptions (Objectives 2.3 and 2.4)

Q28: Assert 1

28: Given the following,


1. public class Test {
2. public static int y;
3. public static int foo(int x) {
4. System.out.print("foo ");
5. return y = x;
6. }
7. public static int bar(int z) {
8. System.out.print("bar ");
9. return y = z;
10. }
11. public static void main(String [] args ) {
12. int t = 2;
13. assert t < 4 : bar(7);
14. assert t > 1 : foo(8);
15. System.out.println("done ");
16. }
17. }
what is the result?
A. done
B. bar done
C. foo done
D. bar foo done
F. Compilation fails
G. An AssertionError is thrown at runtime
Hint: Remember the basic assertion rules.
Reference: Chapter 4: Working with Assertions
Objective: Assertions (Objectives 2.5 and 2.6)

Q29: Assertion Facts

29 Which two of the following statements are true? (Choose two.)


A. It is not good practice to place assertions where you think execution should
never reach.
B. It is sometimes appropriate to call getters and setters from assertions.
C. Use assertions to verify the arguments of private methods.
D. Assertions can be disabled for a particular class.
E. Never throw an AssertionError explicitly.
Hint: Review the basic assertion guidelines.
Reference: Chapter 4: Working with Assertions
Objective: Assertions (Objectives 2.5 and 2.6)

Q30: Encapsulate 1

30. Given the following,


1. public class Student {
2. private String name;
3. public String getName() {
4. return name;
5. }
6. public void setName(String n) {
7. name = n;
8. }
9. int age;
10. }
which two are true about the class described above? (Choose two.)
A. Class Student is tightly encapsulated.
B. Line 3 is in conflict with encapsulation.
C. Line 6 is in conflict with encapsulation.
D. Line 9 is in conflict with encapsulation.
E. If age had a public setter the class would be well encapsulated.
F. The access modifier on line 3 does not affect encapsulation.
Hint: What should be private and what should be public?
Reference: Chapter 5: Encapsulation
Objective: Encapsulation (Objective 6.1)

Q31: IS-A 1
31. Given the following,
1.
2. class Wings { }
3. interface Raptor extends Bird {
4. int size = 5;
5. }
6. abstract class Hawk implements Raptor {
7. private int name;
8. void hunt() { ; }
9. }
10. interface Bird {
11. Wings w = new Wings();
12. }
which is true about instances of the classes listed above?
A. Hawk has-a Wings
B. Wings is-a Raptor
C. Class Hawk has two instance variables
D. Concrete subclasses of Hawk must implement a hunt( ) method
Hint: How far does inheritance go?
Reference: Chapter 5: IS-A , HAS-A
Objective: IS-A / HAS-A (Objective 6.1)

Q32: OverXing 1

32. Given the following,


class Foo extends Bar {
public void m1() {
System.out.print("Foo");
}
}
public class Bar {
public static void main(String [] args) {
Bar b = new Foo();
b.m1();
}
public void m1() {
System.out.print("Bar");
}
}
what is the result?
A. Bar
B. Foo
C. FooBar
D. Compilation fails
E. An exception is thrown at runtime
Hint: What kind of a call is b.m1( ); ?
Reference: Chapter 5: Overload and Override
Objective: Overload / Override (Objective 6.2)

Q33: OverXing 2

33. Given the following,


1. class Alpha {
2. int doStuff(float b) {
3. return 7;
4. }
5. }
6.
7. class Beta extends Alpha {
8. // insert code here
9. }
which two methods, if inserted independently at line 8, will compile? (Choose two.)
A. private int doStuff(float y) {return 4; }
B. protected int doStuff(float y) { return 4; }
C. public Integer doStuff(float e) { return 4; }
D. final int doStuff(float y) { return 4; }
E. public long doStuff(float x) { return 4; }
Hint: Remember your overload and override rules.
Reference: Chapter 5: Overload and Override
Objective: Overload / Override (Objective 6.2)

Q34: Constructor 1

2. 34. Given the following,


1. public class Test {
2. Test(int x) { }
3. }
4.
5. class Delta {
6. Delta() { }
7. }
8.
9. class Gamma {
10. Gamma(String s) { }
11. }
which line of code would build an anonymous inner class?
A. Delta d = new Delta(5) { };
B. Delta d = new Delta() { ; }
C. Gamma g = new Gamma() { };
D. Gamma g = new Gamma("bob") { };
E. Test t = new Test(5) { }
F. Test t = new Test{ };
Hint: What are basic constructor rules?
Reference: Chapter 5: Constructors
Objective: Constructors (Objective 1.3)

Q35: Constructor 2

3. 35. Given the following,


1. public class T {
2. public static void main(String [] args) {
3. new T(7L);
4. }
5. public void T(int x) {
6. System.out.print("int ");
7. }
8. public void T(long x) {
9. System.out.print("long ");
10. }
11.
12. public void T() {
13. System.out.print("no-arg ");
14. }
15. }
what is the result?
A. int
B. long
C. no-arg
D. long int no-arg
E. Compilation fails
F. An exception is thrown at runtime
Hint: What's true about constructors?
Reference: Chapter 5: Constructors
Objective: Constructors (Objective 1.3)

Q36: Returns 1

4. 36. Given the following,


14. class Foo {
15. protected short test(float [] y) { return 3; }
16. }
which three of the following lines, could be legally inserted into a class that extends Class
Foo? (Choose three.)
A. public test(float [] y) { return 7; }
B. public int test(float x) { return 7; }
C. public long test(float [] y) { return (short) 7; }
D. short test(float [] x) { return 7; }
E. private short test(float x) { return 7; }
F. protected short test(float [] yahoo) { return 7; }
Hint: Remember all of the return type considerations
Reference: Chapter 5: Return types
Objective: Legal Return Types (Objective 1.4)

Q37: Returns 2

5. 37. Given the following,


1. public class Felix {
2. protected long cat() {
3. return 7L;
4. }
5. }
6. class Oscar extends Felix {
7.
8. }
which method, inserted at line 7, will not compile?
A. private long cat(int x) { return 7; }
B. public long cat() { return 8L; }
C. long cat() { return 8; }
D. protected int cat(int x) { return 7; }
Hint: Check all the rules that might apply
Reference: Chapter 5: Return types
Objective: Return types (Objective 1.4)

Q38: String (ch 6)

38. Given the following,


1. public class TestStr {
2. public static void main(String [] args) {
3. String s1 = "bing ";
4. String s2 = "bang ";
5. s1 = s1.concat("boom ");
6. String s3 = "bamm ";
7. s3.concat("bamm ");
8. System.out.println(s1 + s2 + s3);
9. }
10. }
what is the result?
A. bing boom bang bamm
B. boom bing bang bamm bamm
C. bing boom bang bamm bamm
D. Compilation fails.
E. An exception is thrown at runtime.
Hint: Are references immutable?
Reference: Chapter 6: Strings
Objective: Strings (Objective 8.2)

Q39: String 2

39. Given the following,


4. String d = "iko ";
5. d += d;
6. d += "ahnay ";
7. d.concat("shockamal ");
8. d.toUpperCase();
9. System.out.println(d);
what is the result?
A. iko
B. iko iko
C. iko iko ahnay
D. iko iko anhay shockamal
E. IKO IKO AHNAY SHOCKAMAL
F. An exception is thrown at runtime.
Hint: Remember your signatures.
Reference: Chapter 6: Strings
Objective: Strings (Objective 8.2)

Q40: Round

40. Given the following,


1. int i1 = (int)Math.ceil(Math.sqrt(15));
2. int i2 = (int)Math.floor(Math.max(5.4,5.7));
3. int i3 = (int)Math.sqrt(Math.abs(-23));
4. int i4 = (int)Math.round(Math.min(4.1,4.004));
5. int i5 = (int)Math.abs(Math.floor(-3.8));
what result is different from all the others?
A. i1
B. i2
C. i3
D. i4
E. i5
Hint: The result of the rightmost Math method is the argument for the leftmost
Reference: Chapter 6: Math methods
Objective: Math class (Objective 8.1)

Q41: Degrees

41. Given that the cosine of 57 degrees is equal to the sine of 33 degrees, which of the
following two lines would be roughly equal?
A. Math.sin(33);
B. Math.cos(57);
C. Math.toDegrees(Math.sin(33));
D. Math.toDegrees(Math.cos(57));
E. Math.sin(Math.toRadians(33));
F. Math.cos(Math.toRadians(57));
Hint: What do the trig functions expect?
Reference: Chapter 6: Math methods
Objective: Math class (Objective 8.1)

Q42: Wrapper1

42. Given the following,


1. public class NFE {
2. public static void main(String [] args) {
3. String s = "34.";
4. try {
5. s += 7;
6. double d = Double.valueOf(s);
7. s = Double.toString(d);
8. int x = (int) (Double.valueOf(s).doubleValue());
9. System.out.println(x);
10. }
11. catch (NumberFormatException e) {
12. System.out.println("bad number");
13. }
14. }
15. }
what is the result?
A. 34
B. 34.7
C. 35
D. bad number
E. Compilation fails.
F. An uncaught exception is thrown at runtime.
Hint: How do you evaluate chained methods?
Reference: Chapter 6: Wrappers
Objective: Wrappers (Objective 8.3)

Q43: Wrapper 2

43. Given the following,


2. try {
3. String y = Double.toString(Math.sqrt(23));
4. Long x = Long.valueOf(y);
5. System.out.println(x);
6. } catch (Exception e) {
7. System.out.println("exception");
8. }
what is the result?
A. 4
B. 5
C. exception
D. Compiler error at line 3
E. Compiler error at line 4
Hint: Remember your method signatures.
Reference: Chapter 6: Wrappers
Objective: Wrappers (Objective 8.3)

Q44: Equals 1

44. Given the following,


1. public class Test2 {
2. public static void main(String [] args) {
3. Test2 t1 = new Test2();
4. Test2 t2 = new Test2();
5. Test2 t3 = t1;
6.
7. System.out.println(t1.equals(t3));
8. System.out.println(t1.equals(t2));
9. }
10. }
what is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation fails
F. An exception is thrown at runtime
Hint: Was equals( ) overridden?
Reference: Chapter 6: equals( )
Objective: equals( ) (Objective 5.2)

Q45: Equals 2

45. If x and y are reference variables for wrapper objects, which statement is true
A. If x.equals(y) is true then x == y must be true.
B. If x.equals(y) is false then x == y may be true.
C. If x.equals(y) is true then x == y may be false.
D. If x.equals(y) is true then x and y may be of different classes.
E. If x == y is false then x.equals(y) must be false.
Hint: Have the wrapper class's equals() methods been overridden?
Reference: Chapter 6: equals( )
Objective: equals( ) (Objective 5.2)

Q46: Hashcode 1 (ch 7)

46. Given the following,


11. x = 0;
12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;
13. if (x3.equals(x4) == false ) x = x + 10;
14. if (x5.equals(x6) == true ) x = x + 100;
15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
16. System.out.println("x = " + x);
if the output is "x = 100", which of the following statements will always be true?
A. x2.equals(x1) == true
B. x3.hashCode() != x4.hashCode()
C. x5.hashCode() == x6.hashCode()
D. x8.equals(x7) == true
Hint: If objects aren't equal can their hashcodes be?
Reference: Chapter 7: Overriding hashcode( )
Objective: hashcode : (Objective 9.2)

Q47: Hashcode 2

2. If the hashCode() method has been overridden, what other method must be overridden?
A. notify()
B. finalize()
C. toString()
D. equals()
E. clone()
Hint: Remember the hashcode contract.
Reference: Chapter 7: Overriding hashcode( )
Objective: hashcode (Objective 9.2)

Q48: Collection 1

48. Which of these collection classes allows you to access its elements in a predetermined
order, and assures you that there will be no duplicated elements in the collection?
A.) java.util.Vector
B.) java.util.TreeSet
C.) java.util.TreeMap
D.) java.util.LinkedList
E.) java.util.ArrayList
F.) java.util.HashSet
Hint: Which interfaces provide which capabilities?
Reference: Chapter 7: collections
Objective: Collections (Objective 9.1)

Q49: Collection 2

49. Which of these collection classes is the best suited to implement a stack, and does not
have the overhead of synchronized methods?
A. java.util.TreeMap
B. java.util.TreeSet
C. java.util.LinkedList
D. java.util.Vector
E. java.util.LinkedHashMap
Hint: What defines a list, a set, or a map?
Reference: Chapter 7: collections
Objective: Collections (Objective 9.1)

Q50: GC 1

50. Given the following,


1. public class X {
2. public static void main(String [] args) {
3. X x1 = m1();
4. m1();
5. X x3 = new X();
6. x3 = x1;
7. //do Complex Stuff
8. }
9. static X m1() {
10. X mx = new X();
11. return mx;
12. }
13. }
After line 6 runs, how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. Not possible to know for sure
Hint: Java is pass by value.
Reference: Chapter 7: garbage collection
Objective: GC (Objectives 3.1, 3.2, 3.3)

Q51: GC 2

51.Which statement is true?


A. Stack reference variables will be removed from the stack by the garbage collector
when they no longer refer to a live object.
B. The garbage collector guarantees that a program will never run out of memory.
C. If an object will never again be used it becomes eligible for garbage collection.
D. Objects instantiated within a constructor are placed in the garbage collectible heap.
E. All objects' finalize() methods will be called before a Java program ends.
Hint: Remember the GC's guarantees.
Reference: Chapter 7: garbage collection
Objective: GC (Objectives 3.1, 3.2, 3.3)

Q52: GC 3

52. Given the following, and that class X looks like this: class X { }
12. void doSomething() {
13. X x = doStuff( new X() );
14. X y = doStuff(x);
15. x = null;
16. y = null;
17. }
18. X doStuff(X mx) {
19. return doStuff2(mx);
20. }
21. X doStuff2(X m2) {
22. X z = new X();
22. return z;
20. }
at what point is the object created in line 13 eligible for garbage collection?
A. After line 13 runs
B. After line 14 runs
C. After line 15 runs
D. After line 16 runs
E. The object is not eligible.
F. It is not possible to know for sure.
Hint: Any reference variables left?
Reference: Chapter 7: garbage collection
Objective: GC (Objectives 3.1, 3.2, 3.3)

Q53: Static Inner(ch 8)

53. Given the following,


1. public class Beta {
2. long x = 42;
3.
4.
5. }
6. }
which two code fragments, inserted independently, can be legally inserted at lines 3 and
4?
A. class Alpha {
public long m1() { return x; }
B. protected class Gamma {
static long m1() { return 7L; }
C. public static class Alpha {
long m1() { return 7L; }
D. static class Gamma {
long m1() { return x; }
Hint: How does static affect this question?
Reference: Chapter 8: static inner
Objective: Inner class (Objectives 4.1 and 6.3)

Q54: Inner Facts

54. Which two statements are true? (Choose two.)


A. An anonymous inner class can implement multiple interfaces.
B. A static nested class cannot access non-static members of the enclosing class.
C. A non-static, method-local inner class must be marked final.
D. A method-local, anonymous inner class cannot access non-final local variables.
E. A method-local, anonymous inner class cannot access static members of the enclosing
class.
Hint: What's a key point for inner classes?
Reference: Chapter 8: static inner
Objective: Inner class (Objectives 4.1 and 6.3)

Q55: Anon 1

55. Which constructs an anonymous inner class instance?


A. Runnable r = new Runnable() {public void run(){ }};
B. Runnable r = new Runnable((public void run() { }));
C. Runnable r = new Runnable() { };
D. System.out.println(new Runnable {public void run() { }});
E. Runnable r = new Runnable ( public void run(){});
Hint: Remember what you are implementing.
Reference: Chapter 8: Anonymous
Objective: Inner class (Objectives 4.1 and 6.3)

Q56: Anon 2

56. Given the following,


1. public class Trick {
2. public static void main(String [] args) {
3. class Treat {
4. protected String name;
5. }
6. Treat t = new Treat() { };
7. t.name = "bob";
8. System.out.println(t.name);
9. }
10. }
what is the result?
A. bob
B. null
C. Compiler error at line 3
D. Compiler error at line 5
E. Compiler error at line 6
Hint: Is this really about inner classes?
Reference: Chapter 8: Anonymous
Objective: Inner class (Objectives 4.1 and 6.3)

Q57: Inner Facts 2

57. Which two statements are true? (Choose two.)


A. A non-static, method-local inner class can be marked abstract.
B. A method-local, anonymous inner class can be declared protected.
C. A method-local, inner class must be marked final.
D. A non-static, method-local inner class can access all members of the enclosing class.
E. A method-local anonymous inner class cannot extend an abstract class.
Hint: In what ways are inner classes normal?
Reference: Chapter 8: method local
Objective: Inner class (Objectives 4.1 and 6.3)

Q58: Anon 3

58. Given the following,


1. public class Animal {
2. int size = 42;
3. public static void main (String [] args) {
4. class Dog extends Animal {
5. public String name = "fido";
6. int size = 37;
7. }
8. Animal a = new Dog();
9. System.out.println(a.size);
10. }
11. }
what is the result?
A. 0
B. 37
C. 42
D. Compilation fails because of an error on line 4.
E. Compilation fails because of an error on line 8.
F. An exception is thrown at runtime.
Hint: What's the same about inner classes?
Reference: Chapter 8: Anonymous
Objective: Inner class (Objectives 4.1 and 6.3)

Q59: Thread 1(ch 9)

59. Given the following,

1. public class ThreadTest {


2. class InnerRun implements Runnable {
3. public void run() {
4. // do Slow Stuff
5. System.out.print("Ren ");
6. }
7. }
8. class InnerRunTwo implements Runnable {
9. Thread other;
10. InnerRunTwo(Thread t) { other = t; }
11. public void run() {
12. other.join();
13. System.out.print("Stimpy ");
14. }
15. }
16. void start() {
17. InnerRun ir = new InnerRun();
18. Thread t = new Thread(ir);
19. InnerRunTwo irr = new InnerRunTwo(t);
20. Thread u = new Thread(irr);
21. t.start();
22. u.start();
23. }
24. public static void main (String[] args) {
25. ThreadTest tt = new ThreadTest();
26. tt.start();
27. }
28. }
what is the result?
A. Ren Stimpy
B. Stimpy Ren
C. Compilation fails
D. The output cannot be determined
E. An exception is thrown at runtime
Hint: You can't be too careful.
Reference: Chapter 9: Starting threads
Objective: Threads (Objective 7.1)
Q60: Thread 2

60. Which of the following methods is not defined in class Thread?


A. run()
B. join()
C. start()
D. sleep()
E. notify()
Hint: What's in class Object?
Reference: Chapter 9: Starting threads
Objective: Threads (Objective 7.1)

Q61: Thread 3

61. Given the following,

1. public class ThreadTest {


2. class InnerRun implements Runnable {
3. public void run() {
4. for (int x = 0; x < 100; x++) {
4.5 // do Slow Stuff
5. System.out.print("Ren "); }
6. }
7. }
8. class InnerRunTwo implements Runnable {
9. Thread other;
10. InnerRunTwo(Thread t) { other = t; }
11. public void run() {
11.5 try {
12. other.join();
12.5 } catch (Exception e) { }
12.7 for (int x = 0; x < 100; x++)
13. System.out.print("Stimpy ");
14. }
15. }
16. void start() {
17. InnerRun ir = new InnerRun();
18. Thread t = new Thread(ir);
19. InnerRunTwo irr = new InnerRunTwo(t);
20. Thread u = new Thread(irr);
21. t.start();
22. u.start();
23. }
24. public static void main (String[] args) {
25. ThreadTest tt = new ThreadTest();
26. tt.start();
27. }
28. }
what is the result of this code?
A. Ren Ren Ren... ...Stimpy Stimpy Stimpy
B. Stimpy Stimpy Stimpy... ...Ren Ren Ren
C. The output cannot be determined
D. Compilation fails
E. An exception is thrown at runtime
Hint: Who's joining whom?
Reference: Chapter 9: Starting threads
Objective: Threads (Objective 7.1)

Q62: Thread 4

62. Which two do not cause a thread to stop being the currently running thread? (Choose
two.)
A. Calling the join() method on a live thread
B. Calling the sleep() method.
C. Calling the wait() method.
D. Calling the notify() method.
E. Entering an unlocked, synchronized block of code.
Hint: Who is making the call?
Reference: Chapter 9: Threads
Objective: Stopping threads (Objective 7.2)

Q63: Thread 5

63. Given the following:

1. public class ThreadTest {


2. class InnerRun implements Runnable {
3. public void run() {
4. for (int x = 0; x < 100; x++) {
4.5 try {
4.6 Thread.sleep(5);
4.7 } catch (Exception e) { }
5. System.out.print("Ren "); }
6. }
7. }
8. class InnerRunTwo implements Runnable {
9. Thread other;
10. InnerRunTwo(Thread t) { other = t; }
11. public void run() {
11.5 try {
12. other.join();
12.2 } catch (Exception e) { }
12.3 for (int x = 0; x < 100; x++) {
12.4 try {
12.5 Thread.sleep(5);
12.6 } catch (Exception e) { }
13. System.out.print("Stimpy "); }
14. }
15. }
16. void start() {
17. InnerRun ir = new InnerRun();
18. Thread t = new Thread(ir);
19. InnerRunTwo irr = new InnerRunTwo(t);
20. Thread u = new Thread(irr);
21. t.start();
22. u.start();
23. }
24. public static void main (String[] args) {
25. ThreadTest tt = new ThreadTest();
26. tt.start();
27. }
28. }
what is the result?
A. Ren Ren Ren... ...Stimpy Stimpy Stimpy
B. Stimpy Stimpy Stimpy... ...Ren Ren Ren
C. A random mixture of Ren's and Stimpy's
D. The output cannot be determined
E. Compilation error
F. An exception is thrown at runtime
Hint: Is this a question of speed?
Reference: Chapter 9: Thread Interaction
Objective: Threads (Objective 7.2)

Q64: Locks

63. (Should be 64) Which statement is true?


A. A thread must own the lock on the current thread before it can call wait().
B. A thread must own the lock on the current thread before it can call notify().
C. A thread must own the lock on the object whose wait() method is to be called.
D. A thread must own the lock on the object whose yield() method is to be called.
E. A thread must own the lock on the object whose join() method is to be called
Hint: Who locks whom?
Reference: Chapter 9: wait notify
Objective: Threads (Objective 7.3)

Q65: Wait/Notify2
64. (should be 65) Which two statements are true? (Choose two.)
A. The notify() method can take a thread argument.
B. The wait() method causes another thread to stop executing.
C. The wait() method can take a long argument.
D. The wait() method can be called from a non-synchronized context.
E. The notify() method does not release an object's lock.
Hint: How do wait and notify work with separate threads?
Reference: Chapter 9: Preventing Execution
Objective: Threads (Objective 7.4)