Command Palette

Search for a command to run...

Back to the lesson: Topic 16.1 — Core Java Interview Questions
Core Java · Example 4 of 4 Java 16+

Streams are lazy and run element by element

Each element flows through the whole pipeline before the next one starts, and limit(2) stops the source early: 4 and 5 are never examined. Stream.toList() is Java 16.

A bank of the Core Java questions interviewers ask most, from the JVM and Strings to HashMap internals, generics, modern Java, concurrency and garbage collection, each with an interview-grade answer and a link to the topic that teaches it.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a Money class with a long cents and a String currency that is safe to use as a HashMap key. Prove it with a map lookup using a separate but equal object.

02

Write a thread-safe hit counter that 4 threads increment 10,000 times each, and print the total. Use the simplest correct tool.

03

Given a list of words, use a stream to count how many words start with each letter, in alphabetical order of letter.

04

Show the difference between orElse and orElseGet with a method that prints when it is called.

Explain it without notes

01

[JVM] What is the difference between the JDK, the JRE and the JVM?

02

[JVM] Is Java compiled or interpreted? Why is it called platform independent?

03

[JVM] What lives on the stack and what lives on the heap?

04

[JVM] How does class loading work, and what is parent delegation?

05

[String] Why is String immutable?

06

[String] What is the String pool, and when does == work on Strings?

07

[String] String vs StringBuilder vs StringBuffer: when do you use each?

08

[equals/hashCode] What is the equals and hashCode contract, and what breaks if you override only equals?

09

[equals/hashCode] Can you use a mutable object as a HashMap key?

10

[Collections] How does HashMap work internally?

11

[Collections] What changed in HashMap in Java 8, and why?

12

[Collections] ArrayList vs LinkedList: which is faster, and when?

13

[Collections] HashMap vs Hashtable vs ConcurrentHashMap?

14

[Collections] What are fail-fast and fail-safe iterators?

15

[Collections] Comparable vs Comparator?

16

[Collections] What is the difference between List.of, Arrays.asList and Collections.unmodifiableList?

17

[Exceptions] Checked vs unchecked exceptions, and Error vs Exception?

18

[Exceptions] Does finally always run? What if finally has a return?

19

[Exceptions] What does try-with-resources do, and what is a suppressed exception?

20

[Generics] What is type erasure, and what can't you do because of it?

21

[Generics] Explain wildcards and PECS.

22

[OOP] Overloading vs overriding? Can you override a static method?

23

[OOP] Abstract class vs interface, after Java 8?

24

[OOP] Is Java pass-by-value or pass-by-reference?

25

[OOP] final vs finally vs finalize?

26

[Java 8+] What were the main features of Java 8?

27

[Java 8+] Intermediate vs terminal stream operations, and map vs flatMap?

28

[Java 8+] How should Optional be used, and how should it not?

29

[Java 8+] What is a record, and what does the compiler generate?

30

[Java 8+] What are sealed classes, and how do they work with pattern matching?

31

[Concurrency] synchronized vs volatile?

32

[Concurrency] Runnable vs Callable, and why use an ExecutorService?

33

[Concurrency] What is a deadlock, and how do you prevent one?

34

[Concurrency] How does ConcurrentHashMap achieve thread safety?

35

[Concurrency] What are virtual threads, and when do they help?

36

[GC] How does garbage collection work, and which collector is the default?

37

[GC] Can a Java program have a memory leak?

38

[GC] OutOfMemoryError vs StackOverflowError?

Streams are lazy and run element by element Java 16+
Sign in to run this example in your browser.

Expected output

pipeline built, nothing ran yet
filter 1
map    1
filter 2
filter 3
map    3
result [10, 30]