10 Tricky Java Interview Questions and Their Answers Explained

This article presents ten challenging Java interview questions covering method overloading, object comparison, garbage collection, parameter passing, string creation, floating‑point precision, static method behavior, double comparison, try‑catch‑finally execution, and main method overloading, each followed by a concise explanation of the correct answer.

Programmer DD
Programmer DD
Programmer DD
10 Tricky Java Interview Questions and Their Answers Explained

Q1: What is the output of the following Java code?

public class Test {
  public static void main(String[] args) {
    method(null);
  }

  public static void method(Object o) {
    System.out.println("Object method");
  }

  public static void method(String s) {
    System.out.println("String method");
  }
}

Answer: It prints "String method". The compiler selects the most specific overload, and method(String s) is more specific than method(Object o).

Q2: What is the output of the following Java code?

public class Test {
  public static void main(String[] args) {
    Integer num1 = 100;
    Integer num2 = 100;
    if (num1 == num2) {
      System.out.println("num1 == num2");
    } else {
      System.out.println("num1 != num2");
    }
  }
}

Answer: It prints "num1 == num2". Because Integer values between -128 and 127 are cached, the two references point to the same object, making the == comparison true.

Q3: How does garbage collection prevent a Java application from running out of memory?

Answer: The Java garbage collector does not guarantee that an application will never run out of memory; it only reclaims memory occupied by objects that are no longer reachable.

Q4: Is Java "pass‑by‑reference" or "pass‑by‑value"?

Answer: Java is always pass‑by‑value. When a reference type is passed, the value being passed is the reference itself, not the actual object.

Q5: How many String objects are created by the following code?

public class Test {
  public static void main(String[] args) {
    String s = new String("Hello World");
  }
}

Answer: Two String objects are created: one in the heap (the explicit new String) and one in the string‑constant pool for the literal "Hello World".

Q6: What is the output of the following code?

public class Test {
  public static void main(String[] arr) {
    System.out.println(0.1 * 3 == 0.3);
    System.out.println(0.1 * 2 == 0.2);
  }
}

Answer: The first print outputs false, the second outputs true. The discrepancy is due to binary floating‑point rounding errors.

Q7: Can a static method be overridden or overloaded in Java?

Answer: Static methods can be overloaded but not overridden. Declaring a static method with the same signature in a subclass hides the superclass method rather than overriding it.

Q8: What is the most reliable way to test equality of two double values?

Answer: Use Double.compare(d1, d2) == 0.

Q9: If a try or catch block executes a return statement, does the finally block still run?

Answer: Yes, the finally block always runs unless the JVM exits via System.exit().

Q10: What is the output of the following code?

public class Test {
  public static void main(String[] args) {
    System.out.println("main method");
  }

  public static void main(String args) {
    System.out.println("Overloaded main method");
  }
}

Answer: It prints "main method". The overloaded main method is not invoked automatically; it would need to be called explicitly from the standard main.

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.

JavaJVMprogramminginterviewOOPquestionsanswers
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.