Java Interview Questions and Answers: StringBuilder, Null Checks, Integer Caching, Collections, File I/O, Float Precision, Multithreading, Loops, and Finally
This article compiles a series of common Java interview questions covering StringBuilder versus String concatenation, null‑check pitfalls, Integer caching behavior, parameter passing, array‑to‑list conversion limits, directory creation methods, floating‑point precision, multithreading techniques, loop edge cases, and the guaranteed execution of finally blocks.
1. StringBuilder replaces String concatenation, often asked in interviews
Difference between String, StringBuilder, and StringBuffer: String is immutable and concatenation creates many temporary objects, leading to low efficiency; use StringBuilder for mutable strings in single‑threaded contexts and StringBuffer when synchronization is required.
2. Not checking for null before calling equals can cause NPE
Always place the constant or a non‑null reference on the left side of the equals call to avoid a NullPointerException.
3. Integer caching and equality comparison
Integers between -128 and 127 are cached; comparing two Integer objects with == returns true for values in this range but false for larger values because they are distinct objects.
4. Passing a variable as a method argument and modifying it
Reassigning a primitive parameter does not affect the original variable, while modifying an object's fields does; therefore the shown total variable remains 0.
5. Converting an array to a list
The resulting list is a fixed‑size view backed by the original array, so adding or removing elements throws an UnsupportedOperationException.
6. Removing an element from a list during iteration
Removing an element while iterating with an index can cause the loop to exceed the new list size, leading to an IndexOutOfBoundsException.
7. Creating directories with mkdir() vs mkdirs()
mkdir() creates only a single directory level, whereas mkdirs() creates all nonexistent parent directories, enabling multi‑level directory creation.
8. Float precision loss in monetary calculations
Subtracting two float values can lose precision; use BigDecimal or scale the amounts to integers before performing arithmetic.
9. Ways to implement multithreading in Java
Common approaches are extending the Thread class and implementing the Runnable interface; a third option is implementing Callable to obtain a result from the thread.
10. Looping three times and integer overflow
The issue arises from using Integer.MAX_VALUE and adding 1, which causes overflow and unexpected loop behavior.
11. Finally block execution
The finally block runs regardless of a return statement in the try block, ensuring cleanup code (e.g., closing streams) is always executed; thus the example returns false.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
