List<String> museums = new ArrayList<>(1);
museums.add("Natural History");
museums.add("Science");
museums.add("Art");
museums.remove(2);
System.out.println(museums);
[Natural History, Science]
[Natural History, Art, Science]
[]String lions = new String[];
String[] tigers = new String[1] {"tiger"};
String bears[] = new String[] {};
String ohMy [] = new String[0] {};
public class News<__________> {}
?
onlyN
only?
and N
News
, and Object
N
, News
, and Object
26: List<String> strings = new ArrayList<?>();
27: var ints = new HashSet<Integer>();
28: Double dbl = 5.0;
29: ints.add(2);
30: ints.add(null);
int[][] blue = new int[2, 2];
int[][] blue = new int[2], [2];
int[][] blue = new int[2][2];
int[][] blue = new int[2 x 2];
var q = new ArrayDeque<String>();
q.offer("snowball");
q.offer("minnie");
q.offer("sugar");
System.out.print(q.peek() + " " + q.peek() + " " + q.size());
sugar sugar 3
sugar minnie 1
snowball minnie 1
snowball snowball 3
ArrayList
, HashSet
ArrayList
, TreeMap
ArrayList
, TreeSet
LinkedList
, HashSet
LinkedList
, TreeMap
LinkedList
, TreeSet
1: package fun;
2: public class Sudoku {
3: static int[][] game;
4:
5: public static void main(String[] args) {
6: game[3][3] = 6;
7: Object[] obj = game;
8: game[3][3] = "X";
9: System.out.println(game[3][3]);
10: } }
X
NullPointerException
at runtime.Comparator<String>
so that it sorts the longest strings first. You may assume there are no null
values. Which method could implement such a comparator?
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
public int compare(String s1, String s2) {
return s2.length() - s1.length();
}
public int compare(Object obj1, Object obj2) {
String s1 = (String) obj1;
String s2 = (String) obj2;
return s1.length() - s2.length();
}
public int compare(Object obj1, Object obj2) {
String s1 = (String) obj1;
String s2 = (String) obj2;
return s2.length() - s1.length();
}
var days = new String[] { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };
for (int i = 1; i < days.length; i++)
System.out.println(days[i]);
true
at runtime?
var c = new _________________ <String>();
c.add("pen");
c.remove("pen");
System.out.println(c.isEmpty());
ArrayDeque
ArrayList
TreeMap
TreeSet
import java.util.*;
public class Garden {
private static void sortAndSearch(String x, String y) {
var coll = Arrays.asList(x,y);
Collections.sort(coll);
_________ result = Collections.binarySearch(coll, x);
System.out.println(result);
}
public static void main(String[] args) {
sortAndSearch("seed", "flower");
} }
int
, then the code outputs 0
.int
, then the code outputs 1
.int
, then the code does not compile.String
, then the code outputs flower
.String
, then the code outputs seed
.String
, then the code does not compile.public void greek() {
[][]String alpha;
[]String beta;
String[][] gamma;
String[] delta[];
String epsilon[][];
var[][] zeta;
}
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 3
2 56
3 3
3 56
java Copier.java
with no arguments? (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: } }
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'));
ArrayList
into the blank, the code prints 1 false
.ArrayList
into the blank, the code does not compile.HashMap
into the blank, the code prints 1 false
.HashMap
into the blank, the code does not compile.HashSet
into the blank, the code prints 1 false
.HashSet
into the blank, the code does not compile.import java.util.*;
record Magazine(String name) {
public int compareTo(Magazine m) {
return name.compareTo(m.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());
} }
Magazine[name=highlights]
Magazine[name=Newsweek]
null
11: char[][] ticTacToe = new char[3][3];
12: ticTacToe[1][3] = 'X';
13: ticTacToe[2][2] = 'X';
14: ticTacToe[3][1] = 'X';
15: System.out.println(ticTacToe.length + " in a row!");
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 R
Line S
Line T
Line U
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]
5: var x = new ArrayDeque<Integer>();
6: x.offer(18);
7: x.offer(5);
8: x.push(13);
9: System.out.println(x.poll() + " " + x.poll());
13 5
13 18
18 5
18 13
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.)
ArrayList
HashMap
HashSet
SortedArray
TreeMap
TreeSet
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]
public static void throwOne(Collection<____________> coll) {
var iter = coll.iterator();
if (iter.hasNext())
throw iter.next();
}
?
? extends RuntimeException
? super RuntimeException
T
T extends RuntimeException
T super RuntimeException
int[][] nums = new int[2][1];
int[] nums[] = new int[2][1];
int[] nums[] = new int[][] { { 0 }, { 0 } };
int[] nums[] = new int[][] { { 0, 0 } };
var laptops = new String[] { "Linux", "Mac", "Windows" };
var desktops = new String[] { "Mac", "Linux", "Windows" };
var search = Arrays.binarySearch(laptops, "Linux");
var mismatch1 = Arrays.mismatch(laptops, desktops);
var mismatch2 = Arrays.mismatch(desktops, desktops);
System.out.println(search + " " + mismatch1 + " " + mismatch2);
-1 0 -1
is guaranteed-1 -1 0
is guaranteed0 -1 0
is guaranteed0 0 -1
is guaranteedmain()
method doesn't compile or points to a class that doesn't compile?
1: interface Comic<S> {
2: void draw(S s);
3: }
4: class ComicClass<S> implements Comic<S> {
5: public void draw(S s) {
6: System.out.println(s);
7: }
8: }
9: class SnoopyClass implements Comic<Snoopy> {
10: public void draw(Snoopy s) {
11: System.out.println(s);
12: }
13: }
14: class SnoopyComic implements Comic<Snoopy> {
15: public void draw(S s) {
16: System.out.println(s);
17: }
18: }
19: public class Snoopy {
20: public static void main(String[] args) {
21: Comic<Snoopy> s1 = s -> System.out.println(s);
22: Comic<Snoopy> s2 = new ComicClass<>();
23: Comic<Snoopy> s3 = new SnoopyClass();
24: Comic<Snoopy> s4 = new SnoopyComic();
25: } }
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)
var days = new String[] { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };
for (int i = 0; i < days.length; i++)
System.out.println(days[i]);
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")
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<>();
blocks
variable?
var blocks = new char[][] {
{ 'a', 'b', 'c' }, { 'd' }, { 'e', 'f' } };
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()
float[] lion = new float[];
float[] tiger = new float[1];
float[] bear = new[] float;
float[] ohMy = new[1] float;
ArrayIndexOutOfBoundsException
?
var matrix = new String[1][2];
matrix[0][0] = "Don't think you are, know you are."; // m1
matrix[0][1] = "I'm trying to free your mind Neo"; // m2
matrix[1][0] = "Is all around you "; // m3
matrix[1][1] = "Why oh why didn't I take the BLUE pill?"; // m4
m1
m2
m3
m4
list
of type List<Integer>
. Which method allows you to pass that List
and return an immutable Set
containing the same elements?
List.copyOf(list)
List.of(list)
Set.copyOf(list)
Set.of(list)
var os = new String[] { "Mac", "Linux", "Windows" };
Arrays.sort(os);
System.out.println(Arrays.binarySearch(os, "RedHat"));
System.out.println(Arrays.binarySearch(os, "Mac"));
-1
-2
-3
0
1
2
var names = new HashMap<String, String>();
names.put("peter", "pan");
names.put("wendy", "darling");
var first = names.entrySet(); // line x1
System.out.println(first.getKey()); // line x2
peter
wendy
x1
.x2
.var q = new ArrayDeque<String>();
q.offerFirst("snowball");
q.offer("sugar");
q.offerLast("minnie");
System.out.println(q.poll());
System.out.println(q.removeFirst());
System.out.println(q.size());
sugar
minnie
snowball
1
2
3
int[][][][] nums1a, nums1b;
int[][][] nums2a[], nums2b;
int[][] nums3a[][], nums3b[][];
int[] nums4a[][][], numbs4b[][][];
var set = new HashSet<>();
var set = new HashSet<Object>();
HashSet<> set = new HashSet<Object>();
HashSet<Object> set = new HashSet<>();
HashSet<Object> set = new HashSet<Object>();
var ints = new int[] {3,1,4};
var others = new int[] {2,7,1,8};
System.out.println(Arrays.compare(ints, others));
ints
has fewer elements.0
because the arrays can't be compared.beta
.
var list = List.of("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::length)
Comparator.comparing(String::length).reversed()
12: var queue = new ArrayDeque<>();
13: queue.offer("exelsior");
14: queue.peekFirst();
15: queue.addFirst("edwin");
16: queue.removeLast();
17: System.out.println(queue);
[edwin]
[excelsior]
[edwin, excelsior]
[excelsior, edwin]
moreBools
allow?
boolean[][] bools[], moreBools;
Comparator<Integer> c = (x, y) -> y - x;
var ints = Arrays.asList(3, 1, 4);
Collections.sort(ints, c);
System.out.println(Collections.binarySearch(ints, 1));
-1
is guaranteed.0
is guaranteed.1
is guaranteed.Arrays
class?
Arrays.binarySearch()
will be accurate, but slower than if it were sorted.Arrays.binarySearch()
to get an accurate result.Arrays.binarySearch()
to get an accurate result.<>
can be inserted at positions P and R without making any other changes.<>
can be inserted at positions Q and S without making any other changes.<>
can be inserted at all four positions.ArrayList<String>
.ArrayList<String>
.ArrayList<String>
.1: import java.util.*;
2: public class Binary {
3:
4: public static void main(String… args) {
5: Arrays.sort(args);
6: System.out.println(Arrays.toString(args));
7: System.out.println(args[0]);
8: } }
null
[]
Binary
ArrayIndexOutOfBoundsException
.NullPointerException
.class Mammal {}
class Bat extends Mammal {}
class Cat extends Mammal {}
class Sat {}
class Fur<? 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
} }
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: } }
6
X
NullPointerException
at runtime.null
values: ArrayList
, LinkedList
, HashSet
, and TreeSet
?
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]
moreBools
allow?
boolean[][][] bools, moreBools;
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 1
true 0
2
[duck, duck, goose]
at runtime?
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>
String[] os = new String[] { "Mac", "Linux", "Windows" };
System.out.println(Arrays.binarySearch(os, "Linux"));
0
is guaranteed.1
is guaranteed.2
is guaranteed11: char[][] ticTacToe = new char[3,3];
12: ticTacToe[1][3] = 'X';
13: ticTacToe[2][2] = 'X';
14: ticTacToe[3][1] = 'X';
15: System.out.println(ticTacToe.length + " in a row!");
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());
0
1
2
3
public static void addStationName(String[] names) {
names[names.length] = "Times Square";
}
Times Square
.Times Square
.private void output(___________________<?> x) {
x.forEach(System.out::println);
}
Collection
LinkedList
TreeMap
public static void getExceptions(Collection<____________________> coll) {
coll.add(new RuntimeException());
coll.add(new Exception());
}
?
? extends RuntimeException
? super RuntimeException
T
T extends RuntimeException
T super RuntimeException
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);
1843
1922
1974
"z"
?
dimensions["three"][2]
dimensions["three"][3]
dimensions[2][2]
dimensions[3][3]
import java.util.*;
record Magazine(String name) implements Comparable<Magazine> {
public int compareTo(Magazine m) {
return name.compareTo(m.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());
} }
Magazine[name=highlights]
Magazine[name=Newsweek]
Cleaned 2 items
?
import java.util.*;
class Wash<T _________________ Collection> {
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
, ArrayList
extends
, List
super
, ArrayList
super
, List
var days = new String[] { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };
for (int i = 1; i <= days.length; i++)
System.out.println(days[i]);
var listing = new String[][] {
{ "Book" }, { "Game", "29.99" } };
System.out.println(listing.length + " " + listing[0].length);
1 1
1 2
2 1
2 2
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 3
sugar minnie 1
minnie minnie 3
minnie snowball 1
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);
3null4
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()
java FirstName.java Wolfie
? (Choose two.)
public class FirstName {
public static void main(String… names) {
System.out.println(names[0]);
System.out.println(names[1]);
} }
FirstName
Wolfie
ArrayIndexOutOfBoundsException
.NullPointerException
.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]
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");
}
-x.compareTo(y)
, then the code outputs 0
.-x.compareTo(y)
, then the code outputs -1
.x.compareTo(y)
, then the code outputs 0
.-y.compareTo(x)
, then the code outputs 0
.-y.compareTo(x)
, then the code outputs -1
.y.compareTo(x)
, then the code outputs 0
.y.compareTo(x)
, then the code outputs -1
.String[] nums = new String[] { "1", "9", "10" };
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
[1, 9, 10]
[1, 10, 9]
[9, 1, 10]
[9, 10, 1]
[10, 1, 9]
[10, 9, 1]
11: char[][] ticTacToe = new char[3][3];
12: ticTacToe[0][0] = 'X';
13: ticTacToe[1][1] = 'X';
14: ticTacToe[2][2] = 'X';
15: System.out.println(ticTacToe.length + " in a row!");
36: var names = new HashMap<String, String>();
37: names.put("peter", "pan");
38: names.put("wendy", "darling");
39:
40: String w = names.getOrDefault("peter");
41: String x = names.getOrDefault("peter", "x");
42: String y = names.getOrDefault("john", "x");
String
on line 40 is set to null
.String
on line 41 is set to "pan"
.String
on line 41 is set to "x"
.String
on line 42 is set to "x"
.18: List<String> list = List.of(
19: "Mary", "had", "a", "little", "lamb");
20: Set<String> set = new HashSet<>(list);
21: set.addAll(list);
22: for(String sheep : set)
23: if (sheep.length()> 1)
24: set.remove(sheep);
25: System.out.println(set);
[a, lamb, had, Mary, little]
[a]
[a, a]
public static void getExceptions(Collection<_________________> coll) {
coll.add(new RuntimeException());
coll.add(new Exception());
}
?
? extends Exception
? super Exception
T
T extends Exception
T super Exception
18.116.37.129