---
title: "Java: Core Language & JVM Fundamentals"
description: "Test your knowledge of Java fundamentals covering OOP, the JVM, collections, generics, streams, and core language features."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/quizzes/post/java-fundamentals-quiz
---

# Java: Core Language & JVM Fundamentals

Java has quietly powered banks, Android, and countless backend systems for decades, and the language keeps evolving with records, sealed classes, and virtual threads. This quiz walks through the core language, the JVM, collections, generics, streams, and modern features. Read each question carefully, pick the single best answer, and use the explanations to sharpen your mental model.

## Questions

### 1. Which OOP principle is best described as hiding internal state and exposing behavior through methods?

- Inheritance, because a subclass hides its parent.
- Encapsulation, by keeping fields private and using accessors.
- Polymorphism, because one method has many forms.
- Abstraction, because it removes all implementation.

**Hint:** Think about the keyword you put in front of a field to protect it.

### 2. What does the `@Override` annotation give you when redefining a method from a superclass?

- It makes the method run faster at the JVM level.
- It forces the method to be called before the parent method.
- It makes the compiler verify the method actually overrides something.
- It automatically calls the superclass version for you.

**Hint:** It is a compile-time contract, not a runtime behavior.

### 3. Which statement about `abstract` classes versus interfaces in modern Java is correct?

- A class can extend multiple abstract classes.
- Interfaces can never contain any method body.
- A class can implement multiple interfaces but extend only one class.
- Abstract classes cannot have constructors.

**Hint:** Count how many of each a single class is allowed to inherit from.

### 4. What is autoboxing in Java?

- The automatic conversion between a primitive and its wrapper class.
- The JVM wrapping every object in a try-catch block.
- Packaging classes into a JAR at build time.
- Copying a primitive onto the heap for garbage collection.

**Hint:** Think about assigning an `int` into an `Integer` variable.

### 5. Why does `new String("hi") == "hi"` evaluate to `false`?

- The two strings contain different characters.
- `==` compares object references, and `new` creates a distinct object outside the pool.
- String literals are always null until assigned.
- `==` on strings always returns false in Java.

**Hint:** Ask whether `==` compares what the objects hold or where they live.

### 6. Which type is a reference type rather than a primitive?

- boolean
- double
- char
- Integer

**Hint:** Capitalization is a strong clue here.

### 7. Which collection guarantees no duplicate elements and, by default, no defined iteration order?

- ArrayList
- HashSet
- LinkedList
- TreeMap

**Hint:** The name tells you both the backing structure and the uniqueness guarantee.

### 8. What is the average time complexity of `get(key)` on a well-distributed `HashMap`?

- O(1) on average.
- O(log n) always.
- O(n) because it scans every entry.
- O(n log n) due to sorting on access.

**Hint:** Think about what hashing buys you versus a sorted tree.

### 9. Which interface should a class implement so its instances can be stored as keys in a `HashMap` and found reliably?

- It must override both `equals()` and `hashCode()`.
- It must implement `Comparable` only.
- It must implement `Serializable`.
- It must implement `Iterable`.

**Hint:** Two methods must agree with each other for hashing to work.

### 10. What does the bounded wildcard `List<? extends Number>` allow?

- Reading elements as `Number`, but not adding elements (except null).
- Adding any `Number` subtype freely.
- Storing only `String` values.
- Changing the list to hold any type at runtime.

**Hint:** Producer-extends: think about whether you are reading or writing.

### 11. What is type erasure in Java generics?

- Generic type parameters are removed at compile time and not present in bytecode.
- The JVM deletes unused classes to save memory.
- Generics are checked at runtime through reflection.
- The compiler erases the whole class if it is never instantiated.

**Hint:** Ask what survives from a generic type into the compiled bytecode.

### 12. What is the key difference between a checked and an unchecked exception?

- Checked exceptions must be declared or handled; unchecked ones need not be.
- Unchecked exceptions cannot be caught.
- Checked exceptions only occur in the JVM, not user code.
- There is no real difference; the terms are interchangeable.

**Hint:** One kind makes the compiler nag you; the other slips past it.

### 13. In a `try`-with-resources statement, when is a resource closed?

