Java SE 21 Developer Professional Questions and Answers
Given:
java
List
abc.stream()
.forEach(x -> {
x = x.toUpperCase();
});
abc.stream()
.forEach(System.out::print);
What is the output?
Given:
java
System.out.print(Boolean.logicalAnd(1 == 1, 2 < 1));
System.out.print(Boolean.logicalOr(1 == 1, 2 < 1));
System.out.print(Boolean.logicalXor(1 == 1, 2 < 1));
What is printed?
Given:
java
interface Calculable {
long calculate(int i);
}
public class Test {
public static void main(String[] args) {
Calculable c1 = i -> i + 1; // Line 1
Calculable c2 = i -> Long.valueOf(i); // Line 2
Calculable c3 = i -> { throw new ArithmeticException(); }; // Line 3
}
}
Which lines fail to compile?
Given:
java
var array1 = new String[]{ "foo", "bar", "buz" };
var array2[] = { "foo", "bar", "buz" };
var array3 = new String[3] { "foo", "bar", "buz" };
var array4 = { "foo", "bar", "buz" };
String array5[] = new String[]{ "foo", "bar", "buz" };
Which arrays compile? (Select 2)
Given:
java
List
List
Collections.copy(l1, l2);
l2.set(0, "d");
System.out.println(l1);
What is the output of the given code fragment?
Given:
java
public class Versailles {
int mirrorsCount;
int gardensHectares;
void Versailles() { // n1
this.mirrorsCount = 17;
this.gardensHectares = 800;
System.out.println("Hall of Mirrors has " + mirrorsCount + " mirrors.");
System.out.println("The gardens cover " + gardensHectares + " hectares.");
}
public static void main(String[] args) {
var castle = new Versailles(); // n2
}
}
What is printed?
Which of the following isn't a valid option of the jdeps command?
Which of the following statements oflocal variables declared with varareinvalid?(Choose 4)
Given:
java
try (FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
fos.write("Today");
fos.writeObject("Today");
oos.write("Today");
oos.writeObject("Today");
} catch (Exception ex) {
// handle exception
}
Which statement compiles?
Given:
java
StringBuilder result = Stream.of("a", "b")
.collect(
() -> new StringBuilder("c"),
StringBuilder::append,
(a, b) -> b.append(a)
);
System.out.println(result);
What is the output of the given code fragment?
Given:
java
import java.io.*;
class A implements Serializable {
int number = 1;
}
class B implements Serializable {
int number = 2;
}
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("o.ser");
A a = new A();
var oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(a);
oos.close();
var ois = new ObjectInputStream(new FileInputStream(file));
B b = (B) ois.readObject();
ois.close();
System.out.println(b.number);
}
}
What is the given program's output?
Given:
java
StringBuffer us = new StringBuffer("US");
StringBuffer uk = new StringBuffer("UK");
Stream
String output = stream.collect(Collectors.joining("-", "=", ""));
System.out.println(output);
What is the given code fragment's output?
Given:
java
String textBlock = """
j \
a \t
v \s
a \
""";
System.out.println(textBlock.length());
What is the output?
Given:
java
public class SpecialAddition extends Addition implements Special {
public static void main(String[] args) {
System.out.println(new SpecialAddition().add());
}
int add() {
return --foo + bar--;
}
}
class Addition {
int foo = 1;
}
interface Special {
int bar = 1;
}
What is printed?
Given:
java
public class ExceptionPropagation {
public static void main(String[] args) {
try {
thrower();
System.out.print("Dom Pérignon, ");
} catch (Exception e) {
System.out.print("Chablis, ");
} finally {
System.out.print("Saint-Émilion");
}
}
static int thrower() {
try {
int i = 0;
return i / i;
} catch (NumberFormatException e) {
System.out.print("Rosé");
return -1;
} finally {
System.out.print("Beaujolais Nouveau, ");
}
}
}
What is printed?
Given:
java
var ceo = new HashMap<>();
ceo.put("Sundar Pichai", "Google");
ceo.put("Tim Cook", "Apple");
ceo.put("Mark Zuckerberg", "Meta");
ceo.put("Andy Jassy", "Amazon");
Does the code compile?
Which of the following methods of java.util.function.Predicate aredefault methods?
Given:
java
sealed class Vehicle permits Car, Bike {
}
non-sealed class Car extends Vehicle {
}
final class Bike extends Vehicle {
}
public class SealedClassTest {
public static void main(String[] args) {
Class> vehicleClass = Vehicle.class;
Class> carClass = Car.class;
Class> bikeClass = Bike.class;
System.out.print("Is Vehicle sealed? " + vehicleClass.isSealed() +
"; Is Car sealed? " + carClass.isSealed() +
"; Is Bike sealed? " + bikeClass.isSealed());
}
}
What is printed?
Given:
java
List
frenchAuthors.add("Victor Hugo");
frenchAuthors.add("Gustave Flaubert");
Which compiles?
Given:
java
Integer frenchRevolution = 1789;
Object o1 = new String("1789");
Object o2 = frenchRevolution;
frenchRevolution = null;
Object o3 = o2.toString();
System.out.println(o1.equals(o3));
What is printed?
A module com.eiffeltower.shop with the related sources in the src directory.
That module requires com.eiffeltower.membership, available in a JAR located in the lib directory.
What is the command to compile the module com.eiffeltower.shop?
Given:
java
void verifyNotNull(Object input) {
boolean enabled = false;
assert enabled = true;
assert enabled;
System.out.println(input.toString());
assert input != null;
}
When does the given method throw a NullPointerException?
Given:
java
var _ = 3;
var $ = 7;
System.out.println(_ + $);
What is printed?
What do the following print?
java
public class Main {
int instanceVar = staticVar;
static int staticVar = 666;
public static void main(String args[]) {
System.out.printf("%d %d", new Main().instanceVar, staticVar);
}
static {
staticVar = 42;
}
}
Given:
java
public static void main(String[] args) {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException();
} finally {
throw new ArithmeticException();
}
}
What is the output?