Self Test


1. 

Given the following code:

 public class OrtegorumFunction {   public int computeDiscontinuous(int x) {     int r = 1;     r += X;     if ((x > 4) && (x < 10)) {       r += 2 * x;     } else (x <= 4) {       r += 3 * x;     } else {       r += 4 * x;     }     r += 5 * x;     return r;   }   public static void main(String [] args) {     OrtegorumFunction o = new OrtegorumFunction();     System.out.println("OF(11) is: " + o.computeDiscontinuous (11));   } } 

What is the result?

  1. OF(11) is: 45

  2. OF(11) is: 56

  3. OF(11) is: 89

  4. OF(11) is: 111

  5. Compilation fails.

  6. An exception is thrown at runtime.

image from book

2. 

Given two files:

 1. class One { 2.   public static void main(String[] args) { 3.     int assert = 0; 4.   } 5. } 1. class Two { 2.   public static void main(String[] args) { 3.      assert(false); 4.    } 5. } 

And the four command-line invocations:

 javac -source 1.3 One.java javac -source 1.4 One.java javac -source 1.3 Two.java javac -source 1.4 Two.java 

What is the result? (Choose all that apply.)

  1. Only one compilation will succeed.

  2. Exactly two compilations will succeed.

  3. Exactly three compilations will succeed.

  4. All four compilations will succeed.

  5. No compiler warnings will be produced.

  6. At least one compiler warning will be produced.

image from book

3. 

