Which StringBuilder variable fails to compile? java public class StringBuilderInstantiations { public static void main(String[] args) { var stringBuilder1 = new StringBuilder(); var stringBuilder2 = new StringBuilder(10); var stringBuilder3 = new StringBuilder("Java"); var stringBuilder4 = new StringBuilder(new char[]{'J', 'a', 'v', 'a'}); } }
Correct Answer: B
In the provided code, four StringBuilder instances are being created using different constructors: * stringBuilder1: new StringBuilder() * This constructor creates an empty StringBuilder with an initial capacity of 16 characters. * stringBuilder2: new StringBuilder(10) * This constructor creates an empty StringBuilder with a specified initial capacity of 10 characters. * stringBuilder3: new StringBuilder("Java") * This constructor creates a StringBuilder initialized to the contents of the specified string "Java". * stringBuilder4: new StringBuilder(new char[]{'J', 'a', 'v', 'a'}) * This line attempts to create a StringBuilder using a char array. However, the StringBuilder class does not have a constructor that accepts a char array directly. The available constructors are: * StringBuilder() * StringBuilder(int capacity) * StringBuilder(String str) * StringBuilder(CharSequence seq) Since a char array does not implement the CharSequence interface, and there is no constructor that directly accepts a char array, this line will cause a compilation error. To initialize a StringBuilder with a char array, you can convert the char array to a String first: java var stringBuilder4 = new StringBuilder(new String(new char[]{'J', 'a', 'v', 'a'})); This approach utilizes the String constructor that accepts a char array, and then passes the resulting String to the StringBuilder constructor.
1z1-830 Exam Question 7
Given: java List<String> frenchAuthors = new ArrayList<>(); frenchAuthors.add("Victor Hugo"); frenchAuthors.add("Gustave Flaubert"); Which compiles?
Correct Answer: C,D,E
* Option A (Map<String, ArrayList<String>> authorsMap1 = new HashMap<>();) * #Compilation Fails * frenchAuthors is declared as List<String>,notArrayList<String>. * The correct way to declare a Map that allows storing List<String> is to use List<String> as the generic type,notArrayList<String>. * Fix: java Map<String, List<String>> authorsMap1 = new HashMap<>(); authorsMap1.put("FR", frenchAuthors); * Reason:The type ArrayList<String> is more specific than List<String>, and this would cause a type mismatcherror. * Option B (Map<String, ? extends List<String>> authorsMap2 = new HashMap<String, ArrayList<String>>();) * #Compilation Fails * ? extends List<String>makes the map read-onlyfor adding new elements. * The line authorsMap2.put("FR", frenchAuthors); causes acompilation errorbecause wildcard (? extends List<String>) prevents modifying the map. * Fix:Remove the wildcard: java Map<String, List<String>> authorsMap2 = new HashMap<>(); authorsMap2.put("FR", frenchAuthors); * Option C (var authorsMap3 = new HashMap<>();) * Compiles Successfully * The var keyword allows the compiler to infer the type. * However,the inferred type is HashMap<Object, Object>, which may cause issues when retrieving values. * Option D (Map<String, List<String>> authorsMap4 = new HashMap<String, ArrayList<String> >();) * Compiles Successfully * Valid declaration:HashMap<K, V> can be assigned to Map<K, V>. * Using new HashMap<String, ArrayList<String>>() with Map<String, List<String>> isallowed due to polymorphism. * Correct syntax: java Map<String, List<String>> authorsMap4 = new HashMap<String, ArrayList<String>>(); authorsMap4.put("FR", frenchAuthors); * Option E (Map<String, List<String>> authorsMap5 = new HashMap<String, List<String>>();) * Compiles Successfully * HashMap<String, List<String>> isa valid instantiation. * Correct usage: java Map<String, List<String>> authorsMap5 = new HashMap<>(); authorsMap5.put("FR", frenchAuthors); Thus, the correct answers are:C, D, E References: * Java SE 21 - Generics and Type Inference * Java SE 21 - var Keyword
1z1-830 Exam Question 8
Which of the following doesnotexist?
Correct Answer: E
1. Understanding Supplier Functional Interfaces * The Supplier<T> interface is part of java.util.function and provides valueswithout taking any arguments. * Java also provides primitive specializations of Supplier<T>: * BooleanSupplier# Returns a boolean. Exists * DoubleSupplier# Returns a double. Exists * LongSupplier# Returns a long. Exists * Supplier<T># Returns a generic T. Exists 2. What about BiSupplier<T, U, R>? * There is no BiSupplier<T, U, R> in Java. * In Java, suppliers donot take arguments, so abi-supplierdoes not exist. * If you need a function thattakes two arguments and returns a value, use BiFunction<T, U, R>. Thus, the correct answer is:BiSupplier<T, U, R> does not exist. References: * Java SE 21 - Supplier<T> * Java SE 21 - Functional Interfaces
1z1-830 Exam Question 9
Given: java Map<String, Integer> map = Map.of("b", 1, "a", 3, "c", 2); TreeMap<String, Integer> treeMap = new TreeMap<>(map); System.out.println(treeMap); What is the output of the given code fragment?
Correct Answer: F
In this code, a Map named map is created using Map.of with the following key-value pairs: * "b": 1 * "a": 3 * "c": 2 The Map.of method returns an immutable map containing these mappings. Next, a TreeMap named treeMap is instantiated by passing the map to its constructor: java TreeMap<String, Integer> treeMap = new TreeMap<>(map); The TreeMap constructor with a Map parameter creates a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys. In Java, the natural ordering for String keys is lexicographical order. Therefore, the TreeMap will store the entries in the following order: * "a": 3 * "b": 1 * "c": 2 When System.out.println(treeMap); is executed, it outputs the TreeMap in its natural order, resulting in: r {a=3, b=1, c=2} Thus, the correct answer is option F: {a=3, b=1, c=2}.
1z1-830 Exam Question 10
Given a properties file on the classpath named Person.properties with the content: ini name=James And: java public class Person extends ListResourceBundle { protected Object[][] getContents() { return new Object[][]{ {"name", "Jeanne"} }; } } And: java public class Test { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("Person"); String name = bundle.getString("name"); System.out.println(name); } } What is the given program's output?
Correct Answer: F
In this scenario, we have a Person class that extends ListResourceBundle and a properties file named Person. properties. Both define a resource with the key "name" but with different values: * Person class (ListResourceBundle):Defines the key "name" with the value "Jeanne". * Person.properties file:Defines the key "name" with the value "James". When the ResourceBundle.getBundle("Person") method is called, the Java runtime searches for a resource bundle with the base name "Person". The search order is as follows: * Class-Based Resource Bundle:The runtime first looks for a class named Person (i.e., Person.class). * Properties File Resource Bundle:If the class is not found, it then looks for a properties file named Person.properties. In this case, since the Person class is present and accessible, the runtime will load the Person class as the resource bundle. Therefore, the getBundle method returns an instance of the Person class. Subsequently, when bundle.getString("name") is called, it retrieves the value associated with the key "name" from the Person class, which is "Jeanne". Thus, the output of the program is: nginx Jeanne