OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
Чтение книги онлайн.
Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 24
4 Fill in the blanks: A try statement ______________ a catch or a finally block, while a try‐with‐resources statement ______________.is not required to contain, is not required to contain eitheris not required to contain, must contain one of themmust contain, is not required to contain eithermust contain, must contain a catch blockNone of the above.
5 What is the output of the following application?package park; class LostBallException extends Exception {} public class Ball { public void toss() throw LostBallException { var windUp = new int[0]; System.out.println(windUp[0]); } public static void main(String[] bouncy) { try { new Ball().toss(); } catch (Throwable e) { System.out.print("Caught!"); } } }0Caught!The code does not compile because LostBallException is not handled or declared in the main() method.The code does not compile because ArrayIndexOutOfBoundsException is not handled or declared in the toss() method.The code does not compile for a different reason.None of the above.
6 Assuming Scanner is a valid class that implements AutoCloseable, what is the expected output of the following code?try (Scanner s = new Scanner(System.in)) { System.out.print(1); s.nextLine(); System.out.print(2); s = null; } catch (IllegalArgumentException | NullPointerException x) { s.nextLine(); System.out.print(3); } finally { s.nextLine(); System.out.print(4); } System.out.print(5);12451251234 followed by a stack trace124 followed by a stack traceDoes not compileNone of the above
7 How many constructors in WhaleSharkException compile in the following class?package friendly; public class WhaleSharkException extends Exception { public WhaleSharkException() { super("Friendly shark!"); } public WhaleSharkException(String message) { super(new Exception(new WhaleSharkException())); } public WhaleSharkException(Exception cause) {} }NoneOneTwoThree
8 What is the output of the following application?package game; public class Football { public static void main(String officials[]) { try { System.out.print('A'); throw new ArrayIndexOutOfBoundsException(); } catch (RuntimeException r) { System.out.print('B'); throw r; } catch (Exception e) { System.out.print('C'); } finally { System.out.print('D'); } } }ABCABDABC followed by a stack traceABD followed by a stack traceAD followed by a stack traceNone of the above
9 Which of the following types are not recommended to catch in your application? (Choose two.)ExceptionCheckedExceptionThrowableRuntimeExceptionUncheckedExceptionError
10 What is the output of the following program?package buffet; class Garden implements AutoCloseable { private final int g; Garden(int g) { this.g = g; } public void close() throws Exception { System.out.print(g); } } public class Salad { public static void main(String[] u) throws Exception { var g = new Garden(5); try (g; var h = new Garden(4); var i = new Garden(2)) { } finally { System.out.println(9); } g = null; } }2459924554299542The code does not compile.None of the above.
11 What is the output of the following application?package paper; import java.io.Closeable; public class PrintCompany { class Printer implements Closeable { // r1 public void print() { System.out.println("This just in!"); } public void close() {} } public void printHeadlines() { try {Printer p = new Printer()} { // r2 p.print(); } } public static void main(String[] headlines) { new PrintCompany().printHeadlines(); // r3 } }This just in!The code does not compile because of line r1.The code does not compile because of line r2.The code does not compile because of line r3.The code does not compile for a different reason.None of the above.
12 How many of these custom exceptions are unchecked exceptions?class ColoringException extends IOException {} class CursiveException extends WritingException {} class DrawingException extends IllegalStateException {} class SketchingException extends DrawingException {} class WritingException extends Exception {}NoneOneTwoThreeFourFive
13 How many lines of text does the following program print?package lighting; import java.io.IOException; public class Light { public static void main(String[] v) throws Exception { try { new Light().turnOn(); } catch (RuntimeException v) { // y1 System.out.println(v); throw new IOException(); // y2 } finally { System.out.println("complete"); } } public void turnOn() throws IOException { new IOException("Not ready"); // y3 } }One.Two.The code does not compile because of line y1.The code does not compile because of line y2.The code does not compile because of line y3.None of the above.
14 Which statements about try‐with‐resources are false? (Choose two.)If more than one resource is used, the resources are closed in the order they were created.Parentheses are used for the resource declaration section, even if more than one resource is used.If the try block and close() method both throw an exception, then the one thrown by the close() method is suppressed.A resource may be declared before it is used in a try‐with‐resources statement.Resources declarations are separated by commas.A catch block is not required.
15 How many lines of text does the following program print?package bee; class SpellingException extends RuntimeException {} public class SpellChecker { public final static void main(String… participants) { try { if(!"cat".equals("kat")) { new SpellingException(); } } catch (SpellingException | NullPointerException e) { System.out.println("Spelling problem!"); } catch (Exception e) { System.out.println("Unknown Problem!"); } finally { System.out.println("Done!"); } } }One.Two.Three.The code does not compile.None of the above.
16 Which of the following exception types must be handled or declared by the method in which they are thrown? (Choose three.)FileNotFoundExceptionClassCastExceptionErrorIOExceptionNullPointerExceptionException
17 What is the output of the following application?package bed; public class Sleep { public static void snore() { try { String sheep[] = new String[3]; System.out.print(sheep[3]); } catch (RuntimeException | Error e) { System.out.print("Awake!"); } finally { throw new Exception(); // x1 } } public static void main(String… sheep) { // x2 new Sleep().snore(); // x3 } }Awake!Awake! followed by a stack traceDoes not compile because of line x1Does not compile because of line x2Does not compile because of line x3None of the above
18 What is the output of the following code?class ProblemException extends Exception { ProblemException(Exception e) { super(e); } } class MajorProblemException extends ProblemException { MajorProblemException(String message) { super(message); } } public class Unfortunate { public static void main(String[] args) throws Exception { try { System.out.print(1); throw new MajorProblemException("Uh oh"); } catch (ProblemException | RuntimeException e) { System.out.print(2); try { throw new MajorProblemException("yikes"); } finally { System.out.print(3); } } finally { System.out.print(4); } } }123123 followed by an exception stack trace.12341234 followed by an exception stack trace.The code does not compile.None of the above.
19 Which statement best describes how a class that implements the AutoCloseable interface should be written? (Choose two.)The close() method is optional since the AutoCloseable interface defines a default implementation.The close() method should avoid modifying data after it has been run once.The close() method should not throw any exceptions.The close() method should throw an exception if there is a problem closing the resource.The close() method should return a status code.
20 Which of the following diagrams of java.lang classes shows the inheritance model properly?
21 Which exception classes, when inserted into the blank in the Problems class, allow the code to compile?class MissingMoneyException {} class MissingFoodException {} public class Problems { public void doIHaveAProblem() throws MissingMoneyException, MissingFoodException { System.out.println("No problems"); } public static void main(String[] s) throws ______________ { try { final Problems p = new Problems(); p.doIHaveAProblem(); } catch (Exception e) { throw e; } } }ExceptionRuntimeExceptionMissingFoodExceptionMissingMoneyException, MissingFoodExceptionMissingMoneyExceptionNone of the above
22 Which statements about Closeable and AutoCloseable are true? (Choose