Название | OCP Oracle Certified Professional Java SE 11 Developer Practice Tests |
---|---|
Автор произведения | Jeanne Boyarsky |
Жанр | Программы |
Серия | |
Издательство | Программы |
Год выпуска | 0 |
isbn | 9781119696148 |
214 Given the following program, what is the first line to fail to compile?1: public class Electricity { 2: interface Power {} 3: public static void main(String[] light) { 4: class Source implements Power {}; 5: final class Super extends Source {}; 6: var start = new Super() {}; 7: var end = new Source() { static boolean t = true; }; 8: } 9: }Line 2Line 4Line 5Line 6Line 7All of the lines compile
215 What is the output of the following application?package prepare; public class Ready { protected static int first = 2; private final short DEFAULT_VALUE = 10; private static class GetSet { int first = 5; static int second = DEFAULT_VALUE; } private GetSet go = new GetSet(); public static void main(String[] begin) { Ready r = new Ready(); System.out.print(r.go.first); System.out.print(", "+r.go.second); } }2, 55, 102, 10The code does not compile because of the GetSet class declaration.The code does not compile for another reason.
216 Which of the following are true about the following code? (Choose two.)public class Values { static ____ defaultValue = 8; static ____ DEFAULT_VALUE; public static void main(String[] args) { System.out.println(defaultValue + DEFAULT_VALUE); } }When you fill in both blanks with double, it prints 8.00.0When you fill in both blanks with double, it prints 8.0When you fill in both blanks with int, it prints 8When you fill in both blanks with int, it prints 80When you fill in both blanks with var, it prints 8When you fill in both blanks with var, it prints 80
217 How many Gems objects are eligible for garbage collection right before the end of the main() method?public class Gems { public String name; public Gems(String name) { this.name = name; } public static void main(String… args) { var g1 = Gems("Garnet"); var g2 = Gems("Amethyst"); var g3 = Gems("Pearl"); var g4 = Gems("Steven"); g2 = g3; g3 = g2; g1 = g2; g4 = null; } }NoneOneTwoThreeFourThe code does not compile
218 How many lines of the following program contain compilation errors?package sky; public class Stars { private int inThe = 4; public void Stars() { super(); } public Stars(int inThe) { this.inThe = this.inThe; } public static void main(String[] endless) { System.out.print(new sky.Stars(2).inThe); } }NoneOneTwoThree
219 What is the output of the following application?package sports; abstract class Ball { protected final int size; public Ball(int size) { this.size = size; } } interface Equipment {} public class SoccerBall extends Ball implements Equipment { public SoccerBall() { super(5); } public Ball get() { return this; } public static void main(String[] passes) { var equipment = (Equipment)(Ball)new SoccerBall().get(); System.out.print(((SoccerBall)equipment).size); } }5The code does not compile due to an invalid cast.The code does not compile for a different reason.The code compiles but throws a ClassCastException at runtime.
220 Which statement about the Elephant program is correct?package stampede; interface Long { Number length(); } public class Elephant { public class Trunk implements Long { public Number length() { return 6; } // k1 } public class MyTrunk extends Trunk { // k2 public Integer length() { return 9; } // k3 } public static void charge() { System.out.print(new MyTrunk().length()); } public static void main(String[] cute) { new Elephant().charge(); // k4 } }It compiles and prints 6.The code does not compile because of line k1.The code does not compile because of line k2.The code does not compile because of line k3.The code does not compile because of line k4.None of the above.
Chapter 4 Exception Handling
THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Exception HandlingHandle exceptions using try/catch/finally clauses, try‐with‐resource, and multi‐catch statementsCreate and use custom exceptions
1 Fill in the blanks: The ___________________ keyword is used in method declarations, while the ___________________ keyword is used to send an exception to the surrounding process.throwing, catchthrows, throwcatch, throwthrows, catchthrow, throwscatch, throwing
2 What is the result of compiling and executing the following application?package mind; import java.io.*; public class Remember { public static void think() throws IOException { // k1 try { throw Exception(); } catch (RuntimeException r) {} // k2 } public static void main(String… ideas) throws Exception { think(); } }The code compiles and runs without printing anything.The code compiles, but a stack trace is printed at runtime.The code does not compile because of line k1.The code does not compile because of line k2.None of the above.
3 Given the following keywords, in which order could they be used? (Choose two.)try, finallycatch, try, finallytry, catch, catch, finallyfinally, catch, trytry, finally, catchtry, catch, finally, finally
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