OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
Чтение книги онлайн.
Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 23
209 What is the output of the following application?package prepare; interface Ready { static int first = 2; final short DEFAULT_VALUE = 10; GetSet go = new GetSet(); // n1 } public class GetSet implements Ready { int first = 5; static int second = DEFAULT_VALUE; // n2 public static void main(String[] begin) { var r = new Ready() {}; System.out.print(r.first); // n3 System.out.print(" " + second); // n4 } }2 105 10The code does not compile because of line n1.The code does not compile because of line n2.The code does not compile because of line n3.The code does not compile because of line n4.
210 What is the result of executing the Tortoise program?// Hare.java package com.mammal; public class Hare { public void init() { System.out.print("init-"); } private void race() { System.out.print("hare-"); } } // Tortoise.java package com.reptile; import com.mammal.Hare; public class Tortoise { protected void race(Hare hare) { hare.init(); // x1 hare.race(); // x2 System.out.print("tortoise-"); } public static void main(String[] args) { var tortoise = new Tortoise(); var hare = new Hare(); tortoise.race(hare); } }init‐hare‐tortoiseinit‐hareThe first line with a compiler error is line x1.The first line with a compiler error is line x2.The code does not compile due to a different line.The code throws an exception.
211 What is the result of executing the Sounds program?// Sheep.java package com.mammal; public class Sheep { private void baa() { System.out.println("baa!"); } private static void speak() { baa(); } } // Sounds.java package com.animals; import com.mammal.Sheep; public class Sounds { public static void main(String[] args) { var sheep = new Sheep(); sheep.speak(); } }The code runs and prints baa!.The Sheep class does not compile.The Sounds class does not compile.Neither class compiles.
212 What is the output of the Helicopter program?package flying; class Rotorcraft { protected final int height = 5; abstract int fly(); } interface CanFly {} public class Helicopter extends Rotorcraft implements CanFly { private int height = 10; protected int fly() { return super.height; } public static void main(String[] unused) { Helicopter h = (Helicopter)new Rotorcraft(); System.out.print(h.fly()); } }510The code does not compile.The code compiles but produces a ClassCastException at runtime.None of the above.
213 Which statements about the following Twins class are true? (Choose three.)package clone; interface Alex { default void write() { System.out.print("1"); } static void publish() {} void think(); private int process() { return 80; } } interface Michael { default void write() { System.out.print("2"); } static void publish() {} void think(); private int study() { return 100; } } public class Twins implements Alex, Michael { void write() { System.out.print("3"); } static void publish() {} void think() { System.out.print("Thinking…"); } }The class fails to compile because of the write() method.The class fails to compile because of the publish() method.The class fails to compile because of the think() method.All of the methods defined in the Alex interface are accessible in the Twins class.All of the methods defined in the Michael interface are accessible in the Twins class.The Twins class cannot be marked abstract.
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