Название | OCP Oracle Certified Professional Java SE 11 Developer Practice Tests |
---|---|
Автор произведения | Jeanne Boyarsky |
Жанр | Программы |
Серия | |
Издательство | Программы |
Год выпуска | 0 |
isbn | 9781119696148 |
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 exception class that directly extends the Exception class?One that takes a single ExceptionOne that takes a single StringBoth of theseNeither of these
55 What is the result of compiling and running the following application?package dinner; public class Pizza { Exception order(RuntimeException e) { // h1 throw e; // h2 } public static void main(String… u) { var p = new Pizza(); try { p.order(new IllegalArgumentException()); // h3 } catch(RuntimeException e) { // h4 System.out.print(e); } } }java.lang.IllegalArgumentException is printed.The code does not compile because of line h1.The code does not compile because of line h2.The code does not compile because of line h3.The code does not compile because of line h4.The code compiles, but a stack trace is printed at runtime.
56 Given an application that hosts a website, which of the following would most likely result in a java.lang.Error being thrown? (Choose two.)A user tries to sign in too many times.Two users try to register an account at the same time.An order update page calls itself infinitely.The application temporarily loses connection to the network.A user enters their password incorrectly.The connections to a database are never released and keep accumulating.
57 How many lines of text does the following program print?package tron; class DiskPlayer implements AutoCloseable { public void close() {} } public class LightCycle { public static void main(String… bits) { try (DiskPlayer john = new DiskPlayer()) { System.out.println("ping"); john.close(); } finally { System.out.println("pong"); john.close(); } System.out.println("return"); } }One.Two.Three.The code does not compile because of the DiskPlayer class.The code does not compile for a different reason.None of the above.
58 What is the output of the following?package com.tps; import java.io.IOException; public class IncidentReportException extends RuntimeException { public IncidentReportException(Exception e) { super(e); } 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.
59 Given the following application, what is the name of the class printed at line e1?package canyon; final class FallenException extends Exception {} final class HikingGear implements AutoCloseable { @Override public void close() throws Exception { throw new FallenException(); } } public class Cliff { public final void climb() throws Exception { try (HikingGear gear = new HikingGear()) { throw new RuntimeException(); } } public static void main(String… rocks) { try { new Cliff().climb(); } catch (Throwable t) { System.out.println(t); // e1 } } }canyon.FallenExceptionjava.lang.RuntimeExceptionThe code does not compile.The code compiles, but the answer cannot be determined until runtime.None of the above.
60 Given the following application, which specific type of exception will be printed in the stack trace at runtime?package carnival; public class WhackAnException { public static void main(String… hammer) { try { throw new ClassCastException(); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(); } catch (RuntimeException e) { throw new NullPointerException(); } finally { throw new RuntimeException(); } } }ClassCastExceptionIllegalArgumentExceptionNullPointerExceptionRuntimeExceptionThe code does not compile.None of the above.
Chapter 5 Working with Arrays and Collections
THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Working with Arrays and CollectionsUse generics, including wildcardsUse a Java array and List, Set, Map and Deque collections, including convenience methodsSort collections and arrays using Comparator and Comparable interfaces
1 What is the output of the following?List<String> museums