1. Which is the most accurate stance regarding java.lang.Error?
Errors like OutOfMemoryError or StackOverflowError signal serious conditions. They are unchecked and not intended for normal recovery logic.
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
Google · Java
Practice Java questions specifically asked in Google interviews – ideal for online test preparation, technical rounds and final HR discussions.
Questions
16
Tagged for this company + subject
Company
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 Google Java round.
Errors like OutOfMemoryError or StackOverflowError signal serious conditions. They are unchecked and not intended for normal recovery logic.
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.
The JMM specifies when a thread’s write becomes visible to other threads and which re-orderings are allowed. It’s about visibility and happens-before guarantees, not physical layouts or GC details.
Weak references allow GC to reclaim entries aggressively—good for caches with no hard retention. Soft references are less aggressively reclaimed (historically for caches, though guidance now favors explicit policies). Phantom references are for post-mortem cleanup with ReferenceQueue; you cannot get the referent.
Most standard collections have fail-fast iterators that throw on structural modification. ConcurrentHashMap provides weakly consistent iterators that reflect some concurrent changes without throwing.
An immutable collection cannot change—no element can be added, removed, or mutated through any reference. An unmodifiable view disallows mutations through that view, but the backing collection may still change elsewhere and those changes will appear. Immutability is safer for sharing across threads and for defensive returns; unmodifiable views are lightweight guards around a mutable source.
ExecutorService abstracts thread creation. You submit tasks, not threads; it reuses a pool, limits concurrency, offers futures for results, and supports graceful shutdown. This improves throughput and stability.
A ThreadFactory standardizes thread characteristics across a pool, aiding debugging, monitoring, and safe error handling for uncaught exceptions.
Generational hypothesis: new objects tend to be short-lived. Minor collections reclaim them quickly, while the old generation is collected less often.
Lambdas close over effectively final variables, just like anonymous classes. They do not require generating a new class per use and can target any compatible functional interface.
Good: return Optional from methods that may be empty; use terminal operations like orElse, orElseGet, orElseThrow instead of get(). Good: chain map/flatMap/filter to avoid nested ifs. Anti-patterns: use Optional for fields or parameters (adds overhead and ambiguity); call get() without checking presence; use Optional in performance-critical collections.
Only start() creates a new OS-level thread managed by the JVM. Calling run() is just a normal method call on the current thread.
Platform defaults vary and can break portability. Always specify the charset explicitly when converting bytes to characters to avoid mojibake and subtle data loss.
Deserialization can trigger gadget chains leading to remote code execution when untrusted data is accepted; the object graph is opaque, brittle across versions, and slow. Safer options: JSON or CBOR with vetted mappers, ProtoBuf/Avro/FlatBuffers for schema-driven, version-tolerant formats, or dedicated DTOs with explicit parsing. If you must deserialize, enable object filters (JEP 290), restrict classes, and do input validation.
Checked exceptions must be declared or handled; examples include IOException and SQLException. Unchecked exceptions extend RuntimeException; NullPointerException and ArithmeticException are typical. Errors are also unchecked and indicate serious VM problems.
try-with-resources auto-closes resources that implement AutoCloseable (or Closeable which extends it). This ensures deterministic cleanup even when exceptions occur.
The contract: equal objects must have equal hash codes. Violating it breaks hashing collections, causing lost or duplicate entries. Distinct objects may share a hash code; the map handles collisions.