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

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

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

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

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

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.

       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 = new ArrayList<>(1); museums.add("Natural History"); museums.add("Science"); museums.add("Art"); museums.remove(2); System.out.println(museums);[Natural History, Science][Natural History, Art, Science]The code does not compile.The code compiles but throws an exception at runtime.

      2 How many of the following are legal declarations?[]String lions = new String[]; String[] tigers = new String[1] {"tiger"}; String bears[] = new String[] {}; String ohMy [] = new String[0] {};NoneOneTwoThreeFour

      3 Which of the following can fill in the blank to make the code compile?public class News<____> {}? onlyN only? and NNews, and ObjectN, News, and ObjectNone of the above

      4 What is true of this code? (Choose two.)26: List<String> strings = new ArrayList<?>(); 27: var ints = new HashSet<Integer>(); 28: Double dbl = 5.0; 29: ints.add(2); 30: ints.add(null);The code compiles as is.One line needs to be removed for the code to compile.Two lines need to be removed for the code to compile.One line of code uses autoboxing.Two lines of code use autoboxing.Three lines of code use autoboxing.

      5 Which of the following creates an empty two‐dimensional array with dimensions 2×2?int[][] blue = new int[2, 2];int[][] blue = new int[2], [2];int[][] blue = new int[2][2];int[][] blue = new int[2 x 2];None of the above

      6 What is the output of the following?var q = new ArrayDeque<String>(); q.offer("snowball"); q.offer("minnie"); q.offer("sugar"); System.out.println(q.peek() + " " + q.peek() + " " + q.size());sugar sugar 3sugar minnie 1snowball minnie 1snowball snowball 3The code does not compile.None of the above.

      7 We are running a library. Patrons select books by name. They get at the back of the checkout line. When they get to the front, they scan the book's ISBN, a unique identification number. The checkout system finds the book based on this number and marks the book as checked out. Of these choices, which data structures best represent the line to check out the book and the book lookup to mark it as checked out, respectively?ArrayList, HashSetArrayList, TreeMapArrayList, TreeSetLinkedList, HashSetLinkedList, TreeMapLinkedList, TreeSet

      8 What is the result of running the following program?1: package fun; 2: public class Sudoku { 3: static int[][] game; 4: 5: public static void main(String[] args) { 6: game[3][3] = 6; 7: Object[] obj = game; 8: game[3][3] = "X"; 9: System.out.println(game[3][3]); 10: } 11: }XThe code does not compile.The code compiles but throws a NullPointerException at runtime.The code compiles but throws a different exception at runtime.

      9 Suppose we want to implement a Comparator<String> so that it sorts the longest strings first. You may assume there are no null values. Which method could implement such a comparator?public int compare(String s1, String s2) { return s1.length() - s2.length(); }public int compare(String s1, String s2) { return s2.length() – s1.length(); }public int compare(Object obj1, Object obj2) { String s1 = (String) obj1; String s2 = (String) obj2; return s1.length() - s2.length(); }public int compare(Object obj1, Object obj2) { String s1 = (String) obj1; String s2 = (String) obj2; return s2.length() – s1.length(); }None of the above

      10 How many lines does the following code output?var days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; for (int i = 1; i < days.length; i++) System.out.println(days[i]);Zero.Six.Seven.The code does not compile.The code compiles but throws an exception at runtime.

      11 Which cannot fill in the blank for this code to compile?var c = new _______________<String>(); c.add("pen"); c.remove("pen"); System.out.println(c.isEmpty());ArrayListLinkedListTreeMapTreeSetAll of these can fill in the blank.

      12 What is true of the following code? (Choose two.)private static void sortAndSearch(String… args) { var one = args[0]; Arrays.sort(args); ________ result = Arrays.binarySearch(args, one); System.out.println(result); } public static void main(String[] args) { sortAndSearch("seed", "flower"); }If the blank contains int, then the code outputs 0.If the blank contains int, then the code outputs 1.If the

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