OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн книгу.is the output of this code?20: Predicate<String> empty = String::isEmpty; 21: Predicate<String> notEmpty = empty.negate(); 22: 23: var result = Stream.generate(() -> "") 24: .filter(notEmpty) 25: .collect(Collectors.groupingBy(k -> k)) 26: .entrySet() 27: .stream() 28: .map(Entry::getValue) 29: .flatMap(Collection::stream) 30: .collect(Collectors.partitioningBy(notEmpty)); 31: System.out.println(result);It outputs {}.It outputs {false=[], true=[]}.The code does not compile.The code does not terminate.
5 What is the result of the following program?1: public class MathFunctions { 2: public static void addToInt(int x, int amountToAdd) { 3: x = x + amountToAdd; 4: } 5: public static void main(String[] args) { 6: var a = 15; 7: var b = 10; 8: MathFunctions.addToInt(a, b); 9: System.out.println(a); } }101525Compiler error on line 3Compiler error on line 8None of the above
6 Suppose that we have the following property files and code. What values are printed on lines 8 and 9, respectively?Penguin.properties name=Billy age=1 Penguin_de.properties name=Chilly age=4 Penguin_en.properties name=Willy 5: Locale fr = new Locale("fr"); 6: Locale.setDefault(new Locale("en", "US")); 7: var b = ResourceBundle.getBundle("Penguin", fr); 8: System.out.println(b.getString("name")); 9: System.out.println(b.getString("age"));Billy and 1Billy and nullWilly and 1Willy and nullChilly and nullThe code does not compile.
7 What is guaranteed to be printed by the following code? (Choose all that apply.)int[] array = {6,9,8}; System.out.println("B" + Arrays.binarySearch(array,9)); System.out.println("C" + Arrays.compare(array, new int[] {6, 9, 8})); System.out.println("M" + Arrays.mismatch(array, new int[] {6, 9, 8}));B1B2C-1C0M-1M0The code does not compile.
8 Which functional interfaces complete the following code, presuming variable r exists? (Choose all that apply.)6: ______ x = r.negate(); 7: ______ y = () -> System.out.println(); 8: ______ z = (a, b) -> a - b;BinaryPredicate<Integer, Integer>Comparable<Integer>Comparator<Integer>Consumer<Integer>Predicate<Integer>RunnableRunnable<Integer>
9 Suppose you have a module named com.vet. Where could you place the following module-info.java file to create a valid module?public module com.vet { exports com.vet; }At the same level as the com folderAt the same level as the vet folderInside the vet folderNone of the above
10 What is the output of the following program? (Choose all that apply.)1: interface HasTail { private int getTailLength(); } 2: abstract class Puma implements HasTail { 3: String getTailLength() { return "4"; } 4: } 5: public class Cougar implements HasTail { 6: public static void main(String[] args) { 7: var puma = new Puma() {}; 8: System.out.println(puma.getTailLength()); 9: } 10: public int getTailLength(int length) { return 2; } 11: }24The code will not compile because of line 1.The code will not compile because of line 3.The code will not compile because of line 5.The code will not compile because of line 7.The code will not compile because of line 10.The output cannot be determined from the code provided.
11 Which lines in Tadpole.java give a compiler error? (Choose all that apply.)// Frog.java 1: package animal; 2: public class Frog { 3: protected void ribbit() { } 4: void jump() { } 5: } // Tadpole.java 1: package other; 2: import animal.*; 3: public class Tadpole extends Frog { 4: public static void main(String[] args) { 5: Tadpole t = new Tadpole(); 6: t.ribbit(); 7: t.jump(); 8: Frog f = new Tadpole(); 9: f.ribbit(); 10: f.jump(); 11: } }Line 5Line 6Line 7Line 8Line 9Line 10All of the lines compile.
12 Which of the following can fill in the blanks in order to make this code compile?__________ a = __________.getConnection( url, userName, password); __________ b = a.prepareStatement(sql); __________ c = b.executeQuery(); if (c.next()) System.out.println(c.getString(1));Connection, Driver, PreparedStatement, ResultSetConnection, DriverManager, PreparedStatement, ResultSetConnection, DataSource, PreparedStatement, ResultSetDriver, Connection, PreparedStatement, ResultSetDriverManager, Connection, PreparedStatement, ResultSetDataSource, Connection, PreparedStatement, ResultSet
13 Which of the following statements can fill in the blank to make the code compile successfully? (Choose all that apply.)Set<? extends RuntimeException> mySet = new _________ ();HashSet<? extends RuntimeException>HashSet<Exception>TreeSet<RuntimeException>TreeSet<NullPointerException>None of the above
14 Assume that birds.dat exists, is accessible, and contains data for a Bird object. What is the result of executing the following code? (Choose all that apply.)1: import java.io.*; 2: public class Bird { 3: private String name; 4: private transient Integer age; 5: 6: // Getters/setters omitted 7: 8: public static void main(String[] args) { 9: try(var is = new ObjectInputStream( 10: new BufferedInputStream( 11: new FileInputStream("birds.dat")))) { 12: Bird b = is.readObject(); 13: System.out.println(b.age); 14: } } }It compiles and prints 0 at runtime.It compiles and prints null at runtime.It compiles and prints a number at runtime.The code will not compile because of lines 9–11.The code will not compile because of line 12.It compiles but throws an exception at runtime.
15 Which of the following are valid instance members of a class? (Choose all that apply.)var var = 3;Var case = new Var();void var() {}int Var() { var _ = 7; return _;}String new = "var";var var() { return null; }
16 Which is true if the table is empty before this code is run? (Choose all that apply.)var sql = "INSERT INTO people VALUES(?, ?, ?)"; conn.setAutoCommit(false); try (var ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) { ps.setInt(1, 1); ps.setString(2, "Joslyn"); ps.setString(3, "NY"); ps.executeUpdate(); Savepoint sp = conn.setSavepoint(); ps.setInt(1, 2); ps.setString(2, "Kara"); ps.executeUpdate(); conn._________________; }If the blank line contains rollback(), there are no rows in the table.If the blank line contains rollback(), there is one row in the table.If the blank line contains rollback(sp), there are no rows in the table.If the blank line contains rollback(sp), there is one row in the table.The code does not compile.The code throws an exception because the second update does not set all the parameters.
17 Which is true if the contents of path1 start with the text Howdy? (Choose two.)System.out.println(Files.mismatch(path1,path2));If path2 doesn't exist, the code prints -1.If path2 doesn't exist, the code prints 0.If path2 doesn't exist, the code throws an exception.If the contents of path2 start with Hello, the code prints -1.If the contents of path2 start with Hello, the code prints 0.If the contents of path2 start with Hello, the code prints 1.
18 Which of the following types can be inserted into the blank to allow the program to compile successfully? (Choose all that apply.)1: import java.util.*; 2: final class Amphibian {} 3: abstract class Tadpole extends Amphibian {} 4: public class FindAllTadpoles { 5: public static void main(String… args) { 6: var tadpoles = new ArrayList<Tadpole>(); 7: for (var amphibian : tadpoles) { 8: ___________ tadpole = amphibian; 9: } } }List<Tadpole>BooleanAmphibianTadpoleObjectNone of the above
19 What is the result of compiling and executing the following program?1: public class FeedingSchedule { 2: public static void main(String[] args) { 3: var x = 5; 4: var j = 0; 5: OUTER: for (var i = 0; i < 3;) 6: INNER: do { 7: i++; 8: x++; 9: if (x> 10) break INNER; 10: x += 4; 11: j++; 12: } while (j <= 2); 13: System.out.println(x); 14: } }10111217The code will not compile because of line 5.The code will not compile because of line 6.
20 When printed, which String gives the same value as this text block?var pooh = """ "Oh, bother." -Pooh """.indent(1); System.out.print(pooh);"\n\"Oh, bother.\" -Pooh\n""\n \"Oh, bother.\" -Pooh\n"" \"Oh, bother.\" -Pooh\n""\n\"Oh, bother.\" -Pooh""\n \"Oh, bother.\" -Pooh"" \"Oh, bother.\" -Pooh"None of the above
21 A(n) _________________ module always contains a module-info.java file, while a(n) _________________ module always exports all its packages to other modules.automatic, namedautomatic, unnamednamed, automaticnamed, unnamedunnamed, automaticunnamed, namedNone of the above
22 What is the result of the following code?22: var treeMap = new TreeMap<Character, Integer>(); 23: treeMap.put('k', 1); 24: treeMap.put('k', 2); 25: treeMap.put('m', 3); 26: treeMap.put('M', 4); 27: treeMap.replaceAll((k, v) -> v + v); 28: treeMap.keySet() 29: .forEach(k -> System.out.print(treeMap.get(k)));26846824688268468246None of the above
23 Which of the following lines can fill in the blank to print true? (Choose all that apply.)10: public static void main(String[] args) { 11: System.out.println(test(____________________________)); 12: } 13: private static boolean test(Function<Integer,