Weekend Special Limited Time Flat 70% Discount offer - Ends in 0d 00h 00m 00s - Coupon code: 70spcl

Oracle 1z0-830 Java SE 21 Developer Professional Exam Practice Test

Page: 1 / 8
Total 84 questions

Java SE 21 Developer Professional Questions and Answers

Question 1

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?

Options:

A.

1 1 1 1

B.

1 5 5 1

C.

1 1 2 3

D.

1 1 2 2

E.

5 5 2 3

Question 2

Given:

java

Optional optionalName = Optional.ofNullable(null);

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?

Options:

A.

bread:Baguette, dish:Frog legs, cheese.

B.

bread:Baguette, dish:Frog legs, no cheese.

C.

bread:bread, dish:dish, cheese.

D.

Compilation fails.

Question 3

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?

Options:

A.

cbca

B.

acb

C.

cacb

D.

abc

E.

bca

F.

cba

G.

bac

Question 4

Given:

java

interface SmartPhone {

boolean ring();

}

class Iphone15 implements SmartPhone {

boolean isRinging;

boolean ring() {

isRinging = !isRinging;

return isRinging;

}

}

Choose the right statement.

Options:

A.

Iphone15 class does not compile

B.

Everything compiles

C.

SmartPhone interface does not compile

D.

An exception is thrown at running Iphone15.ring();

Question 5

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?

Options:

A.

Is Vehicle sealed? true; Is Car sealed? true; Is Bike sealed? true

B.

Is Vehicle sealed? false; Is Car sealed? false; Is Bike sealed? false

C.

Is Vehicle sealed? true; Is Car sealed? false; Is Bike sealed? false

D.

Is Vehicle sealed? false; Is Car sealed? true; Is Bike sealed? true

Question 6

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?

Options:

A.

ok the 2024-07-10T07:17:45.523939600

B.

Compilation fails

C.

An exception is thrown

D.

ok the 2024-07-10

Question 7

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)?

Options:

A.

format4

B.

format2

C.

format3

D.

format1

Question 8

What does the following code print?

java

import java.util.stream.Stream;

public class StreamReduce {

public static void main(String[] args) {

Stream stream = Stream.of("J", "a", "v", "a");

System.out.print(stream.reduce(String::concat));

}

}

Options:

A.

Optional[Java]

B.

Java

C.

null

D.

Compilation fails

Question 9

Given:

java

var sList = new CopyOnWriteArrayList();

Which of the following statements is correct?

Options:

A.

The CopyOnWriteArrayList class is a thread-safe variant of ArrayList where all mutative operations are implemented by making a fresh copy of the underlying array.

B.

The CopyOnWriteArrayList class is not thread-safe and does not prevent interference amongconcurrent threads.

C.

The CopyOnWriteArrayList class’s iterator reflects all additions, removals, or changes to the list since the iterator was created.

D.

The CopyOnWriteArrayList class does not allow null elements.

E.

Element-changing operations on iterators of CopyOnWriteArrayList, such as remove, set, and add, are supported and do not throw UnsupportedOperationException.

Question 10

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?

Options:

A.

bim boom bam

B.

bim bam boom

C.

bim boom

D.

bim bam followed by an exception

E.

Compilation fails.

Question 11

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?

Options:

A.

42000E

B.

42 000,00 €

C.

42000

D.

42 k

Question 12

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?

Options:

A.

CopyEdit

{2=Crazy Horse, 3=Paradis Latin, 4=Le Lido, 5=Folies Bergère}

B.

{2=Crazy Horse, 3=Paradis Latin, 4=Le Lido}

C.

{}

D.

An exception is thrown at runtime.

E.

Compilation fails.

Question 13

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'});

}

}

Options:

A.

None of them

B.

stringBuilder4

C.

stringBuilder1

D.

stringBuilder3

E.

stringBuilder2

Question 14

Which two of the following aren't the correct ways to create a Stream?

Options:

A.

Stream stream = Stream.of("a");

B.

Stream stream = Stream.ofNullable("a");

C.

Stream stream = Stream.generate(() -> "a");

D.

Stream stream = Stream.of();

E.

Stream stream = new Stream();

F.

Stream stream = Stream.builder().add("a").build();

G.

Stream stream = Stream.empty();

Question 15

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?

Options:

A.

A a = new A();

B.

B b = new Test.B();

C.

A a = new Test.A();

D.

B b = new Test().new B();

E.

B b = new B();

F.

A a = new Test().new A();

Question 16

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?

Options:

A.

null

B.

It's a string with value: 42

C.

It's a double with value: 42

D.

It's an integer with value: 42

E.

It throws an exception at runtime.

F.

Compilation fails.

Question 17

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?

Options:

A.

It's either 1 or 2

B.

It's either 0 or 1

C.

It's always 2

D.

It's always 1

E.

Compilation fails

Question 18

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?

Options:

A.

It prints "Task is complete" once and throws an exception.

B.

It prints "Task is complete" twice and throws an exception.

C.

It exits normally without printing anything to the console.

D.

It prints "Task is complete" twice, then exits normally.

E.

It prints "Task is complete" once, then exits normally.

Question 19

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?

Options:

A.

[Paris]

B.

[Paris, Toulouse]

C.

[Lille, Lyon]

D.

Compilation fails

E.

[Lyon, Lille, Toulouse]

Question 20

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?

Options:

A.

Saint-Émilion

B.

Beaujolais Nouveau, Chablis, Saint-Émilion

C.

Beaujolais Nouveau, Chablis, Dom Pérignon, Saint-Émilion

D.

Rosé

Question 21

Given:

java

Stream strings = Stream.of("United", "States");

BinaryOperator operator = (s1, s2) -> s1.concat(s2.toUpperCase());

String result = strings.reduce("-", operator);

System.out.println(result);

What is the output of this code fragment?

Options:

A.

United-States

B.

United-STATES

C.

UNITED-STATES

D.

-UnitedStates

E.

-UNITEDSTATES

F.

-UnitedSTATES

G.

UnitedStates

Question 22

Given:

java

List cannesFestivalfeatureFilms = LongStream.range(1, 1945)

.boxed()

.toList();

try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {

cannesFestivalfeatureFilms.stream()

.limit(25)

.forEach(film -> executor.submit(() -> {

System.out.println(film);

}));

}

What is printed?

Options:

A.

Numbers from 1 to 25 sequentially

B.

Numbers from 1 to 25 randomly

C.

Numbers from 1 to 1945 randomly

D.

An exception is thrown at runtime

E.

Compilation fails

Question 23

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?

Options:

A.

nginx

Hall of Mirrors has 17 mirrors.

The gardens cover 800 hectares.

B.

Nothing

C.

An exception is thrown at runtime.

D.

Compilation fails at line n1.

E.

Compilation fails at line n2.

Question 24

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?

Options:

A.

1

B.

2

C.

Compilation fails

D.

ClassCastException

E.

NotSerializableException

Question 25

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?

Options:

A.

None of the above

B.

C

C.

A

D.

E

E.

B

F.

D

Page: 1 / 8
Total 84 questions