5 Tricky Java Questions That Reveal Hidden Pitfalls
This article presents five Java code snippets—covering floating‑point comparison, wrapper equality, switch handling of null, BigDecimal construction, and lock usage—and challenges readers to identify the correct outcomes, exposing common misconceptions and best‑practice solutions.
In the Java community, many seemingly simple code snippets hide subtle traps that can surprise even experienced developers. Below are five classic questions, each with a code example and multiple‑choice answers, designed to test and deepen your understanding of Java fundamentals.
1. Primitive Float Comparison
public class FloatPrimitiveTest {
public static void main(String[] args) {
float a = 1.0f - 0.9f;
float b = 0.9f - 0.8f;
if (a == b) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}Which output is correct?
A: true
B: false
C: Determined by hardware instructions
2. Wrapper Float Equality
public class FloatWrapperTest {
public static void main(String[] args) {
Float a = Float.valueOf(1.0f - 0.9f);
Float b = Float.valueOf(0.9f - 0.8f);
if (a.equals(b)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}Which output is correct?
A: true
B: false
C: Compilation error
3. Switch on a Null String
public class SwitchTest {
public static void main(String[] args) {
String param = null;
switch (param) {
case "null":
System.out.println("null");
break;
default:
System.out.println("default");
}
}
}Which statement is correct?
A: null
B: Throws an exception
C: default
4. BigDecimal Construction
public class BigDecimalTest {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(0.1);
System.out.println(a);
BigDecimal b = new BigDecimal("0.1");
System.out.println(b);
}
}Which statement is correct?
A: Both assignment methods are identical
B: The first method (using double) is recommended
C: The second method (using String) is recommended
5. ReentrantLock Usage
public class LockTest {
private final static Lock lock = new ReentrantLock();
public static void main(String[] args) {
try {
lock.tryLock();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}Which statement is false?
A: The lock is a non‑fair lock
B: The finally block will never throw an exception
C: If tryLock fails, execution continues without acquiring the lock
These questions are derived from real‑world discussions among developers and aim to highlight subtle language behaviors that can lead to bugs if misunderstood.
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.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
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.
