Название | OCP Oracle Certified Professional Java SE 11 Developer Practice Tests |
---|---|
Автор произведения | Jeanne Boyarsky |
Жанр | Программы |
Серия | |
Издательство | Программы |
Год выпуска | 0 |
isbn | 9781119696148 |
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 int numRakes; public int getNumShovels() { return numShovels; } public int getNumRakes() { return numRakes; }Just getNumRakes()Just getNumShovels()Both methodsNeither method
169 How many lines of the following class contain compilation errors?1: class Fly { 2: public Fly Fly() { return Fly(); } 3: public void Fly(int kite) {} 4: public int Fly(long kite) { return 1; } 5: public static void main(String[] a) { 6: var f = new Fly(); 7: f.Fly(); 8: } 9: }None.One.Two.Three.Four.The answer cannot be determined with the information given.
170 How many of the classes in the figure can write code that references the sky() method?NoneOneTwoThreeFour
171 For the diagram in the previous question, how many classes can write code that references the light variable?NoneOneTwoThreeFour
172 Given the following method signature, which classes cannot call it?protected void run(String government)All classes in other packagesAll classes in the same packageSubclasses in a different packageSubclasses in the same package
173 What is the output of the following application?interface Toy { String play(); } public class Gift { public static void main(String[] matrix) { abstract class Robot {} class Transformer extends Robot implements Toy { public String name = "GiantRobot"; public String play() {return "DinosaurRobot";} // y1 } Transformer prime = new Transformer () { public String play() {return name;} // y2 }; System.out.print(prime.play()+" "+name); } }GiantRobot GiantRobotGiantRobot DinosaurRobotDinosaurRobot DinosaurRobotThe code does not compile because of line y1.The code does not compile because of line y2.None of the above.
174 What is the output of the HighSchool application?package edu; import java.io.FileNotFoundException; abstract class School { abstract Float getNumTeachers(); public int getNumStudents() { return 10; } } public class HighSchool extends School { final Float getNumTeachers() { return 4f; } public int getNumStudents()