Java: Core Language & JVM Fundamentals
Java: Core Language & JVM Fundamentals
Up to 20 questions, shuffled on every run
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.
Answer key and explanations20 questions
The quiz above draws 20 questions at random from these 26, so a second attempt will not be the same run. Everything in the pool is listed here.
Which OOP principle is best described as hiding internal state and exposing behavior through methods?
AnswerEncapsulation, by keeping fields private and using accessors.
Encapsulation keeps fields private and mediates access through methods, protecting invariants.
What does the `@Override` annotation give you when redefining a method from a superclass?
AnswerIt makes the compiler verify the method actually overrides something.
`@Override` is a safety net: the compiler rejects the code if no matching method exists to override.
Which statement about `abstract` classes versus interfaces in modern Java is correct?
AnswerA class can implement multiple interfaces but extend only one class.
Java permits one superclass but many interfaces, which is why interfaces are the tool for mixing in capabilities.
What is autoboxing in Java?
AnswerThe automatic conversion between a primitive and its wrapper class.
Autoboxing and unboxing let primitives and their wrapper types be used interchangeably where the compiler can convert.
Why does `new String("hi") == "hi"` evaluate to `false`?
Answer`==` compares object references, and `new` creates a distinct object outside the pool.
`==` tests reference identity; use `.equals()` to compare String content. The string pool interns literals, but `new` bypasses it.
Which type is a reference type rather than a primitive?
AnswerInteger
The eight primitives (byte, short, int, long, float, double, boolean, char) are value types; their capitalized wrappers are reference types.
Which collection guarantees no duplicate elements and, by default, no defined iteration order?
AnswerHashSet
HashSet is backed by a hash table: elements are unique and iteration order is unspecified. Use LinkedHashSet for insertion order or TreeSet for sorted order.
What is the average time complexity of `get(key)` on a well-distributed `HashMap`?
AnswerO(1) on average.
A HashMap resolves keys through hashing for average O(1) access; heavy collisions can degrade a bucket, which modern Java mitigates by treeifying long buckets to O(log n).
Which interface should a class implement so its instances can be stored as keys in a `HashMap` and found reliably?
AnswerIt must override both `equals()` and `hashCode()`.
Consistent `equals()` and `hashCode()` are the contract that makes hash-based collections work; overriding one without the other breaks lookups.
What does the bounded wildcard `List<? extends Number>` allow?
AnswerReading elements as `Number`, but not adding elements (except null).
An upper-bounded wildcard is a producer: you can read `Number` out, but the unknown subtype means you cannot safely put anything in. This is the "PECS" rule.
What is type erasure in Java generics?
AnswerGeneric type parameters are removed at compile time and not present in bytecode.
Type erasure keeps generics backward compatible: type checks happen at compile time, then the parameters vanish, which is why you cannot do `new T()` or check `instanceof List<String>`.
What is the key difference between a checked and an unchecked exception?
AnswerChecked exceptions must be declared or handled; unchecked ones need not be.
Checked exceptions (subclasses of `Exception` but not `RuntimeException`) force handling at compile time; unchecked exceptions (subclasses of `RuntimeException`) do not.
In a `try`-with-resources statement, when is a resource closed?
AnswerAutomatically at the end of the try block, in reverse order of creation.
Resources declared in the try header are closed automatically in reverse order, which eliminates leaked file handles and connections.
What does the Java compiler (`javac`) produce from a `.java` source file?
AnswerPlatform-independent bytecode in a `.class` file.
`javac` emits portable bytecode; the JVM then interprets it and the JIT compiles hot paths to native code at runtime.
What is the primary job of the JIT compiler in the JVM?
AnswerTo compile frequently executed bytecode into native code at runtime.
The Just-In-Time compiler profiles running code and compiles hot bytecode to optimized native instructions, closing much of the gap with ahead-of-time languages.
Which statement about garbage collection in the JVM is accurate?
AnswerThe JVM reclaims objects that are no longer reachable from any live reference.
The garbage collector frees objects once they become unreachable; generational collectors exploit the fact that most objects die young.
What is a functional interface in Java?
AnswerAn interface with exactly one abstract method.
A functional interface has one abstract method (optionally many default/static ones), which is what makes it a valid lambda target. `@FunctionalInterface` documents and enforces this.
What does the intermediate `Stream` operation `map` do?
AnswerTransforms each element into a new value, producing a new stream.
`map` is a lazy, intermediate transformation: it returns a new stream where each element has been passed through the mapping function.
Why are most Stream intermediate operations described as "lazy"?
AnswerThey do no work until a terminal operation is invoked.
Intermediate operations build a description of the pipeline; nothing runs until a terminal operation triggers it, which enables fusion and short-circuiting.
What does the `synchronized` keyword guarantee for a block of code?
AnswerMutual exclusion on the monitor lock plus visibility of changes across threads.
`synchronized` provides both mutual exclusion (one thread in the critical section) and a happens-before memory guarantee so updates are visible to the next thread.
What does declaring a field `volatile` guarantee?
AnswerReads and writes go to main memory, so other threads see the latest value.
`volatile` ensures visibility and prevents reordering for that field, but it does not make compound operations atomic, which is a common source of bugs.
Why is an `ExecutorService` preferred over creating a `new Thread()` for each task?
AnswerIt reuses a managed pool of threads instead of spawning unbounded new ones.
An `ExecutorService` decouples task submission from thread management, reusing a bounded pool so you avoid the cost and risk of unbounded manual thread creation.
What does a Java `record` primarily give you?
AnswerA concise immutable data carrier with generated constructor, accessors, `equals`, `hashCode`, and `toString`.
A record declares its state in the header and the compiler generates the constructor, accessors, and value-based `equals`/`hashCode`/`toString`, making it ideal for immutable DTOs.
What does a `sealed` class or interface control?
AnswerExactly which classes are permitted to extend or implement it.
A sealed type restricts its subtypes to a known set via `permits`, which lets the compiler verify exhaustive `switch` handling over the hierarchy.
What does the local variable `var` keyword do in `var list = new ArrayList<String>();`?
AnswerInfers the static type from the initializer at compile time.
`var` triggers local-variable type inference: the compiler fixes the static type from the right-hand side, keeping full type safety with less noise.
How does a Java `switch` expression (arrow form) differ from a classic `switch` statement?
AnswerIt returns a value and does not fall through between arms.
A switch expression uses `->` arms, produces a value, avoids fall-through, and can require exhaustiveness, making it safer and more concise than the statement form.








