Given:
java
var deque = new ArrayDeque<>();
deque.add(1);
deque.add(2);
deque.add(3);
deque.add(4);
deque.add(5);
System.out.print(deque.peek() + " ");
System.out.print(deque.poll() + " ");
System.out.print(deque.pop() + " ");
System.out.print(deque.element() + " ");
What is printed?
Given:
java
Optional
String bread = optionalName.orElse("Baguette");
System.out.print("bread:" + bread);
String dish = optionalName.orElseGet(() -> "Frog legs");
System.out.print(", dish:" + dish);
try {
String cheese = optionalName.orElseThrow(() -> new Exception());
System.out.println(", cheese:" + cheese);
} catch (Exception exc) {
System.out.println(", no cheese.");
}
What is printed?
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
interface SmartPhone {
boolean ring();
}
class Iphone15 implements SmartPhone {
boolean isRinging;
boolean ring() {
isRinging = !isRinging;
return isRinging;
}
}
Choose the right statement.
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
package com.vv;
import java.time.LocalDate;
public class FetchService {
public static void main(String[] args) throws Exception {
FetchService service = new FetchService();
String ack = service.fetch();
LocalDate date = service.fetch();
System.out.println(ack + " the " + date.toString());
}
public String fetch() {
return "ok";
}
public LocalDate fetch() {
return LocalDate.now();
}
}
What will be the output?
Given:
java
var now = LocalDate.now();
var format1 = new DateTimeFormatter(ISO_WEEK_DATE);
var format2 = DateTimeFormatter.ISO_WEEK_DATE;
var format3 = new DateFormat(WEEK_OF_YEAR_FIELD);
var format4 = DateFormat.getDateInstance(WEEK_OF_YEAR_FIELD);
System.out.println(now.format(REPLACE_HERE));
Which variable prints 2025-W01-2 (present-day is 12/31/2024)?
What does the following code print?
java
import java.util.stream.Stream;
public class StreamReduce {
public static void main(String[] args) {
Stream
System.out.print(stream.reduce(String::concat));
}
}
Given:
java
var sList = new CopyOnWriteArrayList
Which of the following statements is correct?
Given:
java
public class BoomBoom implements AutoCloseable {
public static void main(String[] args) {
try (BoomBoom boomBoom = new BoomBoom()) {
System.out.print("bim ");
throw new Exception();
} catch (Exception e) {
System.out.print("boom ");
}
}
@Override
public void close() throws Exception {
System.out.print("bam ");
throw new RuntimeException();
}
}
What is printed?
Given:
java
double amount = 42_000.00;
NumberFormat format = NumberFormat.getCompactNumberInstance(Locale.FRANCE, NumberFormat.Style.SHORT);
System.out.println(format.format(amount));
What is the output?
Given:
var cabarets = new TreeMap<>();
cabarets.put(1, "Moulin Rouge");
cabarets.put(2, "Crazy Horse");
cabarets.put(3, "Paradis Latin");
cabarets.put(4, "Le Lido");
cabarets.put(5, "Folies Bergère");
System.out.println(cabarets.subMap(2, true, 5, false));
What is printed?
Which StringBuilder variable fails to compile?
java
public class StringBuilderInstantiations {
public static void main(String[] args) {
var stringBuilder1 = new StringBuilder();
var stringBuilder2 = new StringBuilder(10);
var stringBuilder3 = new StringBuilder("Java");
var stringBuilder4 = new StringBuilder(new char[]{'J', 'a', 'v', 'a'});
}
}
Which two of the following aren't the correct ways to create a Stream?
Given:
java
public class Test {
class A {
}
static class B {
}
public static void main(String[] args) {
// Insert here
}
}
Which three of the following are valid statements when inserted into the given program?
Given:
java
Object input = 42;
String result = switch (input) {
case String s -> "It's a string with value: " + s;
case Double d -> "It's a double with value: " + d;
case Integer i -> "It's an integer with value: " + i;
};
System.out.println(result);
What is printed?
Given:
java
public class Test {
static int count;
synchronized Test() {
count++;
}
public static void main(String[] args) throws InterruptedException {
Runnable task = Test::new;
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(count);
}
}
What is the given program's output?
Given:
java
ExecutorService service = Executors.newFixedThreadPool(2);
Runnable task = () -> System.out.println("Task is complete");
service.submit(task);
service.shutdown();
service.submit(task);
What happens when executing the given code fragment?
Given:
java
var frenchCities = new TreeSet
frenchCities.add("Paris");
frenchCities.add("Marseille");
frenchCities.add("Lyon");
frenchCities.add("Lille");
frenchCities.add("Toulouse");
System.out.println(frenchCities.headSet("Marseille"));
What will be 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
Stream
BinaryOperator
String result = strings.reduce("-", operator);
System.out.println(result);
What is the output of this code fragment?
Given:
java
List
.boxed()
.toList();
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
cannesFestivalfeatureFilms.stream()
.limit(25)
.forEach(film -> executor.submit(() -> {
System.out.println(film);
}));
}
What is printed?
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?
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
interface A {
default void ma() {
}
}
interface B extends A {
static void mb() {
}
}
interface C extends B {
void ma();
void mc();
}
interface D extends C {
void md();
}
interface E extends D {
default void ma() {
}
default void mb() {
}
default void mc() {
}
}
Which interface can be the target of a lambda expression?