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

Читать онлайн книгу.

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


Скачать книгу
many cells in the following table are incorrect?TypeAllows abstract methods?Allows constants?Allows constructors?Abstract classYesYesNoConcrete classYesYesYesInterfaceYesYesYesZeroOneTwoThreeFour

      132 Which statements are true about a functional interface? (Choose three.)It may contain any number of abstract methods.It must contain a single abstract method.It may contain any number of private methods.It must contain a single private method.It may contain any number of static methods.It must contain a single static method.

      133 Which variables have a scope limited to a method?Interface variablesClass variablesInstance variablesLocal variables

      134 What is a possible output of the following application?package wrap; public class Gift { private final Object contents; protected Object getContents() { return contents; } protected void setContents(Object contents) { this.contents = contents; } public void showPresent() { System.out.print("Your gift: "+contents); } public static void main(String[] treats) { Gift gift = new Gift(); gift.setContents(gift); gift.showPresent(); } }Your gift: wrap.Gift@29ca2745Your gift: Your gift:It does not compile.It compiles but throws an exception at runtime.

      135 Which of the following are the best reasons for creating a default interface method? (Choose two.)Allow interface methods to be overloaded.Add backward compatibility to existing interfaces.Give an interface the ability to create final methods.Allow an interface to define a method at the class level.Improve code reuse among classes that implement the interface.Improve encapsulation of the interface.

      136 How many compiler errors does the following code contain?package animal; interface CanFly { public void fly() {} } final class Bird { public int fly(int speed) {} } public class Eagle extends Bird implements CanFly { public void fly() {} }NoneOneTwoThreeFour

      137 Which of the following statements is not true?An instance of one class may access an instance of another class's attributes if it has a reference to the instance and the attributes are declared public.An instance of one class may access package‐private attributes in a parent class, provided the parent class is not in the same package.An instance of one class may access an instance of another class's attributes if both classes are located in the same package and marked protected.Two instances of the same class may access each other's private attributes.All of the above are true.

      138 What is the output of the following code?public class Bunny { static class Rabbit { void hop() { System.out.print("hop"); } } static class FlemishRabbit extends Rabbit { void hop() { System.out.print("HOP"); } } public static void main(String[] args) { Rabbit r1 = new FlemishRabbit(); FlemishRabbit r2 = new FlemishRabbit(); r1.hop(); r2.hop(); } }hophopHOPhophopHOPHOPHOPThe code does not compile.

      139 Which of the following are valid class declarations? (Choose three.)class _ {}class river {}class Str3@m {}class Pond2$ {}class _var_ {}class 5Ocean {}

      140 What is the output of the InfiniteMath program?class Math { public final double secret = 2; } class ComplexMath extends Math { public final double secret = 4; } public class InfiniteMath extends ComplexMath { public final double secret = 8; public static void main(String[] numbers) { Math math = new InfiniteMath(); System.out.print(math.secret); } }2.04.08.0The code does not compile.The code compiles but prints an exception at runtime.None of the above.

      141 Given the following application, which diagram best represents the state of the mySkier, mySpeed, and myName variables in the main() method after the call to the slalom() method?package slopes; public class Ski { private int age = 18; private static void slalom(Ski racer, int[] speed, String name) { racer.age = 18; name = "Wendy"; speed = new int[1]; speed[0] = 11; racer = null; } public static void main(String… mountain) { final var mySkier = new Ski(); mySkier.age = 16; final int[] mySpeed = new int[1]; final String myName = "Rosie"; slalom(mySkier,mySpeed,myName); } }

      142 What is the output of the following application?package zoo; public class Penguin { private int volume = 1; private class Chick { private static int volume = 3; void chick() { System.out.print("Honk("+Penguin.this.volume+")!"); } } public static void main(String… eggs) { Penguin pen = new Penguin(); final Penguin.Chick littleOne = pen.new Chick(); littleOne.chick(); } }Honk(1)!Honk(3)!The code does not compile.The code compiles, but the output cannot be determined until runtime.None of the above.

      143 Which can implement a functional interface?An anonymous classA top‐level classA lambda expressionAn anonymous class or a top‐level classA top‐level class or a lambda expressionAn anonymous class, a top‐level class, or a lambda expression

      144 Fill in the blank with the line of code that allows the program to compile and print E at runtime.interface Fruit { public default char getColor() { return 'F'; } } interface Edible { public default char getColor() { return 'E'; } } public class Banana implements Fruit, Edible { public char getColor() { return ____________; } public static void main(String[] a) { var d = new Banana(); System.out.println(d.getColor()); } }Edible.getColor()Edible.super.getColor()super.Edible.getColor()super.getColor()The code does not compile regardless of what is inserted into the blank.None of the above.

      145 Given the following two classes, each in a different package, which line inserted into the code allows the second class to compile?package clothes; public class Store { public static String getClothes() { return "dress"; } } package wardrobe; // INSERT CODE HERE public class Closet { public void borrow() { System.out.print("Borrowing clothes: "+getClothes()); } }static import clothes.Store.getClothes;import clothes.Store.*;import static clothes.Store.getClothes;import static clothes.Store;

      146 What is the output of the ElectricCar program?package vehicles; class Automobile { private final String drive() { return "Driving vehicle"; } } class Car extends Automobile { protected String drive() { return "Driving car"; } } public class ElectricCar extends Car { public final String drive() { return "Driving electric car"; } public static void main(String[] wheels) { final Automobile car = new ElectricCar(); var v = (Car)car; System.out.print(v.drive()); } }Driving vehicleDriving electric carDriving carThe code does not compile.The code compiles but produces a ClassCastException at runtime.None of the above.

      147 What is the output of the following program?public class Music { { System.out.print("do-"); } static { System.out.print("re-"); } { System.out.print("mi-"); } static { System.out.print("fa-"); } public Music() { System.out.print("so-"); } public Music(int note) { System.out.print("la-"); } public static void main(String[] sound) { System.out.print("ti-"); var play = new Music(); } }re‐fa‐ti‐do‐mi‐so‐do‐re‐mi‐fa‐ti‐so‐ti‐re‐fa‐do‐mi‐so‐re‐fa‐la‐mi‐ti‐do‐do‐re‐mi‐fa‐so‐tiThe code does not compile.None of the above.

      148 Given the following class declaration, which options correctly declare a local variable containing an instance of the class?public class Earth { private abstract class Sky { void fall() { var e = ____________ } } }new Sunset() extends Sky {};new Sky();new Sky() {}new Sky() { final static int blue = 1; };The code does not compile regardless of what is placed in the blank.None of the above.

      149 What is the output of the Encyclopedia program?package paper; abstract class Book { protected static String material = "papyrus"; public Book() {} abstract String read() {} public Book(String material) {this.material = material;} } public class Encyclopedia extends Book { public static String material = "cellulose"; public Encyclopedia() {super();} public String read() { return "Reading is fun!"; } public String getMaterial() {return super.material;} public static void main(String[] pages) { System.out.print(new Encyclopedia().read()); System.out.print("-" + new Encyclopedia().getMaterial()); } }Reading is fun!‐papyrusReading is fun!‐cellulosenull‐papyrusnull‐celluloseThe code does not compile.None of the above.

      150 What does the following print?interface Vehicle {} class Bus implements Vehicle {} public class Transport { public static void main(String[] args) { Bus bus = new Bus(); boolean n = null instanceof Bus; boolean v = bus instanceof Vehicle; boolean b = bus instanceof Bus; System.out.println(n + " " + v + " " + b); } }false false falsefalse false truefalse true truetrue false truetrue true falsetrue true true

      151 How many rows of the following table contain an error?Interface memberOptional modifier(s)Required modifier(s)Private methodprivate‐Default methodpublicdefaultStatic methodpublic static‐Abstract


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