OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
Чтение книги онлайн.
Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 28
13 How many of the following are legal declarations?public void greek() { [][]String alpha; []String beta; String[][] gamma; String[] delta[]; String epsilon[][]; var[][] zeta; }OneTwoThreeFourFiveSix
14 What is the result of the following?var list = new ArrayList<Integer>(); list.add(56); list.add(56); list.add(3); var set = new TreeSet<Integer>(list); System.out.print(set.size()); System.out.print(" " ); System.out.print(set.iterator().next());2 32 563 33 56None of the above
15 What is true of the code when run as java Copier.java? (Choose two.)1: import java.util.Arrays; 2: 3: public class Copier { 4: public static void main(String… original) { 5: String… copy = original; 6: Arrays.linearSort(original); 7: Arrays.search(original, ""); 8: System.out.println(original.size() 9: + " " + original[0]); 10: } 11: } One line contains a compiler error.Two lines contain a compiler error.Three lines contain a compiler error.Four lines contain a compiler error.If the compiler errors were fixed, the code would throw an exception.If the compiler errors were fixed, the code would run successfully.
16 What is the output of the following? (Choose three.)20: var chars = new _______________<Character>(); 21: chars.add('a'); 22: chars.add(Character.valueOf('b')); 23: chars.set(1, 'c'); 24: chars.remove(0); 25: System.out.print(chars.size() + " " + chars.contains('b'));When inserting ArrayList into the blank, the code prints 1 false.When inserting ArrayList into the blank, the code does not compile.When inserting HashMap into the blank, the code prints 1 false.When inserting HashMap into the blank, the code does not compile.When inserting HashSet into the blank, the code prints 1 false.When inserting HashSet into the blank, the code does not compile.
17 What is the output of the following?class Magazine { private String name; public Magazine(String name) { this.name = name; } public int compareTo(Magazine m) { return name.compareTo(m.name); } public String toString() { return name; } } public class Newsstand { public static void main(String[] args) { var set = new TreeSet<Magazine>(); set.add(new Magazine("highlights")); set.add(new Magazine("Newsweek")); set.add(new Magazine("highlights")); System.out.println(set.iterator().next()); } }highlightsNewsweeknullThe code does not compile.The code compiles but throws an exception at runtime.
18 Which is the first line to prevent this code from compiling and running without error?char[][] ticTacToe = new char[3][3]; // r1 ticTacToe[1][3] = 'X'; // r2 ticTacToe[2][2] = 'X'; ticTacToe[3][1] = 'X'; System.out.println(ticTacToe.length + " in a row!"); // r3Line r1Line r2Line r3None of the above
19 What is the first line with a compiler error?class Mammal {} class Bat extends Mammal {} class Cat extends Mammal {} class Sat {} class Fur<T extends Mammal> { // line R void clean() { var bat = new Fur<Bat>(); // line S var cat = new Fur<Cat>(); // line T var sat = new Fur<Sat>(); // line U } }Line RLine SLine TLine UNone of the above
20 What is a possible result of this code?17: var nums = new HashSet<Long>(); 18: nums.add((long) Math.min(5, 3)); 19: nums.add(Math.round(3.14)); 20: nums.add((long) Math.pow(4,2)); 21: System.out.println(nums);[3][16][16, 3][16, 3, 3]None of the above
21 What is the output of the following?5: var x = new LinkedList<Integer>(); 6: x.offer(18); 7: x.offer(5); 8: x.push(13); 9: System.out.println(x.poll() + " " + x.poll());13 513 1818 518 13The code does not compile.The code compiles, but prints something else.
22 Suppose we want to store JellyBean objects. Which of the following require JellyBean to implement the Comparable interface or create a Comparator to add them to the collection? (Choose two.)ArrayListHashMapHashSetSortedArrayTreeMapTreeSet
23 Which of the following references the first and last elements in a nonempty array?trains[0] and trains[trains.length]trains[0] and trains[trains.length ‐ 1]trains[1] and trains[trains.length]trains[1] and trains[trains.length ‐ 1]None of the above
24 Which of the following fills in the blank so this code compiles?public static void throwOne(Collection<__________> coll) { var iter = coll.iterator(); if (iter.hasNext()) throw iter.next(); }?? extends RuntimeException? super RuntimeExceptionNone of the above
25 Which of these four array declarations produces a different array than the others?int[][] nums = new int[2][1];int[] nums[] = new int[2][1];int[] nums[] = new int[][] { { 0 }, { 0 } };int[] nums[] = new int[][] { { 0, 0 } };
26 What does the following output?var linux = new String[] { "Linux", "Mac", "Windows" }; var mac = new String[] { "Mac", "Linux", "Windows" }; var search = Arrays.binarySearch(linux, "Linux"); var mismatch1 = Arrays.mismatch(linux, mac); var mismatch2 = Arrays.mismatch(mac, mac); System.out.println(search + " " + mismatch1 + " " + mismatch2);‐1 0 ‐1‐1 ‐1 00 ‐1 00 0 ‐1The output is not defined.The code does not compile.
27 Which line in the main() method doesn't compile or points to a class that doesn't compile?1: interface Comic<C> { 2: void draw(C c); 3: } 4: class ComicClass<C> implements Comic<C> { 5: public void draw(C c) { 6: System.out.println(c); 7: } 8: } 9: class SnoopyClass implements Comic<Snoopy> { 10: public void draw(Snoopy c) { 11: System.out.println(c); 12: } 13: } 14: class SnoopyComic implements Comic<Snoopy> { 15: public void draw(C c) { 16: System.out.println(c); 17: } 18: } 19: public class Snoopy { 20: public static void main(String[] args) { 21: Comic<Snoopy> c1 = c -> System.out.println(c); 22: Comic<Snoopy> c2 = new ComicClass<>(); 23: Comic<Snoopy> c3 = new SnoopyClass(); 24: Comic<Snoopy> c4 = new SnoopyComic(); 25: } 26: }Line 21.Line 22.Line 23.Line 24.None of the above. All of the code compiles.
28 Fill in the blank to make this code compile:public class Truck implements Comparable<Truck> { private int id; public Truck(int id) { this.id = id; } @Override public int _____________________ { return id - t.id; } }compare(Truck t)compare(Truck t1, Truck t2)compareTo(Truck t)compareTo(Truck t1, Truck t2)None of the above
29 How many lines does the following code output?var days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; for (int i = 0; i < days.length; i++) System.out.println(days[i]);Six.Seven.The code does not compile.The code compiles but throws an exception at runtime.
30 Which of the following fill in the blank to print out true? (Choose two.)String[] array = {"Natural History", "Science"}; var museums = _______________________; museums.set(0, "Art"); System.out.println(museums.contains("Art"));Arrays.asList(array)Arrays.asList("Natural History, Science")List.of(array)List.of("Natural History", "Science")new ArrayList<String>("Natural History", "Science")new List<String>("Natural History", "Science")
31 Which option cannot fill in the blank to print Clean socks?class Wash<T> { T item; public void clean(T item) { System.out.println("Clean " + item); } } public class LaundryTime { public static void main(String[] args) { ______________________ wash.clean("socks"); } }var wash = new Wash<String>();var wash = new Wash<>();Wash wash = new Wash();Wash wash = new Wash<String>();Wash<String> wash = new Wash<>();All of these can fill in the blank.
32 Which of the options in the graphic best represent the blocks variable?char[][] blocks = new char[][] { { 'a', 'b', 'c' }, { 'd' }, { 'e', 'f' } };Option AOption BOption COption D
33 Fill in the blank so the code prints gamma. (Choose two.)var list = Arrays.asList("alpha", "beta", "gamma"); Collections.sort(list, _______________); System.out.println(list.get(0));(s, t) ‐> s.compareTo(t)(s, t) ‐> t.compareTo(s)Comparator.comparing((String s) ‐> s.charAt(0))Comparator.comparing((String s) ‐> s.charAt(0)).reverse()Comparator.comparing((String s) ‐> s.charAt(0)).reversed()
34 How many of the following are legal declarations?float[] lion = new float[]; float[] tiger = new float[1]; float[] bear = new[] float; float[] ohMy =