Название: OCP Oracle Certified Professional Java SE 11 Developer Practice Tests
Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119696148
isbn:
23 Which expressions, when inserted into the blank in the following class, allow the code to compile? (Choose two.)package sun; import java.io.*; public class Beach { class TideException extends Exception {} public void surf() throws RuntimeException { try { throw new TideException(); } catch (_______________) {} } }Exception a | RuntimeException fIllegalStateException | TideException tTideException | IOException iTideException | Exception xError eException z
24 Which of the following are the best scenarios in which to use and catch an exception? (Choose two.)The computer caught fire.A network connection goes down.A caller passes invalid data to a method.The code does not compile.A method finishes sooner than expected.The program runs out of memory.
25 Which statement about the following program is correct?1: package dogpark; 2: public class Fetch { 3: public int play(String name) throws RuntimeException { 4: try { 5: throw new RuntimeException(name); 6: } catch (Throwable e) { 7: throw new RuntimeException(e); 8: } 9: } 10: public static final void main(String[] ball) 11: throws RuntimeException { 12: new Fetch().play("Webby"); 13: new Fetch().play("Georgette"); 14: } 15: }One exception is thrown to the caller at runtime.Two exceptions are thrown to the caller at runtime.More than two exceptions are thrown to the caller at runtime.The class does not compile because of the play() method.The class does not compile because of the main() method.None of the above.
26 What is the output of the following application?package body; import java.io.IOException; class Organ { public void operate() throws IOException { throw new RuntimeException("Not supported"); } } public class Heart extends Organ { public void operate() throws Exception { System.out.print("beat"); } public static void main(String… c) throws Exception { try { new Heart().operate(); } finally { System.out.print("!"); } } }beatbeat!Not supportedThe code does not compile.The code compiles, but a stack trace is printed at runtime.None of the above.
27 Which of the following are not true of using a try‐with‐resources statement? (Choose two.)It shortens the amount of code a developer must write.It is possible to close a resource before the end of the try block.Associated catch blocks are run before the declared resources have been closed.It is compatible with all classes that implement the Closeable interface.It is compatible with all classes that implement the AutoCloseable interface.It cannot be used with a finally block.
28 What is the result of compiling and executing the following class?package wind; public class Storm { public static void main(String… rain) throws Exception { var weatherTracker = new AutoCloseable() { public void close() throws RuntimeException { System.out.println("Thunder"); } }; try (weatherTracker) { System.out.println("Tracking"); } catch (Exception e) { System.out.println("Lightning"); } finally { System.out.println("Storm gone"); weatherTracker.close(); } } }It prints one line.It prints two lines.It prints three lines.It prints four lines.It does not compile due to an error in the declaration of the weatherTracker resource.It does not compile for a different reason.
29 How many of the following are valid exception declarations?class Error extends Exception {} class _X extends IllegalArgumentException {} class 2BeOrNot2Be extends RuntimeException {} class NumberException<Integer> extends NumberFormatException {} interface Worry implements NumberFormatException {}ZeroOneTwoThreeFourFive
30 If a try statement has catch blocks for both ClassCastException and RuntimeException, then which of the following statements is correct?The catch blocks for these two exception types can be declared in any order.A try statement cannot be declared with these two catch block types because they are incompatible.The catch block for ClassCastException must appear before the catch block for RuntimeException.The catch block for RuntimeException must appear before the catch block for ClassCastException.None of the above.
31 Assuming Scanner is a valid class that implements AutoCloseable, what is the expected output of the following application?package castles; import java.util.Scanner; public class Fortress { public void openDrawbridge() throws Exception { // p1 try { throw new Exception("Circle"); // p2 } catch (Exception | Error e) { System.out.print("Opening!"); } finally { System.out.print("Walls"); } } public static void main(String[] moat) { try (var e = new Scanner(System.in)) { new Fortress().openDrawbridge(); // p3 } } }Opening!WallsThe code does not compile because of line p1.The code does not compile because of line p2.The code does not compile because of line p3.The code compiles, but a stack trace is printed at runtime.None of the above.
32 What is the output of the following application?package game; public class BasketBall { public static void main(String[] dribble) { try { System.out.print(1); throw new ClassCastException(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.print(2); } catch (Throwable ex) { System.out.print(3); } finally { System.out.print(4); } System.out.print(5); } }14513451235The code does not compile.The code compiles but throws an exception at runtime.None of the above.
33 Which of the following statements about finally blocks are true? (Choose two.)Every line of the finally block is guaranteed to be executed.The finally block is executed only if the related catch block is also executed.The finally statement requires curly braces, {}.A finally block cannot throw an exception.The first line of a finally block is guaranteed to be executed.A finally block can only throw unchecked exceptions.
34 What is the output of the following application?package signlanguage; import java.io.Closeable; class ReadSign implements Closeable { public void close() {} public String get() {return "Hello";} } class MakeSign implements AutoCloseable { public void close() {} public void send(String message) { System.out.print(message); } } public class Translate { public static void main(String… hands) { try (ReadSign r = new ReadSign(); MakeSign w = new MakeSign();) { w.send(r.get()); } } }HelloThe code does not compile because of the ReadSign class.The code does not compile because of the MakeSign class.The code does not compile because of the Translate class.The code does not compile because of the try‐with‐resources statement.None of the above.
35 What is the output of the following application?package what; class FunEvent implements AutoCloseable { private final int value; FunEvent(int value) { this.value = value; } public void close() { System.out.print(value); } } public class Happening { public static void main(String… lots) { FunEvent e = new FunEvent(1); try (e; var f = new FunEvent(8)) { System.out.print("2"); throw new ArithmeticException(); } catch (Exception x) { System.out.print("3"); } finally { System.out.print("4"); } } }2421834234182348128134The code does not compile.
36 What is the output of the following application?package office; import java.io.*; public class Printer { public void print() { try { throw new FileNotFoundException(); } catch (Exception | RuntimeException e) { System.out.print("Z"); } catch (Throwable f) { System.out.print("X"); } finally { System.out.print("Y"); } } public static void main(String… ink) { new Printer().print(); } }YXYZYThe code does not compile.The code compiles, but a stack trace is printed at runtime.None of the above.
37 What is the output of the following code?class ProblemException extends Exception { ProblemException(Exception e) { super(e); } } class MajorProblemException extends ProblemException { MajorProblemException(Exception e) { super(e); } } public class Unfortunate { public static void main(String[] args) throws Exception { try { System.out.print(1); throw new MajorProblemException( new IllegalStateException()); } catch (ProblemException | RuntimeException e) { System.out.print(2); try { throw new MajorProblemException(e); } finally { System.out.print(3); } } finally { System.out.print(4); } } }123123 followed by an exception stack trace12341234 followed by an exception stack traceDoes not compileNone of the above
38 What is the output of the following application?1: package robot; 2: public class Computer { 3: public void compute() throws Exception { 4: throw new NullPointerException("Does not compute!"); 5: } 6: public static void СКАЧАТЬ