Given:

 import java.io.*; class Master {   String doFileStuff() throws FileNotFoundException { return "a"; } } class Slave extends Master {   public static void main(String[] args) {     String s = null;     try { s = new Slave().doFileStuff();     } catch ( Exception x) {       s = "b"; }     System.out.println(s);   }   // insert code here } 

Which, inserted independently at // insert code here, will compile, and produce the output b? (Choose all that apply.)

  1. String doFileStuff() { return "b"; }

  2. String doFileStuff() throws IOException ( return "b"; }

  3. String doFileStuff(int. x) throws IOException ( return "b"; }

  4. String doFileStuff() throws FileNotFoundException { return "b"; }

  5. String doFileStuff() throws NumberFormatException { return "b"; }

  6. String doFileStuff() throws NumberFormatException, FileNotFoundException { return "b"; }

image from book

4. 

Given:

 class Input {   public static void main(String[] args) {     String s = "- " ;     try {       doMath(args[0]);       s += "t ";        // line 6     }     finally { System.out.println(s += "f "); }   }   public static void doMath(String a) {     int y = 7 / Integer.parseInt(a); } } 

And the command-line invocations:

 java Input java Input 0 

Which are true? (Choose all that apply.)

  1. Line 6 is executed exactly 0 times.

  2. Line 6 is executed exactly 1 time.

  3. Line 6 is executed exactly 2 times.

  4. The finally block is executed exactly 0 times.

  5. The finally block is executed exactly 1 time.

  6. The finally block is executed exactly 2 times.

  7. Both invocations produce the same exceptions.

  8. Each invocation produces a different exception.

image from book

5. 

Given:

 1. class Crivitch { 2.   public static void main(String [] args) { 3.     int x = 0; 4.     // insert code here 5.     do { } while (x++ < y); 6.     System.out.println(x); 7.   } 8. } 

Which, inserted at line 4, produces the output 12?

  1. int y = x;

  2. int y = 10;

  3. int y = 11;

  4. int y = 12;

  5. int y = 13;

  6. None of the above will allow compilation to succeed.

image from book

6. 

Given:

 class Plane {   static String s = "-";   public static void main(String[] args) {     new Plane().s1() ;     System.out.println(s);   }   void sl() {     try { s2();     catch (Exception e) { s += "c"; }   }   void s2() throws Exception  {     s3();  s += "2";     s3();  s += "2b";   }   void s3() throws Exception {     throw new Exception();   } } 

What is the result?

  1. -

  2. -c

  3. -c2

  4. -2c

  5. -c22b

  6. -2c2b

  7. -2c2bc

  8. Compilation fails.

image from book

7. 

Given:

 try { int x = Integer.parselnt("two"); } 

Which could be used to create an appropriate catch block? (Choose all that apply.)

  1. ClassCastException

  2. IllegalStateException

  3. NumberFormatException

  4. IllegalArgumentException

  5. ExceptionInInitializerError

  6. ArrayIndexOutOfBoundsException

image from book

8. 

Given:

  1. class Ping extends Utils {  2.   public static void main(String [] args) {  3.     Utils u = new Ping();  4.     System.out.print(u.getInt(args[0]));  5.   }  6.   int getInt(String arg) {  7.     return Integer.parseInt(arg);  8.   }  9. } 10. class Utils { 11.   int getInt(String x) throws Exception { return 7; } 12. } 

And the following three possible changes:

  • C1. Declare that main() throws an Exception.

  • C2. Declare that Ping.getInt() throws an Exception.

  • C3. Wrap the invocation of getInt() in a try / catch block.

Which change(s) allow the code to compile? (Choose all that apply.)

  1. Just C1 is sufficient.

  2. Just C2 is sufficient.

  3. Just C3 is sufficient.

  4. Both C1 and C2 are required.

  5. Both C1 and C3 are required.

  6. Both C2 and C3 are required.

  7. All three changes are required.

image from book

9. 

Given:

 class Swill {   public static void main(String[] args) {     String s = "-";     switch(TimeZone.CST) {       case EST: s += "e";       case CST: s += "c";       case MST: s += "m";       default:  s += "X";       case PST: s += "p";     }     System.out.println(s);   } } enum TimeZone {EST, CST, MST, PST } 

What is the result?

  1. -c

  2. -X

  3. -cm

  4. -cmp

  5. -cmXp

  6. Compilation fails.

  7. An exception is thrown at runtime.

image from book

10. 

Given:

 class Circus {   public static void main(String[] args) {     int x = 9;     int y = 6;     for(int z = 0; z < 6; z++, y--) {       if(x > 2)  x--;       label:         if(x > 5) {           System.out.print(x + " "};           --X;           continue label;            }          X--;       }    } } 

What is the result?

  1. 8

  2. 8 7

  3. 8 7 6

  4. Compilation fails.

  5. An exception is thrown at runtime.

image from book

11. 

Which are true? (Choose all that apply.)

  1. It is appropriate to use assertions to validate arguments to methods marked public.

  2. It is appropriate to catch and handle assertion errors.

  3. It is NOT appropriate to use assertions to validate command-line arguments.

  4. It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.

  5. It is NOT appropriate for assertions to change a program's state.

image from book

12. 

Given:

 1. class Loopy { 2.   public static void main(String[] args) { 3.     int[] x = {7,6,5,4,3,2,1}; 4.     // insert code here 5.       System.out.print(y + " "); 6.     } 7.   } 8. } 

Which, inserted independently at line 4, compiles? (Choose all that apply.)

  1. for(int y : x) {

  2. for(x : Int y) {

  3. int y = 0; for(y : x) {

  4. for(int y=0, z=0; z<x.length; z++) { y = x[z];

  5. for(int y=0, int z=0, int z=0; z<x.length; z++) { y = x[z];

  6. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];

image from book

13. 

Given:

  1. class Ring {  2.   final static int x2 = 7;  3.   final static Integer x4 = 8;  4.   public static void main(String[] args) {  5.     Integer x1 = 5;  6.     String s = "a";  7.     if(xl < 9) s += "b";  8.     switch(x1) {  9.       case 5:  s += "c"; 10.       case x2: s += "d"; 11.       case x4: s += "e"; 12.     } 13.     System.out.println(s); 14.   } 15. } 

What is the result?

  1. abc

  2. abcde

  3. Compilation fails due only to an error on line 7.

  4. Compilation fails due only to an error on line 8.

  5. Compilation fails due only to an error on line 10.

  6. Compilation fails due only to an error on line 11.

  7. Compilation fails due to errors on multiple lines.

image from book

14. 

Given:

 class Emu {   static String s = "-";   public static void main(String[] args) {     try {       throw new Exception();     } catch (Exception e) {         try {           try { throw new Exception();           } catch (Exception ex) { s += "ic "; }           throw new Exception(); }         catch (Exception x) { s += "mc "; }         finally { s += "mf "; }     } finally { s += "of "; }     System.out.println(s); } } 

What is the result?

  1. -ic of

  2. -mf of

  3. -mc mf

  4. -ic mf of

  5. -ic mc mf of

  6. -ic mc of mf

  7. Compilation fails.

image from book

15. 

Given:

 class Mineral { } class Gem extends Mineral { } class Miner {   static int x = 7;   static String s = null;   public static void getWeight(Mineral m) {     int y = 0 / x;     System.out.print(s + " ");   }   public static void main(String[] args) {     Mineral[] ma = {new Mineral(), new Gem()};    for(Object o : ma)      getWeight((Mineral) o);   } } 

And the command-line invocation:

 java Miner.java 

What is the result?

  1. null

  2. null null

  3. A ClassCastException is thrown.

  4. A NullPointerException is thrown.

  5. A NoClassDefFoundError is thrown.

  6. An ArithmeticException is thrown.

  7. An IllegalArgumentException is thrown.

  8. An ArrayIndexOutOfBoundsException is thrown.

image from book

16. 

Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)

  1. ClassCastException

  2. IllegalStateException

  3. NumberFormatException

  4. IllegalArgumentException

  5. ExceptionInInitializerError

image from book

Answers

1. 

þ  

E is correct. The if statement is illegal. The if-else-else must be changed to if-else if-else, which would result in OF (11) is: 111.

ý  

A, B, C, D, and F are incorrect based on the above. (Objective 2.1)

2. 

þ  

B and F are correct. Class One will compile (and issue a warning), using.the 1.3 flag, and class Two will compile using the 1.4 flag.

ý  

A, C, D, and E are incorrect based on the above. (Objective 2..3)

3. 

þ  

A, D, E, and F are correct. It's okay for an overriding method to throw the same exceptions, narrower exceptions, or no exceptions. And it's okay for the overriding method to throw any runtime exceptions.

ý  

B is incorrect, because the overriding method is trying to throw a broader exception. C is incorrect. This method doesn't override, so the output is a. (Objective 2.4)

4. 

þ  

A, F, and H are correct. Since both invocations throw exceptions, line 6 is never reached. Since both exceptions occurred within a try block, the finally block will always.execute. The first invocation throws an ArrayIndexOutOf BoundsException, and the second invocation throws an ArithmeticException for the attempt to divide by zero.

ý  

B, C, D, E, and G are incorrect based on the. above. (Objective 2.5)

5. 

þ  

C is correct. x reaches the value of 11, at which point the while test fails. x is then incremented (after the comparison test!), and the println() method runs.

ý  

A, B, D, E, and F are incorrect based on the above. (Objective 2.2)

6. 

þ  

B is correct. Once s3() throws the exception to s2(), s2() throws it to s1(), and no more of s2()'s code will be executed.

ý  

A, C, D, E, F, G, and H are incorrect based on the above. (Objective 2.5)

7. 

þ  

C and D are correct. Integer.parseInt can throw a NumberFormatException, and IllegalArgumentException is its superclass (i.e., a broader exception).

ý  

A, B, E, and F are not in NumberFormatException's class hierarchy. (Objective 2.6)

8. 

þ  

A and C are correct. Remember that line 4 is making a polymorphic call so the compiler knows that an exception might be thrown. If C1 is implemented the exception has been sufficiently declared, and if C3 is implemented the exception has been sufficiently handled. C2 is not necessary in either case.

ý  

B, D, E, F, and G are incorrect based on the above. (Objective 2.4)

9. 

þ  

E is correct. It's legal to use enums in a switch, and normal switch fall-through logic applies; i.e., once a match is made the switch has been entered, and all remaining blocks will run if no break statement is encountered. Note: default doesn't have to be last.

ý  

A, B, C, D, and F are incorrect based on the above. (Objective 2.1)

10. 

þ  

D is correct. A labeled continue3E works only with loops. In this case, although the label is legal, label is not a label on a loop statement, it's a label on an if statement.

ý  

A, B, C, and E are incorrect based on the above. (Objective 2.2)

11. 

þ  

C, D, and E are correct statements.

ý  

A is incorrect. It is acceptable to use assertions to test the arguments of private methods. B is incorrect. While assertion errors can be caught, Sun discourages you from doing so. (Objective 2.3)

12. 

þ  

A, D, and E are correct. A is an example of the enhanced for loop. D and F are examples of the basic for loop.

ý  

B is incorrect because its operands are swapped. C is incorrect because the enhanced for must declare its first operand. E is incorrect syntax to declare two variables in a for statement. (Objective 2.2)

13. 

þ  

F is correct. A switch statement requires its case expressions to be constants, and wrapper variables (even final static ones) aren't considered constants, The rest of the code is correct.

ý  

A, B, C, D, E, and G are incorrect based on the above. (Objective 2.1)

14. 

þ  

E is correct. There is no problem nesting try / catch blocks. As is normal, when an exception is thrown, the code in the catch block runs, then the code in the finally block runs.

ý  

A, B, C, D, and F are incorrect based on the above. (Objective 2.5)

15. 

þ  

E is correct. The invocation should be java Miner, in which case null null would be produced.

ý  

A, B, C, D, F, G, and H are incorrect based on the above. (Objective 2.6)

16. 

þ  

B, C, and D are correct. B is typically used to report an environment problem such as trying to access a resource that's closed. C is often thrown in API methods that attempt to convert poorly formed String arguments to numeric values. D is often thrown in API methods that receive poorly formed arguments.

ý  

A and E are thrown by the JVM. (Objective 2.6)




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net