Key Java Interview Questions and Answers (2017 Edition)
This article compiles essential Java interview questions covering topics such as switch statements, equality operators, object methods, reference types, hashCode, string handling, overriding, abstract classes, XML parsing, thread control, memory layout, polymorphism, garbage collection, and collections, providing concise explanations to help job seekers prepare effectively.
Interviews are an inevitable part of every developer's career, often occurring multiple times; this article gathers the latest 2017 Java interview questions to help job seekers study efficiently.
1. Can a switch statement use a String as its parameter? Before Java 7, switch could only handle byte, short, char, int, their wrapper classes, and enum types. Java 7 introduced support for String.
2. Difference between == and equals: == checks whether two variables reference the exact same memory location, while equals() compares the values held by the objects.
3. Common methods of java.lang.Object:
a. equals() tests object equality.
b. clone() creates a copy of the object.
c. getClass() returns the runtime Class object of the instance.
d. notify(), notifyAll(), and wait() are used for thread synchronization on a given object.
4. The four kinds of references in Java and their use cases:
a. Soft references can be used to build memory‑sensitive caches (e.g., storing images in a HashMap with soft references so the JVM reclaims them when memory is low).
b. Weak references are useful for caches where entries should disappear as soon as they are no longer strongly reachable.
c. Strong references prevent the object from being reclaimed; the object is only collected when the reference is set to null.
d. Phantom references act as placeholders; an object reachable only through a phantom reference is considered reclaimed and can be used to perform cleanup actions.
5. Purpose of hashCode() and its difference from equals() : hashCode() provides a numeric hash used by hash‑based collections (e.g., HashMap) to locate buckets quickly, reducing the number of equals() calls needed for equality checks.
6. Differences among String, StringBuffer, and StringBuilder:
a. String is immutable.
b. Both StringBuffer and StringBuilder are backed by a char[] array.
c. StringBuffer is thread‑safe (synchronized); StringBuilder is not.
7. Override vs. Overload:
a. Overload means defining multiple methods with the same name but different parameter lists (type, number, or order).
b. Override means a subclass provides its own implementation of a method declared in a superclass with the same signature.
8. Abstract class vs. interface:
a. A class can extend only one abstract class but can implement multiple interfaces.
b. Interfaces focus on defining capabilities; abstract classes define a common inheritance hierarchy.
c. Abstract classes may contain concrete methods; interfaces (prior to Java 8) required all methods to be abstract, though default methods are now allowed.
9. XML parsing approaches:
a. DOM : Loads the entire XML document into memory and builds a tree structure; easy to use but memory‑intensive.
b. SAX : Event‑driven, reads the document sequentially, triggers callbacks for start/end of elements; low memory usage and high speed.
c. Pull (XmlPullParser) : Similar to SAX but the application pulls parsing events by calling next() and can query attributes via getAttribute() or text via nextText().
10. Difference between wait() and sleep() :
a. sleep() is a method of Thread; wait() belongs to Object.
b. Calling sleep() does **not** release the object's monitor; wait() releases the monitor and allows other threads to acquire it.
c. sleep() simply pauses the thread for a specified time; wait() pauses the thread until another thread invokes notify() or notifyAll().
11. Heap vs. stack and Java memory model:
a. Primitive values and references to objects are stored on the stack.
b. The heap holds objects and arrays created with new.
c. Static (class) variables are allocated in the heap at class‑loading time, with their addresses stored on the stack.
d. Instance variables reside in the heap; they become eligible for garbage collection when no strong references exist.
e. Local variables are allocated on the stack and are reclaimed automatically when they go out of scope.
12. How Java implements polymorphism: Polymorphism is achieved through dynamic (late) binding; the JVM determines the actual method to invoke at runtime based on the object's actual type.
13. Java garbage‑collection algorithms:
a. Mark‑Sweep: Traverses object graph, marks reachable objects, then sweeps away unmarked ones.
b. Mark‑Compact: Same as mark‑sweep but compacts surviving objects to eliminate fragmentation.
c. Copying: Divides the heap into two halves; live objects are copied to the other half, and the original half is cleared.
d. Generational: Separates the heap into young and old generations; most objects die young, so different algorithms are applied to each generation.
e. Reference‑Counting: Keeps a count of references; when the count reaches zero, the object is reclaimed (rarely used alone in modern JVMs).
f. Object‑graph traversal (the default in most JVMs): Starts from GC roots and recursively marks reachable objects.
g. The GC ultimately frees memory occupied by objects that are no longer reachable.
14. Overview of Java collection types and differences:
a. Hashtable is an older class based on Dictionary; HashMap implements the Map interface.
b. Hashtable is synchronized (thread‑safe); HashMap is not.
c. HashMap permits null keys and values; Hashtable does not.
d. ArrayList and Vector use a backing array; Vector is synchronized and therefore slower. LinkedList uses a doubly‑linked list, offering faster insertions/removals but slower random access.
e. When inserting into a HashMap, the key’s hashCode() determines the bucket; collisions are resolved by chaining (linked list) or tree structures in newer JDKs.
f. Fail‑Fast: If a collection is modified while iterating, a ConcurrentModificationException is thrown. This is implemented via a modCount field that the iterator checks against an expectedModCount.
g. Additional differences between HashMap and Hashtable include performance, legacy status, and default load factor.
Recent requests for large‑scale Java project tutorials are listed below (promotional content):
• Java e‑commerce shopping‑cart core payment system
• Java AI face‑recognition scanning system
• Java Baidu Cloud big‑data management system
• Java NetEase 163 email mass‑sending system
• Java AI instant‑messenger customer‑service system
… and more. Interested readers can join the “Java Group Leader” QQ group (number 495273252) for direct access to these tutorials.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
