OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky. Читать онлайн. Newlib. NEWLIB.NET

Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Программы
Год издания: 0
isbn: 9781119696148
Скачать книгу
return 20; } public static void main(String[] s) throws Exception { var school = new HighSchool(); System.out.print(school.getNumStudents()); } }10204.0One line of the program does not compile.Two lines of the program do not compile.None of the above.

      175 What is the output of the following application?package track; interface Run { default CharSequence walk() { return "Walking and running!"; } } interface Jog { default String walk() { return "Walking and jogging!"; } } public class Sprint implements Run, Jog { public String walk() { return "Sprinting!"; } public static void main(String[] args) { var s = new Sprint(); System.out.println(s.walk()); } }Walking and running!Walking and jogging!Sprinting!The code does not compile.The code compiles but prints an exception at runtime.None of the above.

      176 What is true of these two interfaces?interface Crawl { void wriggle(); } interface Dance { public void wriggle(); }A concrete class can implement both, but must implement wriggle().A concrete class can implement both, but must not implement wriggle().A concrete class would only be able to implement both if the public modifier were removed but must implement wriggle().If the public modifier were removed, a concrete class can implement both, but must not implement wriggle().None of the above.

      177 Which of these are functional interfaces?interface Lion { public void roar(); default void drink() {} boolean equals(Lion lion); } interface Tiger { public void roar(); default void drink() {} String toString(String name); }LionTigerBoth Lion and TigerNeither is a functional interface.The code does not compile.

      178 How many lines of the following class contain a compiler error?1: public class Dragon { 2: boolean scaly; 3: static final int gold; 4: Dragon protectTreasure(int value, boolean scaly) { 5: scaly = true; 6: return this; 7: } 8: static void fly(boolean scaly) { 9: scaly = true; 10: } 11: int saveTheTreasure(boolean scaly) { 12: return this.gold; 13: } 14: static void saveTheDay(boolean scaly) { 15: this.gold = 0; 16: } 17: static { gold = 100; } 18: }NoneOneTwoThreeMore than three

      179 What is true of the following method?public String getColor() { return color; }It is a correctly implemented accessor method.It is a correctly implemented mutator method.It is an incorrectly implemented accessor method.It is an incorrectly implemented mutator method.None of the above.

      180 Which statement is true?You can always change a method signature from call(String[] arg) to call(String… arg) without causing a compiler error in the calling code.You can always change a method signature from call(String… arg) to call(String[] arg) without causing a compiler error in the existing code.Both of the above.Neither of the above.

      181 What are two motivations for marking a class final? (Choose two.)Guarantee behavior of a classAllow the class to be extendedImprove securitySupport polymorphismImprove performanceEnsure the contents of the class are immutable

      182 Which statement about the following interface is correct?public interface Planet { int circumference; public abstract void enterAtmosphere(); public default int getCircumference() { enterAtmosphere(); return circumference; } private static void leaveOrbit() { var earth = new Planet() { public void enterAtmosphere() {} }; earth.getCircumference(); } }The code compiles.The method enterAtmosphere() does not compile.The method getCircumference() does not compile.The method leaveOrbit() does not compile.The code does not compile for a different reason.None of the above.

      183 Fill in the blanks: ___________________ methods always have the same name but a different list of parameters, while ___________________ methods always have the same name and the same return type.Overloaded, overriddenInherited, overriddenOverridden, overloadedHidden, overloadedOverridden, hiddenNone of the above

      184 What is the output of the following program?public class Husky { { this.food = 10; } { this.toy = 2; } private final int toy; private static int food; public Husky(int friend) { this.food += friend++; this.toy -= friend--; } public static void main(String… unused) { var h = new Husky(2); System.out.println(h.food+","+h.toy); } }12,‐112,213,‐1Exactly one line of this class does not compile.Exactly two lines of this class do not compile.None of the above.

      185 Suppose you have the following code. Which of the images best represents the state of the references right before the end of the main() method, assuming garbage collection hasn't run?1: public class Link { 2: private String name; 3: private Link next; 4: public Link(String name, Link next) { 5: this.name = name; 6: this.next = next; 7: } 8: public void setNext(Link next) { 9: this.next = next; 10: } 11: public Link getNext() { 12: return next; 13: } 14: public static void main(String… args) { 15: var apple = new Link("x", null); 16: var orange = new Link("y", apple); 17: var banana = new Link("z", orange); 18: orange.setNext(banana); 19: banana.setNext(orange); 20: apple = null; 21: banana = null; 22: } 23: }Option A.Option B.Option C.Option D.The code does not compile.None of the above.

      186 Which statement about a no‐argument constructor is true?The Java compiler will always insert a default no‐argument constructor if you do not define a no‐argument constructor in your class.For a class to call super() in one of its constructors, its parent class must explicitly implement a no‐argument constructor.If a class extends another class that has only one constructor that takes a value, then the child class must explicitly declare at least one constructor.A class may contain more than one no‐argument constructor.

      187 Which variable declaration is the first line not to compile?public class Complex { class Building {} class House extends Building{} public void convert() { Building b1 = new Building(); House h1 = new House(); Building b2 = new House(); Building b3 = (House) b1; House h2 = (Building) h1; Building b4 = (Building) b2; House h3 = (House) b2; } }b3h2b4h3All of the lines compile.

      188 What is the output of the following application?1: interface Tasty { 2: default void eat() { 3: System.out.print("Spoiled!"); 4: } } 5: public class ApplePicking { 6: public static void main(String[] food) { 7: var apple = new Tasty() { 8: @Override 9: void eat() { 10: System.out.print("Yummy!"); 11: } 12: } 13: } }Spoiled!Yummy!The application completes without printing anything.One line of this application fails to compile.Two lines of this application fail to compile.None of the above.

      189 Which of the following statements about functional interfaces is true?It is possible to define a functional interface that returns two data types.It is possible to define a primitive functional interface that uses float, char, or short.All functional interfaces must take arguments or return a value.None of the primitive functional interfaces includes generic arguments.None of these statements is true.

      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