Create program flow control constructs including if/else, switch statements and expressions, loops, and break and continue statements.
Implement inheritance, including abstract and sealed types as well as record classes. Override methods, including that of the Object class. Implement polymorphism and differentiate between object type and reference type. Perform reference type casting, identify object types using the instanceof operator, and pattern matching with the instanceof operator and the switch construct.
1. The correct answer is A.
Explanation:
x is between 5 and 20
x
is 10, which satisfies both conditions in the nested if
statements (x > 5
and x < 20
). Therefore, the program prints "x is between 5 and 20"
.x is 5 or less
x
is 10, which does not satisfy the condition x <= 5
in the else
block. Therefore, this message will not be printed.x is greater than 20
x
is 10, which does not satisfy the condition x > 20
. Therefore, this message will not be printed.x
satisfies the conditions within the nested if
statements, leading to the output "x is between 5 and 20"
.2. The correct answer is D.
Explanation:
if (emp instanceof Employee) {
var (id, Person(name, age)) = emp;
System.out.println(name + " is " + age + " years old.");
}
if (emp instanceof Employee(_, Person(var name, var age))) {
System.out.println(name + " is " + age + " years old.");
}
_
) to ignore the id
field, which is not a valid technique in Java 21.if (emp instanceof Employee e) {
System.out.println(e.person().name() + " is " + e.person().age() + " years old.");
}
instanceof
without pattern matching, relying on accessor methods to extract the data.if (emp instanceof Employee(var id, Person(var name, var age))) {
System.out.println(name + " is " + age + " years old.");
}
Employee
and Person
data in a single step. It uses var
for type inference and correctly names the variables name
and age
as required.if (emp instanceof Employee(var id, var person)) {
System.out.println(person.name() + " is " + person.age() + " years old.");
}
Employee
record, it doesn’t nest the pattern matching for the Person
record, so it still requires calling accessor methods on person
.3. The correct answer is C.
Explanation:
y
is declared inside the first if
statement and is not accessible outside its block. Therefore, trying to print y
outside its scope results in a compilation error.z
is declared inside the second if
statement and is not accessible outside its block. Therefore, attempting to use z
outside its scope results in a compilation error.a
is declared outside the if
statement, so it is accessible both inside and outside the if
block. Reassigning a inside the if
block is allowed.4. The correct answer is C.
Explanation:
Weekend
dayOfWeek
is 3, which does not match cases 1 or 7, so it does not print "Weekend"
.Invalid day
dayOfWeek
matches one of the specific cases (2, 3, 4, 5, or 6).Weekday
dayOfWeek
is 3, which matches case 3. Therefore, the variable dayType
is set to "Weekday"
, and this value is printed."Weekday"
based on the given dayOfWeek
value.5. The correct answer is B.
Explanation:
A
score
is 85, which does not match the cases for 90 or 100. Therefore, it does not print "A"
.F
score
doesn’t match any of the other case
statements.switch
expression correctly, compiling without errors.B
score
is 85, which doesn’t match the case for 80 or 89."F"
based on the given score
value.6. The correct answer is A.
Explanation:
case CarType.SEDAN, CarType.HATCHBACK -> System.out.println("Compact vehicle");
case CarType.SUV -> System.out.println("Large vehicle");
case CarType.CONVERTIBLE -> System.out.println("Open-top vehicle");
Vehicle
is assignment-compatible with CarType
).case SEDAN, HATCHBACK -> System.out.println("Compact vehicle");
case SUV -> System.out.println("Large vehicle");
case CONVERTIBLE -> System.out.println("Open-top vehicle");
Vehicle
) as the selector expression, you must use fully qualified names for the enum constants. Using unqualified names (SEDAN
, HATCHBACK
, etc.) will result in a compilation error.case CarType.SEDAN || CarType.HATCHBACK -> System.out.println("Compact vehicle");
case CarType.SUV -> System.out.println("Large vehicle");
case CarType.CONVERTIBLE -> System.out.println("Open-top vehicle");
||
) in the case label, which is not valid syntax for switch statements. Multiple case labels should be separated by commas, not logical operators.case Vehicle.SEDAN, Vehicle.HATCHBACK -> System.out.println("Compact vehicle");
case Vehicle.SUV -> System.out.println("Large vehicle");
case Vehicle.CONVERTIBLE -> System.out.println("Open-top vehicle");
Vehicle
instead of CarType
. The enum constants belong to the CarType
enum, not the Vehicle
interface, so this will result in a compilation error.7. The correct answer is D.
Explanation:
case Circle c -> Math.PI * c.radius() * c.radius();
case Square s -> s.side() * s.side();
case null -> 0;
switch
expression is not exhaustive, it does not cover all possible Shape
values.default -> 0;
case Circle c -> Math.PI * c.radius() * c.radius();
case Square s -> s.side() * s.side();
case Triangle t -> 0.5 * t.base() * t.height();
default
case comes before the rest of the case
statements.case Shape s when s instanceof Circle ->
Math.PI * ((Circle)s).radius() * ((Circle)s).radius();
case Shape s when s instanceof Square ->
((Square)s).side() * ((Square)s).side();
case Shape s when s instanceof Triangle ->
0.5 * ((Triangle)s).base() * ((Triangle)s).height();
instanceof
checks instead of leveraging pattern matching, it’s missing a default
branch.case Circle c -> Math.PI * c.radius() * c.radius();
case Square s -> s.side() * s.side();
case Triangle t -> 0.5 * t.base() * t.height();
Shape
interface without an unnecessary default
case.8. The correct answer is B.
Explanation:
2
count
is incremented until it reaches 3. The labeled break
statement breaks out of the outer loop when count
equals 3.3
count
is incremented inside the inner while
loop. When count
reaches 3, the labeled break
statement (break outerLoop
) is executed, causing the control to exit the outer loop. Therefore, count
is 3 when printed.4
count
to 4 because the labeled break
statement exits the loop when count
is 3.5
count
to 5 because the labeled break
statement exits the loop when count
is 3.9. The correct answer is C.
Explanation:
5
10
15
i
to sum
. The calculations are as follows: 1 + 2 + 3 + 4 + 5 = 15.20
10. The correct answer is A.
Explanation:
9
continue
statement skips the current iteration when the number is even (num % 2 == 0
). The odd numbers in the array are 1, 3, and 5. Their sum is 1 + 3 + 5 = 9.10
12
15
continue
statement causes the loop to skip adding the even numbers.Do you like what you read? Would you consider?
Do you have a problem or something to say?