public class Penguins {
public static void main(String[] args) {
var pen = new Penguins();
pen.length("penguins");
pen.length(5);
pen.length(new Object());
}
public void length(Object obj) {
if (obj instanceof String x)
System.out.println(x.length());
} }
switch
statement? (Choose two.)
var
double
int
String
char
Object
default
condition in a switch
expression? (Choose two.)
enum
are coveredBoolean
Byte
Boolean
or Byte
3: var gas = true;
4: do (
5: System.out.println("helium");
6: gas = gas ^ gas;
7: gas = !gas;
8: ) while (!gas);
helium
once.helium
repeatedly.10: int m = 0, n = 0;
11: while (m < 5) {
12: n++;
13: if (m == 3)
14: continue;
15:
16: switch (m) {
17: case 0:
18: case 1:
19: n++;
20: default:
21: n++;
22: }
23: m++;
24: }
25: System.out.println(m + " " + n);
3 10
3 12
5 10
5 12
enum Admission { ADULT, SENIOR, CHILD}
public class Movie {
public static void main(String[] args) {
var price = switch (Admission.CHILD) {
case ADULT -> 12.50;
case SENIOR, CHILD -> 10;
};
System.out.println(price);
} }
case
branches are different.case
branches has two values.switch
is hard coded.10
.10.0
.var quest = _________________;
for(var zelda : quest) {
System.out.print(zelda);
}
3
new int[] {3}
new StringBuilder("3")
List.of(3)
new String[3]
"Link"
default
branch to this switch
statement are correct? (Choose two.)
switch (numPenguins) {
case 0 : System.out.println("no penguins");
case 1 : System.out.println("one penguin");
}
switch
statement is required to declare a default
statement.default
statement must be placed after all case
statements.default
statement can be placed between any case
statements.case
statement, a default
statement does not take a parameter value.switch
statement can contain more than one default
statement.default
statement can be used only when at least one case
statement is present.void dance() {
var singer = 0;
while (singer)
System.out.print(singer++);
}
0
and then terminates.22: int magicNumber = 7;
23: var ok = switch (magicNumber) {
24: case 7 -> true; break;
25: case 9 -> { yield true }
26: case 11 -> yield true;
27: case 13 : {yield true;}
28: default -> false;
29: }
for
loops? (Choose two.)
for
loop can iterate through an array starting with the first element.for
loop can iterate through an array starting from the end.zero(0)
? (Choose two.)
public void zero(Object number) {
if (number instanceof _________________ Math.abs(n) == 0)
System.out.println("zero");
else
System.out.println("non-zero");
}
Integer n ||
, the code does not compile.Integer n ||
, the output is zero
.Integer n ||
, the output is non-zero
.int n &&
, the code does not compile.int n &&
, the output is zero
.int n &&
, the output is non-zero
.package planning;
public class ThePlan {
public static void main(String[] input) {
var plan = 1;
plan = plan++ + --plan;
if(plan==1) {
System.out.print("Plan A");
} else { if(plan==2) System.out.print("Plan B"); }
} else System.out.print("Plan C");
}
}
}
Plan A
Plan B
Plan C
23: var race = "";
24: loop:
25: do {
26: race += "x";
27: break loop;
28: } while (true);
29: System.out.println(race);
x
.x
.int count = 0;
char letter = 'A';
switch (letter) {
case 'A' -> count++;
case 'B' -> count++;
}
System.out.println(count);
0
1
2
perform()
method to produce the same output on any nonempty input? (Choose two.)
public void perform(String[] circus) {
for (int i=circus.length-1; i>=0; i--)
System.out.print(circus[i]);
}
for (int i=circus.length; i>0; i--)
System.out.print(circus[i-1]);
for-reversed (String c = circus)
System.out.print(c);
for (var c : circus)
System.out.print(c);
for (var i=0; i<circus.length; i++)
System.out.print(circus[circus.length-i-1]);
for (int i=circus.length; i>0; i--)
System.out.print(circus[i+1]);
for-each (String c circus)
System.out.print(c);
var bottles = List.of("glass", "plastic", "can");
for (int type = 1; type < bottles.size();) {
System.out.print(bottles.get(type) + "-");
if(type < bottles.size()) break;
}
System.out.print("end");
glass-end
glass-plastic-can-end
plastic-end
plastic-can-end
final var GOOD = 100;
var score = 10;
switch (score) {
default:
1 : System.out.print("1-");
-1 : System.out.print("2-"); break;
4,5 : System.out.print("3-");
6 : System.out.print("4-");
9 : System.out.print("5-");
}
1-
1-2-
2-
3-
4-
package dinosaur;
public class Park {
public final static void main(String… arguments) {
int pterodactyl = 8;
long triceratops = 3;
if(pterodactyl % 3> 1 + 1)
triceratops++;
triceratops--;
System.out.print(triceratops);
} }
2
3
4
red
allows the following application to compile?
package tornado;
public class Kansas {
public static void main(String[] args) {
int colorOfRainbow = 10;
_________________ red = 5;
switch(colorOfRainbow) {
default:
System.out.print("Home");
break;
case red:
System.out.print("Away");
} } }
long
double
int
var
String
ArrayList
? (Choose two.)
public void meow(Collection<String> kitties) {
if (kitties instanceof List c) {
System.out.println("L " + c.size());
} else if (kitties instanceof Map c) {
c = new TreeMap<>(); // x1
System.out.println("M " + c.size());
} else {
System.out.println("E " + c.size());
}
}
x1
.L0
.E0
.magic()
method contain compilation errors?
10: public void magic() {
11: do {
12: int trick = 0;
13: LOOP: do {
14: trick++;
15: } while (trick < 2--);
16: continue LOOP;
17: } while (1> 2);
18: System.out.println(trick);
19: }
println
to have the code flow follow the arrow in this diagram?
break;
break letters;
break numbers;
continue;
continue letters;
continue numbers;
package dessert;
public class IceCream {
public final static void main(String… args) {
var flavors = 30;
int eaten = 0;
switch(flavors) {
case 30: eaten++;
case 40: eaten+=2;
default: eaten--;
}
System.out.print(eaten);
} }
1
2
3
var
cannot be used in a switch
statement.while (!false) {}
do {}
for( : ) {}
do {} while (true);
while {}
for( ; ; ) {}
public void m(Object obj) {
if (obj instanceof LocalDate date)
System.out.println(date);
else
System.out.println(date);
}
public void n(Object obj) {
if (obj instanceof LocalDate date)
return;
else
System.out.println(date);
}
public void o(Object obj) {
if (!obj instanceof LocalDate date)
return;
else
System.out.println(date);
}
public void p(Object obj) {
if (!(obj instanceof LocalDate date))
return;
else
System.out.println(date);
}
public void q(Object obj) {
if (!obj instanceof LocalDate date)
return;
System.out.println(date);
}
public void r(Object obj) {
if (!(obj instanceof LocalDate date))
return;
System.out.println(date);
}
for (int k=0; k < 5; k++) {}
for (int k=1; k <= 5; k++) {}
int k=0; do { } while(k++ < 5);
int k=0; while (k++ < 5) {}
int count = 0;
var stops = new String[] { "Washington", "Monroe",
"Jackson", "LaSalle" };
while (count < stops.length)
if (stops[++count].length() < 8)
break;
else continue;
System.out.println(count);
0
1
2
3
int hops = 0;
int jumps = 0;
jumps = hops++;
if(jumps)
System.out.print("Jump!");
else
System.out.print("Hop!");
Jump!
Hop!
for
loop if beta
always returns false
?
for (alpha; beta; gamma) {
delta;
}
alpha
alpha, beta
alpha, beta, gamma
alpha, gamma
alpha, gamma, beta
boolean balloonInflated = false;
do {
if (!balloonInflated) {
balloonInflated = true;
System.out.print("inflate-");
}
} while (!balloonInflated);
System.out.println("done");
done
inflate-done
switch
expressions and switch
statements? (Choose three.)
case
.switch
expression supports break
.switch
statement supports break
.switch
expression is more compact.switch
statement is more compact.if (numChipmunks == 1)
System.out.println("One chipmunk");
if (numChipmunks == 2)
System.out.println("Two chipmunks");
if (numChipmunks == 3)
System.out.println("Three chipmunks");
switch (numChipmunks) {
case 1: System.out.println("One chipmunk");
case 2: System.out.println("Two chipmunks");
case 3: System.out.println("Three chipmunks");
}
if (numChipmunks == 1)
System.out.println("One chipmunk");
else if (numChipmunks == 2)
System.out.println("Two chipmunks");
else if (numChipmunks == 3)
System.out.println("Three chipmunks");
do
/while
loop requires a body.while
loop cannot be exited early with a return
statement.while
loop requires a conditional expression.do
/while
loop executes the body (if present) at least once.do
/while
loop cannot be exited early with a return
statement.while
loop executes the body (if present) at least once.enum Season { SPRING, SUMMER, WINTER }
public class Weather {
public int getAverageTemperate(Season s) {
switch (s) {
default:
_________________ return 30;
} } }
case Season.WINTER:
case WINTER, SPRING:
case SUMMER | WINTER:
case SUMMER ->
case FALL:
package nyc;
public class TourBus {
public static void main(String… args) {
var nycTour = new String[] { "Downtown", "Uptown",
"Brooklyn" };
var times = new String[] { "Day", "Night" };
for (_________________ i<nycTour.length && j<times.length;
i++, j++)
System.out.println(nycTour[i] + "-" + times[j]);
} }
int i=1; j=1;
int i=0, j=1;
int i=1; int j=0;
int i=1, int j=0;
int i=1, j=0;
public class Penguins {
public static void main(String[] args) {
var pen = new Penguins();
pen.length("penguins");
pen.length(5);
pen.length(new Object());
}
public void length(Object obj) {
if (obj instanceof String) {
System.out.println(obj.length());
} } }
12: public static void main(String[] args) {
13: int secret = 0;
14: for (int i = 0; i < 10; i++) {
15: while (i < 10) {
16: if (i == 5) {
17: System.out.println("if");
18: } else {
19: System.out.println("in");
20: System.out.println("else");
21: }
22: }
23: }
24: switch (secret) {
25: case 0: System.out.println("zero");
26: }
27: }
travel()
method to produce the same output on any nonempty input?
public void travel(List<Integer> roads) {
for (int w = 1; w <= roads.size(); w++)
System.out.print(roads.get(w-1));
}
for (int r = 0; r < roads.size(); r += 1)
System.out.print(roads.get(0));
for(var z : roads)
System.out.print(z);
for (int t = roads.size(); t> 0; t--)
System.out.print(roads.get(t));
for (var var : roads)
System.out.print(roads);
for (int q = roads.size(); q>= 0; q++)
System.out.print(roads.get(q));
3: final var javaVersions = List.of(17,11,8);
4: var names = List.of("JDK", "Java");
5: V: for (var e1 : javaVersions) {
6: E: for (String e2 : names)
7: System.out.println(e1 + "_" + e2);
8: break;
9: }
3.147.59.217