1. You catch ArithmeticException and Exception in that order. Which is correct?
Catch blocks are matched top-down. If a broader type comes first, narrower types are unreachable, causing a compile-time error.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
Oracle · Java
Practice Java questions specifically asked in Oracle interviews – ideal for online test preparation, technical rounds and final HR discussions.
Questions
23
Tagged for this company + subject
Company
Oracle
View company-wise questions
Subject
Java
Explore topic-wise practice
Go through each question and its explanation. Use this page for targeted revision just before your Oracle Java round.
Catch blocks are matched top-down. If a broader type comes first, narrower types are unreachable, causing a compile-time error.
For complete preparation, combine this company + subject page with full company-wise practice and subject-wise practice. You can also explore other companies and topics from the links below.
HashMap maintains a linked list per bucket; when collisions make a bucket long (e.g., ≥8) and capacity is sufficient, it treeifies that bucket into a red-black tree. This improves worst-case lookups from O(n) to O(log n).
One: Fail fast with clear messages—include context like IDs and key parameters for quick diagnosis. Two: Do not swallow exceptions—either handle meaningfully or rethrow; always log with level and cause. Three: Use domain-specific exceptions for business errors, and keep stack traces for unexpected faults; map them to proper HTTP or API error codes for clients.
Finalizers run at GC’s discretion and can cause leaks or latency spikes. Use explicit close patterns or Cleaner for safety and determinism.
Future.get() is a blocking wait. Use the timed overload to bound waiting, and handle InterruptedException, ExecutionException, or CancellationException.
One: Impose a strict global lock ordering and acquire locks in that order everywhere. Two: Use tryLock with timeout and back off if you can’t acquire all locks. Other options: minimize lock scope, prefer immutable data, or replace coarse locks with higher-level concurrency utilities.
Use Comparable when a class has a natural, single ordering—like String or BigDecimal—and you control the source to implement compareTo. Prefer Comparator when you need multiple orderings (e.g., by name, then by date) or you can’t modify the class. Comparator keeps sorting logic separate and flexible.
Phaser allows parties to register and deregister at runtime and supports multiple synchronization phases with custom phase-advance logic, making it a superset of many barrier patterns.
Call shutdown to reject new tasks and let running tasks finish. Await for a bounded time. If tasks still run, call shutdownNow to interrupt and drain the queue. This avoids leaks and dangling threads.
Both aim for sub-millisecond pauses by doing most GC work concurrently and using barriers to keep mutators running while objects move.
There are four forms: static, instance of particular object, instance of arbitrary object of a particular type, and constructor. `objectRef::instanceMethod` is the particular object form.
`distinct` tracks seen elements to remove duplicates, requiring state. `sorted` is also stateful. `filter`, `map`, and `peek` are stateless.
`collect` is built for mutable result containers and supports parallel-friendly accumulation and combination. `reduce` targets immutable, associative reductions.
Entering a synchronized block acquires the monitor; exiting establishes a happens-before edge that flushes writes to main memory. It ensures mutual exclusion and visibility but not fairness.
Platform defaults vary and can break portability. Always specify the charset explicitly when converting bytes to characters to avoid mojibake and subtle data loss.
Generics are erased at runtime. Arrays need their reified element type. Without it, the JVM can’t enforce type safety for a new T[]. Workarounds include creating Object[] and casting, or using List<T>.
ForkJoinPool keeps per-worker deques; when a worker runs out of local tasks, it steals from the tail of another's deque, reducing contention and balancing work for divide-and-conquer algorithms.
Releasing a monitor happens-before the subsequent acquire of that monitor by another thread. This ensures visibility of changes made inside the critical section.
G1 uses regions and mixed collections to meet pause goals. CMS is concurrent but largely non-compacting and can suffer fragmentation.
At safepoints the JVM halts mutator threads. If a thread can’t poll (e.g., busy native call or hot loop with eliminated polls), the VM waits, inflating pause time.
Encounter order is defined by the source; unordered sources can produce arbitrary visitation order. Use `sorted` or ordered sources (e.g., List, LinkedHashSet) when order matters, or `forEachOrdered` in parallel.
Fork/Join targets divide-and-conquer algorithms. Workers steal tasks from others’ deques to stay busy. It’s not intended for I/O-heavy workloads nor does it preserve order.
serialVersionUID is a version stamp. Declaring it stabilizes compatibility decisions across builds and code changes. It has nothing to do with compression or encryption.