7 dic 2012

Preparación para Certificación SCJP 1Z0-851. Tema 3

Continuando con esta "saga" de posts sobre la preparación para realizar el examen de certificación de java SCJP 1Z0-851.

Question 1
Given:

1.  class Scoop {

2.     static int thrower() throws Exception { return 42; }

3.     public static void main(String [] args) {

4.        try {

5.           int x = thrower();

6.        } catch (Exception e) {

7.           x++;

8.        } finally {

9.        System.out.println("x = " + ++x);

10. } } }

What is the result?
    a. x = 42    
    b. x = 43    
    c. x = 44    
    d. Compilation fails.    
    e. The code runs with no output.    

Answer: D

Question 2
Which are true? (Choose all that apply.)
    a. It is appropriate to use assertions to validate arguments to methods marked public.    
    b. It is appropriate to catch and handle assertion errors.    
    c. It is NOT appropriate to use assertions to validate command-line arguments.    
    d. It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.    
    e. It is NOT appropriate for assertions to change a program    

Answer: C, D and E

Question 3
Given:

1.  class Emu {

2.    static String s = "-";

3.    public static void main(String[] args) {

4.      try {

5.        throw new Exception();

6.      } catch (Exception e) {

7.        try {

8.          try { throw new Exception();

9.          } catch (Exception ex) { s += "ic "; }

10.         throw new Exception(); }

11.       catch (Exception x) { s += "mc "; }

12.       finally { s += "mf "; }

13.    } finally { s += "of "; }

14.      System.out.println(s);

15. } }

What is the result?
Seleccione una respuesta.
    a. -ic of    
    b. -mf of    
    c. -mc mf    
    d. -ic mf of    
    e. -ic mc mf of    
    f. -ic mc of mf    
    g. Compilation fails.    

Answer: E

Question 4
Given:

1.  class Mineral { }

2.  class Gem extends Mineral { }

3.  class Miner {

4.    static int x = 7;

5.    static String s = null;

6.    public static void getWeight(Mineral m) {

7.      int y = 0 / x;

8.      System.out.print(s + " ");

9.    }

10.   public static void main(String[] args) {

11.     Mineral[] ma = {new Mineral(), new Gem()};

12.     for(Object o : ma)

13.       getWeight((Mineral) o);

14.   }

15. }

And the command-line invocation:

java Miner.java

What is the result?
Seleccione una respuesta.
    a. null    
    b. null null    
    c. A ClassCastException is thrown.    
    d. A NullPointerException is thrown.    
    e. A NoClassDefFoundError is thrown.    
    f. An ArithmeticException is thrown.    
    g. An IllegalArgumentException is thrown.    
    h. An ArrayIndexOutOfBoundsException is thrown.    

Answer: E

Question 5
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.)
    a. ClassCastException    
    b. IllegalStateException    
    c. NumberFormatException    
    d. IllegalArgumentException    
    e. ExceptionInInitializerError    

Answer: B, C and D

6
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.)
    a. Only one compilation will succeed.    
    b. Exactly two compilations will succeed.    
    c. Exactly three compilations will succeed.    
    d. All four compilations will succeed.    
    e. No compiler warnings will be produced.    
    f. At least one compiler warning will be produced.    

Answer: B and F

Question 7
Given:

1.  import java.io.*;

2.  class Master {

3.    String doFileStuff() throws FileNotFoundException { return "a"; }

4.  }

5.  class Slave extends Master {

6.    public static void main(String[] args) {

7.      String s = null;

8.      try { s = new Slave().doFileStuff();

9.      } catch ( Exception x) {

10.        s = "b"; }

11.     System.out.println(s);

12.   }

13.   // insert code here

14. }

Which, inserted independently at

// insert code here

, will compile, and produce the output b? (Choose all that apply.)
    a. String doFileStuff() { return "b"; }    
    b. String doFileStuff() throws IOException { return "b"; }    
    c. String doFileStuff(int x) throws IOException { return "b"; }    
    d. String doFileStuff() throws FileNotFoundException { return "b"; }    
    e. String doFileStuff() throws NumberFormatException { return "b"; }    
    f. String doFileStuff() throws NumberFormatException, FileNotFoundException { return "b"; }    

Answer: A, D, E, F

Question 8
Given:

1.  class Input {

2.    public static void main(String[] args) {

3.      String s = "-";

4.      try {

5.        doMath(args[0]);

6.        s += "t "; // line 6

7.      }

8.      finally { System.out.println(s += "f "); }

9.    }

10.   public static void doMath(String a) {

11.     int y = 7 / Integer.parseInt(a);

12.   }

13. }

And the command-line invocations:

java Input

java Input 0

Which are true? (Choose all that apply.)
    a. Line 6 is executed exactly 0 times.    
    b. Line 6 is executed exactly 1 time.    
    c. Line 6 is executed exactly 2 times.    
    d. The finally block is executed exactly 0 times.    
    e. The finally block is executed exactly 1 time.    
    f. The finally block is executed exactly 2 times.    
    g. Both invocations produce the same exceptions.    
    h. Each invocation produces a different exception.    

Answer: A, F and H

Question 9
Given:

1.  class Plane {

2.    static String s = "-";

3.    public static void main(String[] args) {

4.      new Plane().s1();

5.      System.out.println(s);

6.    }

7.    void s1() {

8.      try { s2(); }

9.      catch (Exception e) { s += "c"; }

10.   }

11.   void s2() throws Exception {

12.     s3(); s += "2";

13.     s3(); s += "2b";

14.   }

15.   void s3() throws Exception {

16.     throw new Exception();

17.   } }

What is the result?
    a. -    
    b. -c    
    c. -c2    
    d. -2c    
    e. -c22b    
    f. -2c2b    
    g. -2c2bc    
    h. Compilation fails.    

Answer: B

Question 10
Given:

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

Which could be used to create an appropriate catch block? (Choose all that apply.)
    a. ClassCastException    
    b. IllegalStateException    
    c. NumberFormatException    
    d. IllegalArgumentException    
    e. ExceptionInInitializerError    
    f. ArrayIndexOutOfBoundsException

Answer: C and D

11
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.)
Seleccione al menos una respuesta.
    a. Just C1 is sufficient.    
    b. Just C2 is sufficient.    
    c. Just C3 is sufficient.    
    d. Both C1 and C2 are required.    
    e. Both C1 and C3 are required.    
    f. Both C2 and C3 are required.    
    g. All three changes are required.    

Answer: A and C

Question 12
Given:

1.  class Swill {

2.    public static void main(String[] args) {

3.      String s = "-";

4.      switch(TimeZone.CST) {

5.        case EST: s += "e";

6.        case CST: s += "c";

7.        case MST: s += "m";

8.        default: s += "X";

9.        case PST: s += "p";

10.     }

11.     System.out.println(s);

12.   }

13. }

14. enum TimeZone {EST, CST, MST, PST }

What is the result?
Seleccione una respuesta.
    a. -c    
    b. -X    
    c. -cm    
    d. -cmp    
    e. -cmXp    
    f. Compilation fails.    
    g. An exception is thrown at runtime.

Answer: E

No hay comentarios :

Publicar un comentario