1Z0-829 Working with Arrays and Collections • Complete Question Bank
Complete 1Z0-829 Working with Arrays and Collections question bank — all 0 questions with answers and detailed explanations.
Refer to the exhibit.
$ cat list.txt
A
B
C
D
$ cat ListDemo.java
import java.util.*;
import java.util.stream.*;
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("A", "B", "C", "D"));
list.removeIf(s -> s.compareTo("C") > 0);
System.out.println(list);
}
}
$ javac ListDemo.java && java ListDemoRefer to the exhibit.
Error log:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String
at com.example.Cache.get(Cache.java:12)
at com.example.Main.main(Main.java:10)
Source code:
public class Cache {
private Map<String, Object> store = new HashMap<>();
public <T> T get(String key, Class<T> type) {
Object value = store.get(key);
return type.cast(value);
}
}
public class Main {
public static void main(String[] args) {
Cache cache = new Cache();
cache.store.put("age", 30);
String age = cache.get("age", String.class);
System.out.println(age);
}
}Refer to the exhibit.
```
List<String> list = new ArrayList<>(List.of("A", "B", "C"));
for (String s : list) {
if (s.equals("B")) {
list.remove(s);
}
}
System.out.println(list);
```Refer to the exhibit.
List<String> list = new ArrayList<>(List.of("a","b","c"));
list.removeIf(s -> s.equals("b"));
System.out.println(list);What is the result of attempting to compile and run the following code? ```java
import java.util.*;
List<String> list = Arrays.asList("A", "B"); list.removeIf(s -> s.startsWith("A")); System.out.println(list); ```
Refer to the exhibit.
List<String> list = new ArrayList<>();
list.add("A");
List<?> list2 = list;
list2.add("B");
System.out.println(list2);Refer to the exhibit.
```java
import java.util.*;
import java.util.stream.*;
public class Test {
public static void main(String[] args) {
List<String> list = List.of("a", "b", "c");
var result = list.stream()
.filter(s -> s.startsWith("b"))
.collect(Collectors.toUnmodifiableList());
result.add("d");
System.out.println(result);
}
}
```What is the output of the following code?
List<Integer> list = List.of(0, 1, 2, 3); System.out.println(list.indexOf(0) + " " + list.lastIndexOf(3));
Refer to the exhibit.
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("A");
System.out.println(list.indexOf("A"));
System.out.println(list.lastIndexOf("A"));Drag or tap steps into the slots.
Drag a concept onto its matching description — or click a concept then click the description.
Resizable array
Doubly linked list
Hash table
Red-black tree
Hash table with buckets
Which of the following correctly describes the behavior of the following code? List<String> list = new ArrayList<>(); list.add("A"); list.add("B");
for (String s : list) {
if (s.equals("A")) {list.remove(s);
} }
System.out.println(list);
A developer is writing a method that must return a List<String> containing only the elements of an input List<String> that start with the letter "A", without modifying the original list. The developer writes:
List<String> result = input.stream() .filter(s -> s.startsWith("A")) .collect(Collectors.toList()); input.clear();
return result;
What is the result of calling this method?
A developer writes the following code in a Java 17 application:
List<String> list = new ArrayList<>(List.of("a", "b")); List<String> copy = List.copyOf(list); list.add("c"); System.out.println(copy.size());
What is printed?
A developer is using a Deque<String> to implement a stack. The developer writes the following code:
Deque<String> stack = new ArrayDeque<>(); stack.push("A"); stack.push("B"); stack.push("C"); System.out.println(stack.pop()); System.out.println(stack.peek());
What is printed?
A developer is using a HashMap<String, Integer> to count occurrences of words. The developer writes:
Map<String, Integer> counts = new HashMap<>(); counts.put("apple", 1); counts.put("banana", 2); counts.compute("apple", (k, v) -> v + 1); counts.computeIfAbsent("cherry", k -> 0); counts.computeIfPresent("banana", (k, v) -> null); System.out.println(counts);
What is the result?