Like Chapter 12, this chapter contains 50 questions and is designed to simulate a real 1Z0-829 exam. Ready with your scratch paper and 90 minutes? Good luck!
package party;
import java.util.concurrent.*;
public class Plan {
ExecutorService s = Executors.newScheduledThreadPool(10);
public void planEvents() {
Runnable r1 = () -> System.out.print("Check food");
Runnable r2 = () -> System.out.print("Check drinks");
Runnable r3 = () -> System.out.print("Take out trash");
s.scheduleWithFixedDelay(r1,1,TimeUnit.HOURS); // g1
s.scheduleAtFixedRate(r2,1,1000,TimeUnit.SECONDS); // g2
s.execute(r3); // g3
s.shutdownNow();
} }
g1
does not compile.g2
does not compile.g3
does not compile.$
) and the following method is called with a double value of 960_010
, which of the following values are printed? (Choose two.)
public void print(double t) {
System.out.print(NumberFormat.getCompactNumberInstance().format(t));
System.out.print(
NumberFormat.getCompactNumberInstance(
Locale.getDefault(), Style.SHORT).format(t));
System.out.print(NumberFormat.getCurrencyInstance().format(t));
}
960K
9M
9.6M
$960,010.00
$960,000.00
900 thousand
9 million
peacocks
table with two columns: name
and rating
. What does the following code output if the table is empty?
var url = "jdbc:hsqldb:file:birds";
var sql = "SELECT name FROM peacocks WHERE name = ?";
try (var conn = DriverManager.getConnection(url);
var stmt = conn.prepareStatement(sql)) { // s1
stmt.setString(1, "Feathers");
try (var rs = stmt.executeQuery()) { // s2
while (rs.hasNext()) {
System.out.println(rs.next());
}
}
}
false
true
s1
.s2
.1: import static java.util.stream.Collectors.*;
2: import java.util.*;
3: public record Goat(String food) {
4: public static void main(String[] args) {
5: var goats = List.of(
6: new Goat("can"),
7: new Goat("hay"),
8: new Goat("shorts"),
9: new Goat("hay"));
10:
11: goats.stream()
12: .collect(groupingBy(Goat::food))
13: .entrySet()
14: .stream()
15: .filter(e -> e.getValue().size() == 2)
16: .map(e -> e.getKey())
17: .collect(partitioningBy(e -> e.isEmpty()))
18: .get(false)
19: .stream()
20: .sorted()
21: .forEach(System.out::print);
22: } }
canshorts
hay
hayhay
shortscan
abstract class TShirt {
abstract int insulate();
public TShirt() {
System.out.print("Starting…");
}
}
public class Wardrobe {
abstract class Sweater extends TShirt {
int insulate() {return 5;}
}
private void dress() {
final class Jacket extends Sweater { // v1
int insulate() {return 10;}
};
final TShirt outfit = new Jacket() { // v2
int insulate() {return 20;}
};
System.out.println("Insulation:"+outfit.insulate());
}
public static void main(String… snow) {
new Wardrobe().dress();
} }
Starting…Insulation:20
Starting…Insulation:40
v1
.v2
.List<String> a = new ArrayList();
List<> b = new ArrayList();
List<String> c = new ArrayList<>();
List<> d = new ArrayList<>();
List<String> e = new ArrayList<String>();
List<> f = new ArrayList<String>();
reduce()
method is used with a lambda expression, then it should be stateful.ParallelStream<T>
.package tax;
public class Accountant {
public void doTaxes() throws Throwable {
try {
throw new NumberFormatException();
} catch (ClassCastException
| ArithmeticException e) { // p1
System.out.println("Math");
} catch (IllegalArgumentException | Exception f) { // p2
System.out.println("Unknown");
} }
public static void main(String[] numbers) throws Throwable {
try {
new Accountant().doTaxes();
} finally {
System.out.println("Done!");
} } }
Math
Unknown
Unknown
followed by Done!
p1
.p2
.1: public record Bee(boolean gender, String species) {
2: Bee {
3: this.gender = gender;
4: this.species = species;
5: }
6: Bee(boolean gender) {
7: this(gender, "Honeybee");
8: }
9: @Override public String getSpecies() {
10: return species;
11: }
12: @Override public String toString() {
13: return species;
14: } }
var trainDay = LocalDate.of(2022, 5, 14);
var time = LocalTime.of(10, 0);
var zone = ZoneId.of("America/Los_Angeles");
var zdt = ZonedDateTime.of(trainDay, time, zone);
var instant = zdt.toInstant();
instant = instant.plus(1, ChronoUnit.YEARS);
System.out.println(instant);
2022-05-14T10:00-07:00[America/Los_Angeles]
2022-05-14T17:00:00Z
2023-05-14T10:00-07:00[America/Los_Angeles]
2023-05-14T17:00:00Z
interface CanClimb {
default void climb() {}
static void climb(int x) {}
}
interface CanDance {
int dance() { return 5; }
}
interface CanFly {
abstract void fly();
}
interface CanRun {
void run();
static double runFaster() {return 2.0; }
}
interface CanSwim {
abstract Long swim();
boolean test();
}
jlink
? (Choose two.)
--add-modules
are included in the runtime image.--add-modules
are included in the runtime image.requires
directive in the module-info
file are included in the runtime image.requires
directive in the module-info
file and their transitive dependencies are included in the runtime image.var builder = new StringBuilder("Leaves growing");
do {
builder.delete(0, 5);
} while (builder.length()> 5);
System.out.println(builder);
Leaves growing
ing
wing
1: package fruit;
2: enum Season {
3: SPRING(1), SUMMER(2), FALL(3), WINTER(4);
4: public Season(int orderId) {}
5: }
6: public class PickApples {
7: public static void main(String… orchard) {
8: final Season s = Season.FALL;
9: switch(s) {
10: case Season.FALL:
11: System.out.println("Time to pick!");
12: default:
13: System.out.println("Not yet!");
14: } } }
Time to pick!
Time to pick!
followed by Not yet!
switch
statement that takes an int
value for numEggs
? (Choose two.)
boolean hasEggs;
switch (numEggs) {
case 0 : hasEggs = true; break;
default : hasEggs = false; break;
}
System.out.print(hasEggs);
boolean hasEggs = switch (numEggs) {
case 0 -> true
default -> false
};
System.out.print(hasEggs);
boolean hasEggs = switch (numEggs) {
case 0 -> true;
default -> false;
};
System.out.print(hasEggs);
boolean hasEggs = switch (numEggs) {
case 0 -> return true;
default -> return false;
};
System.out.print(hasEggs);
boolean hasEggs = switch (numEggs) {
case 0 -> {yield true};
default -> {yield false};
};
System.out.print(hasEggs);
boolean hasEggs = switch (numEggs) {
case 0 -> {yield true;}
default -> {yield false;}
};
System.out.print(hasEggs);
boolean hasEggs = switch (numEggs) {
case 0 -> {return true;}
default -> {return false;}
};
System.out.print(hasEggs);
10: long sum = IntStream.of(4, 6, 8)
11: .boxed()
12: .parallel()
13: .mapToInt(x -> x)
14: .sum();
15: System.out.print(sum);
double
.int
.unboxed()
.main()
method?
public class Tennis {
public static void main(String[] game) {
String[] balls = new String[1];
int[] scores = new int[1];
balls = null;
scores = null;
} }
import java.util.*;
public class Yellow {
public static void main(String[] args) {
List list = Arrays.asList("Sunny");
method(list); // c1
}
private static void method(Collection<?> x) { // c2
x.forEach(a -> {}); // c3
} }
c1
.c2
.c3
.equals()
method in this record?
public record Coin(boolean heads) {
public boolean equals(Object obj) {
if (! (obj instanceof Coin coin)) {
return false;
}
return heads == coin.heads;
} }
true
when passed in a null
.Boolean
instead of a Coin
.instanceof
.equals()
is not allowed to be implemented in a record
.package ranch;
public class Cowboy {
private int space = 5;
private double ship = space < 2 ? 3L : 10.0f; // g1
public void printMessage() {
if(ship>1) {
System.out.print("Goodbye!");
} if(ship<10 && space>=2) // g2
System.out.print("Hello!");
else System.out.print("See you again!");
}
public static final void main(String… stars) {
new Cowboy().printMessage();
} }
Hello!
Goodbye!
See you again!
g1
.g2
.package names;
import java.util.*;
import java.util.function.*;
interface ApplyFilter {
void filter(List<String> input);
}
public class FilterBobs {
static Function<String,String> first = s ->
{System.out.println(s); return s;};
static Predicate second = t -> "bob".equalsIgnoreCase(t);
public void process(ApplyFilter a, List<String> list) {
a.filter(list);
}
public static void main(String[] contestants) {
final List<String> people = new ArrayList<>();
people.add("Bob");
people.add("bob");
people.add("Tina");
people.add("Louise");
final FilterBobs f = new FilterBobs();
f.process(q -> {
q.removeIf(second);
q.forEach(first);
}, people);
} }
1: import java.util.*;
2: public class ListOfList {
3: public void create() {
4: List<?> n = new ArrayList<>();
5:
6: List<? extends RuntimeException> o
7: = new ArrayList<Exception>();
8:
9: List<? super RuntimeException> p
10: = new ArrayList<Exception>();
11:
12: List<T> q = new ArrayList<?>();
13:
14: List<T extends RuntimeException> r
15: = new ArrayList<Exception>();
16:
17: List<T super RuntimeException> s
18: = new ArrayList<Exception>();
19: } }
1: package fun;
2: public class Sudoku {
3: static int[][] game;
4: public static void main(String args[]) {
5: game[3][3] = 6;
6: Object[] obj = game;
7: obj[3] = 'X';
8: System.out.println(game[3][3]);
9: } }
6
X
NullPointerException
at runtime.JDK Internal API Suggested Replacement
sun.misc.Unsafe See http://openjdk.java.net/jeps/260
jdeps sneaky.jar
jdeps –j sneaky.jar
jdeps –s sneaky.jar
jdeps --internals sneaky.jar
jdeps -jdkinternals sneaky.jar
jdeps --jdk-internals sneaky.jar
CountSheep
class is correct?
package fence;
import java.util.concurrent.atomic.*;
public class CountSheep {
private static AtomicInteger counter = new AtomicInteger();
private Object lock = new Object();
public synchronized int increment1() {
return counter.incrementAndGet();
}
public static synchronized int increment2() {
return counter.getAndIncrement();
}
public int increment3() {
synchronized(lock) {
return counter.getAndIncrement();
} } }
increment1()
is removed.increment2()
is removed.increment3()
is removed.10: public class Transport {
11: static interface Vehicle {}
12: static class Bus implements Vehicle {}
13:
14: public static void main(String[] args) {
15: Bus bus = new Bus();
16:
17: System.out.println(null instanceof Bus);
18: System.out.println(bus instanceof Vehicle);
19: System.out.println(bus instanceof Bus);
20: System.out.println(bus instanceof ArrayList);
21: System.out.println(bus instanceof Collection);
22: } }
package fly;
public class Helicopter {
public int adjustPropellers(int length, String[] type) {
length++;
type[0] = "LONG";
return length;
}
public static void main(String[] climb) {
final var h = new Helicopter();
var length = 5;
var type = new String[1];
length = h.adjustPropellers(length, type);
System.out.print(length + "," + type[0]);
} }
5,LONG
6,LONG
5,null
6,null
var dice = new LinkedList<Integer>();
dice.offer(3);
dice.offer(2);
dice.offer(4);
System.out.print(dice.stream().filter(n -> n != 4));
2
3
[3 2]
final class Chimp extends Primate { }
non-sealed class Bonabo extends Primate { }
final class Gorilla { }
public abstract sealed class Primate permits Chimp, Bonabo {
abstract String getName();
}
requires
directive? (Choose two.)
requires direct
directive is always allowed.requires direct
directive is never allowed.requires direct
directive is sometimes allowed.requires java.base
is allowed, but redundant.requires java.base
is never allowed.requires java.base
is sometimes needed to change the meaning of a file.transformation()
method ends? (Choose two.)
public static void transformation() {
String state1 = new String("ice");
String state2 = new String("water");
String state3 = new String("mist");
state1 = state2;
state2 = state3;
state3 = state1;
}
peacocks
table with two columns: name
and rating
. What does the following code output if the table is empty?
8: var url = "jdbc:hsqldb:file:birds";
9: var sql = "SELECT name FROM peacocks WHERE name = ?";
10: try (var conn = DriverManager.getConnection(url);
11: var stmt = conn.prepareStatement(sql,
12: ResultSet.TYPE_FORWARD_ONLY,
13: ResultSet.CONCUR_READ_ONLY)) {
14:
15: stmt.setString(1, "Feathers");
16:
17: try (var rs = stmt.execute()) {
18: System.out.println(rs.next());
19: }
20: }
false
true
10: int numWorms = 0;
11: ++numWorms;
12: System.out.println(numWorms++);
13: int mask = ~numWorms;
14: System.out.println(mask);
1
2
3
-1
-2
-3
1: package bookworm;
2: import java.io.*;
3: import java.nio.file.*;
4: public class ReadEverything {
5: public void readFile(Path p) {
6: try {
7: Files.readAllLines(p)
8: .parallel()
9: .forEach(System.out::println);
10: } catch (Exception e) {}
11: }
12: public void read(Path directory) throws Exception {
13: Files.walk(directory)
14: .filter(p -> File.isRegularFile(p))
15: .forEach(x -> readFile(x));
16: }
17: public static void main(String[] b) throws IOException {
18: Path p = Path.get("collection");
19: new ReadEverything().read(p);
20: } }
var x = new ArrayDeque<>();
x.offer(18);
x.(____________);
x.(____________);
peek
, peek
peekFirst
, peekLast
poll
, poll
pop
, pop
remove
, remove
removeFirst
, removeLast
public class InitOrder {
{ System.out.print("1"); }
static { System.out.print("2"); }
public InitOrder() {
System.out.print("3");
}
public static void callMe() {
System.out.print("4");
}
public static void main(String[] args) {
callMe();
callMe();
System.out.print("5");
} }
1223445
2445
22445
223445
2233445
FileNotFoundException
ArithmeticException
IOException
BigProblem
IllegalArgumentException
RuntimeException
FileNotFoundException
is a subclass of IOException
and Long
is a subclass of Number
, what is the output of the following application?
package materials;
import java.io.*;
class CarbonStructure {
protected long count;
public abstract Number getCount() throws IOException; // q1
public CarbonStructure(int count) { this.count = count; }
}
public class Diamond extends CarbonStructure {
public Diamond() { super(15); }
public Long getCount() throws FileNotFoundException { // q2
return count;
}
public static void main(String[] cost) {
try {
final CarbonStructure ring = new Diamond(); // q3
System.out.print(ring.getCount()); // q4
} catch (IOException e) {
e.printStackTrace();
} } }
null
15
q1
. q2
.q3
.q4
.Locale
formats? (Choose two.)
iw
UA
it_ch
JA_JP
th_TH
ES_hn
import java.util.*;
public class Museums {
public static void main(String[] args) {
String[] array = {"Natural History", "Science", "Art"};
List<String> museums = Arrays.asList(array);
museums.remove(2);
System.out.println(museums);
} }
[Natural History, Science]
[Natural History, Science, Art]
static
methods.static
nested class can contain static
methods.var list = Arrays.asList("flower", "seed", "plant");
Collections.sort(list);
Collections.reverse(list);
var result = Collections.binarySearch(list,
list.get(0), Comparator.reverseOrder());
System.out.println(result);
0
1
2
javac ____________ mods -d birds com-bird/*.java *.java
-cp
-m
-p
-classpath
--classpath
--module-path
16: void play() {
17: record Toy(String name){ }
18:
19: var toys = Stream.of(
20: new Toy("Jack in the Box"),
21: new Toy("Slinky"),
22: new Toy("Yo-Yo"),
23: new Toy("Rubik's Cube"));
24:
25: var spliterator = toys.spliterator();
26: var batch1 = spliterator.trySplit();
27: var batch2 = spliterator.trySplit();
28: var batch3 = spliterator.trySplit();
29:
30: batch1.forEachRemaining(System.out::print);
31: }
Toy[name=Jack in the Box]
Toy[name=Slinky]
Toy[name=Yo-Yo]
Toy[name= Rubik's Cube]
Toy[name=Jack in the Box]Toy[name=Slinky]
Toy[name=Yo-Yo]Toy[name=Rubik's Cube]
1: package castles;
2: import java.io.*;
3: public class Palace {
4: public void openDrawbridge() throws Exception {
5: try {
6: throw new Exception("Problem");
7: } catch (IOException e) {
8: throw new IOException();
9: } catch (FileNotFoundException e) {
10: try {
11: throw new IOException();
12: } catch (Exception e) {
13: } finally {
14: System.out.println("Almost done");
15: }
16: } finally {
17: throw new RuntimeException("Unending problem");
18: } }
19: public static void main(String[] moat)
20: throws IllegalArgumentException {
21: new Palace().openDrawbridge();
22: } }
1: public class Massage {
2: var name = "Sherrin";
3: public void massage(var num) {
4: var zip = 10017;
5: var underscores = 1_001_7;
6: var _ = "";
7: } }
true
?
var lol = "lol";
var moreLols = """
lol
""";
var smiley = lol.toUpperCase() == lol;
var smirk = lol.toUpperCase() == lol.toUpperCase();
var blush = lol.toUpperCase().equals(lol);
var cool = lol.toUpperCase().equals(lol.toUpperCase());
var wink = lol.toUpperCase().equalsIgnoreCase(lol);
var yawn = lol.toUpperCase().equalsIgnoreCase(
lol.toUpperCase());
var sigh = lol.equals(moreLols);
() -> {}
(Double adder) -> {int y; System.out.print(adder); return adder;}
(Long w) -> {Long w=5; return 5;}
(int count, vote) -> count*vote
dog -> dog
name -> {name.toUpperCase()}
play()
method can be called from all classes in the com.mammal.eland
package, but not the com.mammal.gopher
package?
package com.mammal;
public class Enrichment {
____________ void play() {}
}
private
protected
public
18.222.80.236