Weekend Sale Limited Time 70% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: 70percent

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

Demo: 25 questions
Total 84 questions

Java SE 21 Developer Professional Questions and Answers

Question 1

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 2

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 3

Given:

java

DoubleSummaryStatistics stats1 = new DoubleSummaryStatistics();

stats1.accept(4.5);

stats1.accept(6.0);

DoubleSummaryStatistics stats2 = new DoubleSummaryStatistics();

stats2.accept(3.0);

stats2.accept(8.5);

stats1.combine(stats2);

System.out.println("Sum: " + stats1.getSum() + ", Max: " + stats1.getMax() + ", Avg: " + stats1.getAverage());

What is printed?

Options:

A.

Sum: 22.0, Max: 8.5, Avg: 5.5

B.

Sum: 22.0, Max: 8.5, Avg: 5.0

C.

An exception is thrown at runtime.

D.

Compilation fails.

Question 4

Given:

java

var _ = 3;

var $ = 7;

System.out.println(_ + $);

What is printed?

Options:

A.

10

B.

_$

C.

It throws an exception.

D.

Compilation fails.

Question 5

Given:

java

List abc = List.of("a", "b", "c");

abc.stream()

.forEach(x -> {

x = x.toUpperCase();

});

abc.stream()

.forEach(System.out::print);

What is the output?

Options:

A.

abc

B.

An exception is thrown.

C.

Compilation fails.

D.

ABC

Question 6

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 7

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 8

Given:

java

var hauteCouture = new String[]{ "Chanel", "Dior", "Louis Vuitton" };

var i = 0;

do {

System.out.print(hauteCouture[i] + " ");

} while (i++ > 0);

What is printed?

Options:

A.

Chanel

B.

Chanel Dior Louis Vuitton

C.

An ArrayIndexOutOfBoundsException is thrown at runtime.

D.

Compilation fails.

Question 9

Which of the following statements is correct about a final class?

Options:

A.

The final keyword in its declaration must go right before the class keyword.

B.

It must contain at least a final method.

C.

It cannot be extended by any other class.

D.

It cannot implement any interface.

E.

It cannot extend another class.

Question 10

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?

Options:

A.

true

B.

false

C.

A NullPointerException is thrown.

D.

A ClassCastException is thrown.

E.

Compilation fails.

Question 11

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?

Options:

A.

True

B.

False

Question 12

Given:

java

public class Test {

public static void main(String[] args) throws IOException {

Path p1 = Path.of("f1.txt");

Path p2 = Path.of("f2.txt");

Files.move(p1, p2);

Files.delete(p1);

}

}

In which case does the given program throw an exception?

Options:

A.

Neither files f1.txt nor f2.txt exist

B.

Both files f1.txt and f2.txt exist

C.

An exception is always thrown

D.

File f2.txt exists while file f1.txt doesn't

E.

File f1.txt exists while file f2.txt doesn't

Question 13

Given:

java

LocalDate localDate = LocalDate.of(2020, 8, 8);

Date date = java.sql.Date.valueOf(localDate);

DateFormat formatter = new SimpleDateFormat(/* pattern */);

String output = formatter.format(date);

System.out.println(output);

It's known that the given code prints out "August 08".

Which of the following should be inserted as the pattern?

Options:

A.

MM d

B.

MM dd

C.

MMMM dd

D.

MMM dd

Question 14

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?

Options:

A.

fos.write("Today");

B.

fos.writeObject("Today");

C.

oos.write("Today");

D.

oos.writeObject("Today");

Question 15

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 16

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 17

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 18

How would you create a ConcurrentHashMap configured to allow a maximum of 10 concurrent writer threads and an initial capacity of 42?

Which of the following options meets this requirement?

Options:

A.

var concurrentHashMap = new ConcurrentHashMap(42);

B.

None of the suggestions.

C.

var concurrentHashMap = new ConcurrentHashMap();

D.

var concurrentHashMap = new ConcurrentHashMap(42, 10);

E.

var concurrentHashMap = new ConcurrentHashMap(42, 0.88f, 10);

Question 19

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 20

Which of the following methods of java.util.function.Predicate aredefault methods?

Options:

A.

and(Predicate<? super T> other)

B.

isEqual(Object targetRef)

C.

negate()

D.

not(Predicate<? super T> target)

E.

or(Predicate<? super T> other)

F.

test(T t)

Question 21

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 22

Which of the following can be the body of a lambda expression?

Options:

A.

Two statements

B.

An expression and a statement

C.

A statement block

D.

None of the above

E.

Two expressions

Question 23

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?

Options:

A.

Line 1 and line 3

B.

Line 2 only

C.

Line 1 only

D.

Line 1 and line 2

E.

Line 2 and line 3

F.

Line 3 only

G.

The program successfully compiles

Question 24

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 25

What is the output of the following snippet? (Assume the file exists)

java

Path path = Paths.get("C:\\home\\joe\\foo");

System.out.println(path.getName(0));

Options:

A.

C:

B.

IllegalArgumentException

C.

C

D.

Compilation error

E.

home

Demo: 25 questions
Total 84 questions