OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
Чтение книги онлайн.
Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 22
190 What is the result of executing the Tortoise program?// Hare.java package com.mammal; public class Hare { void init() { System.out.print("init-"); } protected 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.
191 How many lines of the following program do not compile?interface Tool { void use(int fun); } abstract class Childcare { abstract void use(int fun); } final public class Stroller extends Childcare implements Tool { final public void use(int fun) { int width = 5; class ParkVisit { int getValue() { return width + fun; } } System.out.print(new ParkVisit().getValue()); } }ZeroOneTwoThreeMore than three
192 What is the result of executing the Sounds program?// Sheep.java package com.mammal; public class Sheep { default void baa() { System.out.println("baa!"); } default 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.
193 What is the best reason for marking an existing static method private within in an interface?It allows the method to be overridden in a subclass.It hides the secret implementation details from another developer using the interface.It improves the visibility of the method.It ensures the method is not replaced with an overridden implementation at runtime.It allows the method to be marked abstract.Trick question! All static methods are implicitly private within an interface.
194 What is the output of the following application?package jungle; public class RainForest extends Forest { public RainForest(long treeCount) { this.treeCount = treeCount+1; } public static void main(String[] birds) { System.out.print(new RainForest(5).treeCount); } } class Forest { public long treeCount; public Forest(long treeCount) { this.treeCount = treeCount+2; } }568The code does not compile.
195 What is the result of compiling and executing the following class?package sports; public class Bicycle { String color = "red"; private void printColor(String color) { color = "purple"; System.out.print(color); } public static void main(String[] rider) { new Bicycle().printColor("blue"); } }redpurpleblueIt does not compile.
196 Given that Short and Integer extend Number directly, what type can be used to fill in the blank in the following class to allow it to compile?package band; interface Horn { public Integer play(); } abstract class Woodwind { public Short play() { return 3; } } public final class Saxophone extends Woodwind implements Horn { public _________ play() { return null; } }ObjectIntegerShortNumberNone of the above
197 Which statements about abstract classes and methods are correct? (Choose three.)An abstract class can be extended by a final class.An abstract method can be overridden by a final method.An abstract class can be extended by multiple classes directly.An abstract class can extend multiple classes directly.An abstract class cannot implement an interface.An abstract class can extend an interface.
198 Given the following enum declaration, how many lines contain compilation errors?public enum Proposition { TRUE(1) { String getNickName() { return "RIGHT"; }}, FALSE(2) { public String getNickName() { return "WRONG"; }}, UNKNOWN(3) { public String getNickName() { return "LOST"; }} public int value; Proposition(int value) { this.value = value; } public int getValue() { return this.value; } protected abstract String getNickName(); }ZeroOneTwoThreeMore than three
199 Which statements about Java classes are true? (Choose three.)A Java class file may include more than one package statement.A Java class file may include more than one import statement.A Java class file may contain more than one comment.Any instance fields within a class must be defined after the class name.Any instance fields within a class must be defined before the class name.Java supports macros, in which fragments of code within a class may be defined inside a Java file, separate from any top‐level type declaration.
200 What is the result of executing the HopCounter 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 } } // HopCounter.java package com.insect; public class HopCounter { 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.
201 Which of the following is not an attribute common to both abstract classes and interfaces?They both can contain abstract methods.They both can contain public methods.They both can contain protected methods.They both can contain static variables.
202 Given the following class, which method signature could be successfully added to the class as an overloaded version of the findAverage() method?public class Calculations { public Integer findAverage(int sum) { return sum; } }public Long findAverage(int sum)public Long findAverage(int sum, int divisor)public Integer average(int sum)private void findAverage(int sum)
203 Which of the following is a valid method name in Java? (Choose two.)Go_$Outside$2()have‐Fun()new()9enjoyTheWeather()$sprint()walk#()
204 Fill in the blanks: A functional interface must contain or inherit ______________ and may optionally include ______________.at least one abstract method, the @Override annotationexactly one method, static methodsexactly one abstract method, the @FunctionalInterface annotationat least one static method, at most one default methodNone of the above
205 Fill in the blank with the line of code that allows the program to compile and print 15 at runtime.package love; interface Sport { private int play() { return 15; } } interface Tennis extends Sport { private int play() { return 30; } } public class Game implements Tennis { public int play() { return ______________; } public static void main(String… ace) { System.out.println(new Game().play()); } }Sport.play()Sport.super.play()Sport.Tennis.play()Tennis.Sport.super.play()The code does not compile regardless of what is inserted into the blank.None of the above.
206 What is the output of the following program?public class MoreMusic { { System.out.print("do-"); System.out.print("re-"); } public MoreMusic() { System.out.print("mi-"); } public MoreMusic(int note) { this(null); System.out.print("fa-"); } public MoreMusic(String song) { this(9); System.out.print("so-"); } public static void main(String[] sound) { System.out.print("la-"); var play = new MoreMusic(1); } }la‐do‐re‐mi‐so‐fa‐la‐do‐re‐mi‐fa‐do‐re‐mi‐fa‐so‐la‐fa‐re‐do‐mi‐so‐The code does not compile.None of the above.
207 Given the following two classes in the same package, what is the result of executing the Hug program?public class Kitten { /** private **/ float cuteness; /* public */ String name; // default double age; void meow() { System.out.println(name + " - "+cuteness); } } public class Hug { public static void main(String… friends) { var k = new Kitten(); k.cuteness = 5; k.name = "kitty"; k.meow(); } }kitty ‐ 5.0The Kitten class does not compile.The Hug class does not compile.The Kitten and Hug classes do not compile.None of the above.
208 Which