OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky

Чтение книги онлайн.

Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 26

OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky

Скачать книгу

{ 7: try { 8: new Computer().compute(); 9: } catch (RuntimeException e) { 10: System.out.print("zero"); 11: throw e; 12: } catch (Exception e) { 13: System.out.print("one"); 14: throw e; 15: } 16: } 17: }zeroonezero followed by a stack traceone followed by a stack traceDoes not compileNone of the above

      39 Given the following class diagram, which two classes are missing in the hierarchy at positions 1 and 2?IOException at position 1, Exception at position 2Exception at position 1, RuntimeException at position 2IllegalArgumentException at position 1, RuntimeException at position 2IllegalStateException at position 1, RuntimeException at position 2Exception at position 1, FileNotFoundException at position 2None of the above

      40 What is the output of the following application?package vortex; class TimeException extends Exception {} class TimeMachine implements AutoCloseable { int v; public TimeMachine(int v) {this.v = v;} public void close() throws Exception { System.out.print(v); } } public class TimeTraveler { public static void main(String[] twelve) { try (var timeSled = new TimeMachine(1); var delorean = new TimeMachine(2); var tardis = new TimeMachine(3)) { } catch (TimeException e) { System.out.print(4); } finally { System.out.print(5); } } }1235321551235321The code does not compile.None of the above.

      41 Which of the following are common reasons to add a checked exception to a method signature? (Choose three.)To alert developers that the state of the JVM has been corruptedTo force a caller to handle or declare potential problemsTo ensure that exceptions never cause the application to terminateTo notify the caller of potential types of problemsTo give the caller a chance to recover from a problemTo annoy other developers

      42 Which statement about the following application is correct?package highway; import java.io.*; class CarCrash extends RuntimeException { CarCrash(Exception e) {} // w1 } public class Car { public static void main(String[] s) throws Exception { // w2 try { throw new IOException("Auto-pilot error"); } catch (Exception | CarCrash e) { // w3 throw e; } catch (Exception a) { // w4 throw a; } } }The code does not compile because of line w1.The code does not compile because of line w2.The code does not compile because of line w3.The code does not compile because of line w4.The code compiles and prints a stack trace at runtime.None of the above.

      43 Which of the following exception classes must be handled or declared in the method in which they are thrown? (Choose three.)public class Happy extends IOException {} public class Dopey extends Grumpy {} public class Sleepy extends IllegalStateException {} public class Sneezy extends UnsupportedOperationException {} public class Doc extends AssertionError {} public class Grumpy extends SQLException {}HappyDopeySleepySneezyDocGrumpy

      44 What is the output of the following application?package pond; abstract class Duck { protected int count; public abstract int getDuckies(); } public class Ducklings extends Duck { private int age; public Ducklings(int age) { this.age = age; } public int getDuckies() { return this.age/count; } public static void main(String[] pondInfo) { Duck itQuacks = new Ducklings(5); System.out.print(itQuacks.getDuckies()); } }015The code does not compile.The code compiles but throws an exception at runtime.None of the above.

      45 Which statements about the following line of code are correct? (Choose three.)throw new IllegalArgumentException ();The method where this is called must declare a compatible exception.The code where this is called can include a try‐with‐resources block that handles this exception.This exception should not be handled or declared.The code where this is called can include a try/catch block that handles this exception.This exception should be thrown only at the start of a method.This exception does not need to be handled by the method in which it is called.

      46 What is the output of the following application?package storage; import java.io.*; public class Backup { public void performBackup() { try { throw new IOException("Disk not found"); // z1 } catch (Exception e) { try { throw new FileNotFoundException("File not found"); } catch (FileNotFoundException e) { // z2 System.out.print("Failed"); } } } public static void main(String… files) { new Backup().performBackup(); // z2 } }FailedThe application compiles, but a stack trace is printed at runtime.The code does not compile because of line z1.The code does not compile because of line z2.The code does not compile because of line z3.None of the above.

      47 What is the output of the following?package com.tps; import java.io.IOException; public class IncidentReportException extends RuntimeException { public static void main(String[] args) throws Exception { try { throw new IncidentReportException(new IOException()); } catch (RuntimeException e) { System.out.println(e.getCause()); } } }com.tps.IncidentReportExceptionjava.lang.IOExceptionThe code does not compile because IOException is a checked exception.The code does not compile due to the declaration of IncidentReportException.None of the above.

      48 Which expression, when inserted into the blank in the following class, allows the code to compile?package music; import java.sql.*; public class Bells { class Player implements AutoCloseable { @Override public void close() throws RingException {} } class RingException extends Exception { public RingException(String message) {} } public static void main(String[] notes) throws Throwable { try (Player p = null) { throw new Exception(); } catch (Exception e) { } catch (_______________) { } } }Error rIllegalStateException bRingException qSQLException pRuntimeException rThe code does not compile regardless of the expression used.

      49 What is the output of the following application?package zoo; class BigCat { void roar(int level) throw RuntimeException { if(level<3) throw new IllegalArgumentException(); System.out.print("Roar!"); } } public class Lion extends BigCat { public void roar() { System.out.print("Roar!!!"); } void roar(int sound) { System.out.print("Meow"); } public static void main(String[] cubs) { final BigCat kitty = new Lion(); kitty.roar(2); } }MeowRoar!Roar!!!MeowRoar!A stack trace is printed at runtime.None of the above.

      50 Which statement about the following program is true?package tag; class MissedCallException extends Exception {} public class Phone { static void makeCall() throws RuntimeException { throw new ArrayIndexOutOfBoundsException("Call"); } public static void main(String[] messages) { try { makeCall(); } catch (MissedCallException e) { throw new RuntimeException("Voicemail"); } finally { throw new RuntimeException("Text"); } } }An exception is printed at runtime with Call in the message.An exception is printed at runtime with Voicemail in the message.An exception is printed at runtime with Text in the message.The code does not compile.None of the above.

      51 If a try statement has catch blocks for both IllegalArgumentException and NullPointerException, 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 IllegalArgumentException must appear before the catch block for NullPointerException.The catch block for NullPointerException must appear before the catch block for IllegalArgumentException.None of the above.

      52 What is the output of the following application?package furniture; class Chair { public void sit() throws IllegalArgumentException { System.out.print("creek"); throw new RuntimeException(); } } public class Stool extends Chair { public void sit() throws RuntimeException { System.out.print("thud"); } public static void main(String… c) throws Exception { try { new Stool().sit(); } finally { System.out.print("?"); } } }creekthudthud?The code does not compile.The code compiles, but a stack trace is printed at runtime.None of the above.

      53 What is the output of the following application?import java.io.*; import java.sql.*; public class DatabaseHelper { static class MyDatabase implements Closeable { public void close() throws SQLException { System.out.print("2"); } public void write(String data) {} public String read() {return null;} } public static void main(String… files) throws Exception { try (MyDatabase myDb = new MyDatabase()) { // TODO: Decide what to read/rite } finally { System.out.print("1"); } } }1221The code does not compile because of the MyDatabase nested class.The code does not compile because of the try‐with‐resources statement.The code does not compile for a different reason.

      54 What constructors are capable of being called on a custom

Скачать книгу