OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
Чтение книги онлайн.
Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 20
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 methodpublicabstractZeroOneTwoThreeFour
152 What is the output of the following program?public class Dwarf { private final String name; public Dwarf() { this("Bashful"); } public Dwarf(String name) { name = "Sleepy"; } public static void main(String[] sound) { var d = new Dwarf("Doc"); System.out.println(d.name); } }SleepyBashfulDocThe code does not compile.An exception is thrown at runtime.
153 What is the output of the following application?package pocketmath; interface AddNumbers { int add(int x, int y); static int subtract(int x, int y) { return x-y; } default int multiply(int x, int y) { return x*y; } } public class Calculator { protected void calculate(AddNumbers n, int a, int b) { System.out.print(n.add(a, b)); } public static void main(String[] moreNumbers) { final var ti = new Calculator() {}; ti.calculate((k,p) -> p+k+1, 2, 5); // j1 } }8The code does not compile because AddNumbers is not a functional interface.The code does not compile because of line j1.The code does not compile for a different reason.None of the above.
154 Which of the following variables are always in scope for the entire program once defined?Package variablesClass variablesInstance variablesLocal variables
155 What is the command to call one constructor from another constructor in the same class?construct()parent()super()this()that()
156 Which of the following statements about no‐argument constructors and inheritance are correct? (Choose two.)The compiler cannot insert a no‐argument constructor into an abstract class.If a parent class does not include a no‐argument constructor, a child class cannot declare one.If a parent class declares constructors but each of them take at least one parameter, then a child class must declare at least one constructor.The no‐argument constructor is sometimes inserted by the compiler.If a parent class declares a no‐argument constructor, a child class must declare a no‐argument constructor.If a parent class declares a no‐argument constructor, a child class must declare at least one constructor.
157 Fill in the blanks: ______________ allow Java to support multiple inheritance, and anonymous classes can ______________ of them.Abstract classes, extend at most oneAbstract classes, extend any numberInterfaces, implement at most oneInterfaces, implement any numberConcrete classes, extend at most oneNone of the above
158 What is the result of executing the Grasshopper program?// Hopper.java package com.animals; public class Hopper { protected void hop() { System.out.println("hop"); } } // Grasshopper.java package com.insect; import com.animals.Hopper; public class Grasshopper extends Hopper { public void move() { hop(); // p1 } public static void main(String[] args) { var hopper = new Grasshopper(); hopper.move(); // p2 hopper.hop(); // p3 } }The code prints hop once.The code prints hop twice.The first compiler error is on line p1.The first compiler error is on line p2.The first compiler error is on line p3.
159 What is the minimum number of lines that need to be removed to make this code compile?@FunctionalInterface public interface Play { public static void baseball() {} private static void soccer() {} default void play() {} void fun(); void game(); void toy(); }1234The code compiles as is.
160 Which of the following are the best reasons for creating a private interface method? (Choose two.)Add backward compatibility to existing interfaces.Provide an implementation that a class implementing the interface can override.Increase code reuse within the interface.Allow interface methods to be inherited.Improve encapsulation of the interface.Allow static methods to access instance methods.
161 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 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.
162 What is the output of the following application?package stocks; public class Bond { private static int price = 5; public boolean sell() { if(price<10) { price++; return true; } else if(price>=10) { return false; } } public static void main(String[] cash) { new Bond().sell(); new Bond().sell(); new Bond().sell(); System.out.print(price); } }568The code does not compile.
163 Given the following class declaration, what expression can be used to fill in the blank so that 88 is printed at runtime?final public class Racecar { final private int speed = 88; final protected class Engine { private final int speed = 100; public final int getSpeed() { return _____________________; } } final Engine engine = new Engine(); final public static void main(String[] feed) { System.out.print(new Racecar().engine.getSpeed()); } }Racecar.speedthis.speedthis.Racecar.speedRacecar.Engine.this.speedRacecar.this.speedThe code does not compile regardless of what is placed in the blank.
164 Which statements about static initializers are correct? (Choose three.)They cannot be used to create instances of the class they are contained in.They can assign a value to a static final variable.They are executed at most once per program.They are executed each time an instance of the class is created from a local cache of objects.They are executed each time an instance of the class is created using the new keyword.They may never be executed.
165 What is the output of the BlueCar program?package race; abstract class Car { static { System.out.print("1"); } public Car(String name) { super(); System.out.print("2"); } { System.out.print("3"); } } public class BlueCar extends Car { { System.out.print("4"); } public BlueCar() { super("blue"); System.out.print("5"); } public static void main(String[] gears) { new BlueCar(); } }23451123451452313245The code does not compile.None of the above.
166 Given the following class declaration, which value cannot be inserted into the blank line that would allow the code to compile?package mammal; interface Pet {} public class Canine implements Pet { public ______ getDoggy() { return this; } }CanineListObjectPetAll of the above can be inserted.
167 Which statement about the following interface is correct?public interface Movie { String pass = "TICKET"; private void buyPopcorn() { purchaseTicket(); } public static int getDrink() { buyPopcorn(); return 32; } private static String purchaseTicket() { getDrink(); return pass; } }The code compiles.The code contains an invalid constant.The method buyPopcorn() does not compile.The method getDrink() does not compile.The method purchaseTicket() does not compile.The code does not compile for a different reason.
168 Which methods compile?private static int numShovels; private