Fundamentals 4 min read

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.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
5 Tricky Java Questions That Reveal Hidden Pitfalls

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.

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.

concurrencyBigDecimalinterview-questionsfloating-pointswitch
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

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.