OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
is the result of running the following program?1: package fun; 2: public class Sudoku { 3: static int[][] game = new int[6][6]; 4: 5: public static void main(String[] args) { 6: game[3][3] = 6; 7: Object[] obj = game; 8: obj[3] = "X"; 9: System.out.println(game[3][3]); 10: } 11: }6XThe code does not compile.The code compiles but throws a NullPointerException at runtime.The code compiles but throws a different exception at runtime.
52 How many of these allow inserting null values: ArrayList, LinkedList, HashSet, and TreeSet?01234
53 What is the output of the following?var threes = Arrays.asList("3", "three", "THREE"); Collections.sort(threes); System.out.println(threes);[3, three, THREE][3, THREE, three][three, THREE, 3][THREE, three, 3]None of the above
54 How many dimensions does the array reference moreBools allow?boolean[][][] bools, moreBools;One dimensionTwo dimensionsThree dimensionsNone of the above
55 What is the output of the following?20: List<Character> chars = new ArrayList<>(); 21: chars.add('a'); 22: chars.add('b'); 23: chars.clear(); 24: chars.remove(0); 25: System.out.print(chars.isEmpty() + " " + chars.length());false 0true 12The code does not compile.The code throws an exception at runtime.
56 Which fills in the blank in the method signature to allow this code to compile?import java.util.*; public class ExtendingGenerics { private static < ___________, U> U add(T list, U element) { list.add(element); return element; } public static void main(String[] args) { var values = new ArrayList<String>(); add(values, "duck"); add(values, "duck"); add(values, "goose"); System.out.println(values); } }? extends Collection<U>? implements Collection<U>T extends Collection<U>T implements Collection<U>None of the above
57 What does the following output?String[] os = new String[] { "Mac", "Linux", "Windows" }; System.out.println(Arrays.binarySearch(os, "Linux"));012The output is not defined.
58 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
59 What is the result of the following?var list = new ArrayList<String>(); list.add("Austin"); list.add("Boston"); list.add("San Francisco"); list.removeIf(a -> a.length()> 10); System.out.println(list.size());123None of the above
60 What happens when calling the following method with a non‐null and non‐empty array?public static void addStationName(String[] names) { names[names.length] = "Times Square"; }It adds an element to the array the value of which is Times Square.It replaces the last element in the array with the value Times Square.It does not compile.It throws an exception.None of the above.
61 Which is not a true statement about an array?An array expands automatically when it is full.An array is allowed to contain duplicate values.An array understands the concept of ordered elements.An array uses a zero index to reference the first element.
62 Which of the following cannot fill in the blank to make the code compile?private void output(____________<?> x) { x.forEach(System.out::println); }CollectionLinkedListTreeMapNone of these can fill in the blank.All of these can fill in the blank.
63 Which of the following fills in the blank so this code compiles?public static void getExceptions(Collection<____________> coll) { coll.add(new RuntimeException()); coll.add(new Exception()); }?? extends RuntimeException? super RuntimeExceptionNone of the above
64 What is the output of the following? (Choose two.)35: var mags = new HashMap<String, Integer>(); 36: mags.put("People", 1974); 37: mags.put("Readers Digest", 1922); 38: mags.put("The Economist", 1843); 39: 40: Collection<Integer> years = mags.values(); 41: 42: List<Integer> sorted = new ArrayList<>(years); 43: Collections.sort(sorted); 44: 45: int first = sorted.get(0); 46: System.out.println(first); 47: 48: Integer last = sorted.get(sorted.size()); 49: System.out.println(last);184319221974The code compiles but throws an exception at runtime.
65 How do you access the array element with the value of "z"?dimensions["three"][2]dimensions["three"][3]dimensions[2][2]dimensions[3][3]
66 What is the output of the following?class Magazine implements Comparable<Magazine> { private String name; public Magazine(String name) { this.name = name; } @Override public int compareTo(Magazine m) { return name.compareTo(m.name); } @Override 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()); } }highlightsNewsweekThe code does not compile.The code compiles but throws an exception at runtime.
67 Which options can fill in the blanks to print Cleaned 2 items?import java.util.*; class Wash<T __________ Collection> { T item; public void clean(T items) { System.out.println("Cleaned " + items.size() + " items"); } } public class LaundryTime { public static void main(String[] args) { Wash<List> wash = new Wash<_______________>(); wash.clean(List.of("sock", "tie")); } }extends, ArrayListextends, Listsuper, ArrayListsuper, ListNone of the above
68 How many lines does the following code output?var days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; for (int i = 1; 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.
69 What is the output of the following?var listing = new String[][] { { "Book" }, { "Game", "29.99" } }; System.out.println(listing.length + " " + listing[0].length);1 22 12 2The code does not compile.The code compiles but throws an exception at runtime.
70 What is the output of the following?Queue<String> q = new ArrayDeque<>(); q.add("snowball"); q.addLast("sugar"); q.addFirst("minnie"); System.out.println(q.peek() + " " + q.peek() + " " + q.size());sugar sugar 3sugar minnie 1minnie minnie 3minnie snowball 1The code does not compile.None of the above.
71 What is the result of the following?13: var numbers = Arrays.asList(3, 1, 4); 14: numbers.set(1, null); 15: int first = numbers.get(0); 16: int middle = numbers.get(1); 17: int last = numbers.get(3); 18: System.out.println(first + " " + middle + " " + last);The code does not compile.Line 14 throws an exception.Line 15 throws an exception.Line 16 throws an exception.Line 17 throws an exception.3null4
72 Fill in the blank so the code prints gamma.var list = Arrays.asList("alpha", "beta", "gamma"); Collections.sort(list, ___________________________); System.out.println(list.get(0));Comparator.comparing(String::length) .andCompare(s -> s.charAt(0))Comparator.comparing(String::length) .thenCompare(s -> s.charAt(0))Comparator.comparing(String::length) .thenComparing(s -> s.charAt(0))Comparator.comparing(String::length) .andCompare(s -> s.charAt(0)) .reversed()Comparator.comparing(String::length) .thenCompare(s -> s.charAt(0)) .reversed()Comparator.comparing(String::length) .thenComparing(s -> s.charAt(0)) .reversed()
73 What is the output of the following when run as java FirstName Wolfie? (Choose two.)public class FirstName { public static void main(String… names) { System.out.println(names[0]); System.out.println(names[1]); } }FirstNameWolfieThe code throws an ArrayIndexOutOfBoundsException.The code throws a NullPointerException.The code throws a different exception.
74 What does the following output?11: var pennies = new ArrayList<>(); 12: pennies.add(1); 13: pennies.add(2); 14: pennies.add(Integer.valueOf(3)); 15: pennies.add(Integer.valueOf(4)); 16: pennies.remove(2); 17: pennies.remove(Integer.valueOf(1)); 18: System.out.println(pennies);[1, 2][1, 4][2, 4][2, 3][3, 4]The code does not compile.
75 What is true of the following code? (Choose two.)private static void sortAndSearch(String… args) { var one = args[1]; Comparator<String> comp = (x, y) -> _______________; Arrays.sort(args, comp); var result = Arrays.binarySearch(args, one, comp); System.out.println(result); } public static void main(String[] args) { sortAndSearch("seed", "flower"); }If the blank contains ‐x.compareTo(y), then the code outputs 0.If the blank contains ‐x.compareTo(y), then the code outputs ‐1.If the blank contains x.compareTo(y), then the code outputs 0.If the blank contains ‐y.compareTo(x), then the code outputs 0.If the blank contains ‐y.compareTo(x), then the