Master Java Interviews: Overloading, HashMap, Thread Pools, GC & HTTP Essentials

This article combines practical advice for negotiating offers at Tonghuashun with a comprehensive Java interview cheat‑sheet covering method overloading vs overriding, final vs finally, sleep vs wait, HashMap insertion and resizing, reference types, HTTP request structure, status codes, thread‑pool parameters, and garbage‑collection algorithms.

IT Services Circle
IT Services Circle
IT Services Circle
Master Java Interviews: Overloading, HashMap, Thread Pools, GC & HTTP Essentials

Recent graduates who passed Tonghuashun's technical interviews are now facing HR negotiations and wonder what salary to request. In 2023 the campus hiring salary was around 20x15 (approximately 300k per year), and this year Tonghuashun has reopened Java backend positions with larger hiring scales.

Java Interview Questions and Answers

1. What is the difference between overloading and overriding?

Overloading

means defining multiple methods with the same name in the same class but with different parameter lists. Overriding means a subclass provides a new implementation for a method defined in its superclass. Overloading occurs within a single class; the compiler selects the appropriate method based on the argument types at compile time. Overriding requires the subclass method to have the same signature as the superclass method and is indicated with the @Override annotation.

2. What is the difference between final and finally ?

final

is a modifier that restricts program structure: it can be applied to classes (prevent inheritance), methods (prevent overriding), or variables (make them immutable after assignment). finally is a block in the try‑catch‑finally construct that always executes, typically used to release resources such as streams or database connections, regardless of whether an exception was thrown.

3. What is the difference between sleep() and wait() and when to use each?

Thread.sleep()

pauses the current thread for a specified time without releasing any locks; it is used to control execution timing. Object.wait() must be called inside a synchronized block; it releases the object's lock and puts the thread into the object's wait set until another thread calls notify() or notifyAll(). It is used for thread coordination (e.g., producer‑consumer).

4. How does HashMap.put() work?

The put() operation follows these steps (JDK 8):

Compute the hash of the key and locate the bucket index.

If the bucket is empty, create a new Node (Entry) and store the key‑value pair.

If the bucket already contains entries, compare the hash and key with existing nodes.

If a matching key is found, replace its value.

If not, insert the new node at the head of the linked list or tree.

If the bucket becomes a linked list longer than the threshold (default 8) and the table size is at least 64, the list is transformed into a red‑black tree to improve lookup performance.

5. How does HashMap resize?

When the load factor (default 0.75) is exceeded, the table size is doubled. Each existing entry's index is recomputed; because the new capacity is a power of two, the new index is either the old index or old index plus the old capacity, depending on the new high‑order bit of the hash. This redistribution spreads entries evenly without recomputing the full hash.

6. Shallow copy vs deep copy

A shallow copy duplicates the object itself and copies primitive fields, but reference fields still point to the same objects. A deep copy recursively copies all referenced objects, producing a completely independent object graph.

7. The four types of Java references

Strong : ordinary references; objects are not reclaimed as long as a strong reference exists.

Soft : SoftReference; cleared only when memory is low, useful for caches.

Weak : WeakReference; cleared on any GC cycle, used in WeakHashMap.

Phantom : PhantomReference; never returns the referent, used with a ReferenceQueue to track object reclamation (e.g., for direct byte buffers).

8. HTTP request structure

A request consists of:

Request line (method, URI, HTTP version)

Headers (Host, User‑Agent, Content‑Type, etc.)

Blank line

Optional body (e.g., for POST)

9. Common HTTP status codes and when to use 206 / 304

1xx: informational, 2xx: success, 3xx: redirection, 4xx: client error, 5xx: server error.

206 Partial Content is returned when the client requests a byte range (e.g., for resumable downloads or video streaming).

304 Not Modified is used when the resource has not changed since the client’s last request; the server returns only headers, allowing the client to use its cached copy.

10. HTTP caching

Strong cache is controlled by Cache-Control or Expires headers and can be used without contacting the server. When strong cache expires, conditional requests with If-Modified-Since or If-None-Match are sent; a 304 response lets the client reuse its cached representation.

11. Thread pool parameters and principle

The ThreadPoolExecutor constructor takes seven arguments:

corePoolSize : number of core threads kept alive.

maximumPoolSize : maximum total threads.

keepAliveTime and unit : idle time before non‑core threads are terminated.

workQueue : queue that holds tasks awaiting execution.

threadFactory : creates new threads with custom settings.

handler : rejection policy when the queue is full and the pool has reached its maximum size.

Task submission follows these steps: check pool state, try to add a core thread, enqueue if possible, create non‑core threads if the queue is full, or apply the rejection handler.

12. Garbage collection mechanisms

Algorithms:

Mark‑Sweep : mark live objects, then sweep away unmarked ones (may leave fragmentation).

Mark‑Copy : copy live objects to a new space, clearing the old space (no fragmentation, but uses half the heap).

Mark‑Compact : after marking, shift live objects to one end and compact, eliminating fragmentation.

Generational : separate young and old generations; young generation uses copy collection, old generation uses mark‑sweep or mark‑compact.

Collectors (HotSpot): Serial, Parallel, CMS, G1, ZGC – each balancing throughput, pause time, and memory usage.

Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaThreadPoolHTTPHashMapGarbageCollection
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.