Open In App

Java Tricky Output Questions

Last Updated : 19 Dec, 2025
Comments
Improve
Suggest changes
130 Likes
Like
Report

Java tricky output questions focus on common pitfalls related to control flow, data types, autoboxing, operators, and method overloading. Each question follows a consistent structure for better clarity and quick understanding.

Question 1. Break Statement Outside Loop

Java
public class A {
    public static void main(String[] args)
    {
        if (true)
            break;
    }
}

Choices:

  • a) Nothing
  • b) Error

Answer: b) Error

Explanation:

  • The break statement is valid only inside a loop or a switch block.
  • Using break inside an if statement causes a compile-time error:"break outside switch or loop".

Question 2. Character Addition vs String Concatenation

Java
public class A {
    public static void main(String[] args)
    {
        System.out.println('j' + 'a' + 'v' + 'a');
    }
}

Choices:

  • a) java
  • b) Something else (Other than simple concatenation)

Answer: b) Something else (Other than simple concatenation)

Explanation:

  • Character literals ('j', 'a', 'v', 'a') are treated as char, not String.
  • The + operator performs numeric addition on character Unicode values.
  • Calculation:106 + 97 + 118 + 97 = 418
  • String concatenation would occur only with double-quoted literals.

Question 3. Valid Java Identifiers

Java
public class A {
    public static void main(String[] args)
    {
        int $_ = 5;
    }
}

Choices:

  • a) Nothing
  • b) Error

Answer: a) Nothing

Explanation:

  • In Java, identifiers can start with: Letters, Underscore (_), Dollar sign ($)
  • The variable name $_ is valid, so the program compiles and runs without output.  

Question 4. Integer Caching and Reference Comparison

Java
public class Demo{
    public static void main(String[] arr){
        Integer num1 = 100;
        Integer num2 = 100;
        Integer num3 = 500;
        Integer num4 = 500;
        
        if(num1==num2){
            System.out.println("num1 == num2");
        }
        else{
            System.out.println("num1 != num2");
        }
        if(num3 == num4){
            System.out.println("num3 == num4");
        }
        else{
            System.out.println("num3 != num4");
        }
    }
}

Choices:

  • a) num1 == num2     num3 == num4
  • b) num1 == num2     num3 != num4
  • c) num1 != num2     num3 == num4
  • d) num1 != num2     num3 != num4

Answer: b) num1 == num2               num3 != num4

Explanation:

  • Java caches Integer objects in the range -128 to 127.
  • Autoboxed values within this range reference the same object.
  • 100 is cached → same reference → == returns true.
  • 500 is not cached → different objects → == returns false.
  • == compares object references, not values.

Question 5. Overloading the main() Method

Java
public class Demo {
    public static void main(String[] arr) {}
    public static void main(String arr) {}
}

Choices:

  • a) Nothing
  • b) Error

Answer: a) Nothing

Explanation:

  • The main() method can be overloaded.
  • The JVM only invokes main(String[] args) as the entry point.
  • The overloaded main(String) method is ignored unless explicitly called.

More Java Output Questions.


Article Tags :

Explore