- Automatically at the end of the try block, in reverse order of creation.
- Only if you call `.close()` in a `finally` block yourself.
- When the garbage collector runs.
- Never; the resource stays open until the program exits.

**Hint:** The resource must implement `AutoCloseable` for this to work.

### 14. What does the Java compiler (`javac`) produce from a `.java` source file?

- Native machine code for the current CPU.
- Platform-independent bytecode in a `.class` file.
- An executable binary you can run directly on the OS.
- Optimized assembly tuned for the local machine.

**Hint:** The output runs on the JVM, not the bare CPU.

### 15. What is the primary job of the JIT compiler in the JVM?

- To compile frequently executed bytecode into native code at runtime.
- To translate Java source into bytecode.
- To free unused objects from the heap.
- To load `.class` files from disk.

**Hint:** Just-In-Time hints that the work happens while the program runs.

### 16. Which statement about garbage collection in the JVM is accurate?

- The JVM reclaims objects that are no longer reachable from any live reference.
- You must manually free every object with a `delete` call.
- Calling `System.gc()` guarantees immediate collection.
- Objects are collected the instant they leave scope.

**Hint:** The question is whether something can still be reached, not whether it is in scope.

### 17. What is a functional interface in Java?

- An interface with exactly one abstract method.
- Any interface annotated with `@Override`.
- An interface that only contains static methods.
- An interface that extends `Function` by name.

**Hint:** Count the abstract methods a lambda would need to implement.

### 18. What does the intermediate `Stream` operation `map` do?

- Transforms each element into a new value, producing a new stream.
- Removes elements that fail a predicate.
- Collects the stream into a `Map`.
- Immediately runs the pipeline and returns a list.

**Hint:** One element in, one transformed element out.

### 19. Why are most Stream intermediate operations described as "lazy"?

- They do no work until a terminal operation is invoked.
- They always run on a background thread.
- They cache every result forever.
- They skip elements at random to save time.

**Hint:** Nothing happens until you ask for a result.

### 20. What does the `synchronized` keyword guarantee for a block of code?

- Mutual exclusion on the monitor lock plus visibility of changes across threads.
- That the method runs faster under load.
- That exceptions inside the block are swallowed.
- That the block runs on a dedicated thread.

**Hint:** It solves two problems at once: races and stale reads.

### 21. What does declaring a field `volatile` guarantee?

- Reads and writes go to main memory, so other threads see the latest value.
- Compound operations like `count++` become atomic.
- The field can never be modified after initialization.
- The field is stored on disk instead of memory.

**Hint:** It fixes stale reads but not `x++`.

### 22. Why is an `ExecutorService` preferred over creating a `new Thread()` for each task?

- It reuses a managed pool of threads instead of spawning unbounded new ones.
- It runs tasks without using any threads at all.
- It makes every task run in a guaranteed order.
- It disables the garbage collector during execution.

**Hint:** Think about what happens if a busy server creates a new thread per request.

### 23. What does a Java `record` primarily give you?

- A concise immutable data carrier with generated constructor, accessors, `equals`, `hashCode`, and `toString`.
- A mutable class with public fields and setters.
- A logging utility for writing to disk.
- An interface for database rows.

**Hint:** It exists to kill boilerplate for plain data holders.

### 24. What does a `sealed` class or interface control?

- Exactly which classes are permitted to extend or implement it.
- Whether the class can be garbage collected.
- Whether fields are encrypted at runtime.
- That the class cannot be instantiated at all.

**Hint:** It is about closing a hierarchy to a fixed list of subtypes.

### 25. What does the local variable `var` keyword do in `var list = new ArrayList<String>();`?

- Infers the static type from the initializer at compile time.
- Makes the variable dynamically typed like in JavaScript.
- Declares a field usable across the whole class.
- Forces the variable to be `final`.

**Hint:** Static typing is preserved; only the annotation is omitted.

### 26. How does a Java `switch` expression (arrow form) differ from a classic `switch` statement?

- It returns a value and does not fall through between arms.
- It can only switch on `int` values.
- It requires a `break` after every case.
- It runs every matching case in sequence.

**Hint:** Think about fall-through and whether the construct yields a result.
