ava Output Prediction Questions for Freshers (2026)

“What will be the output of this code?” is one of the most common ways interviewers test whether you actually understand Java or only memorised definitions. These questions appear in written tests, online coding rounds and technical interviews, and they catch a lot of freshers because the code looks simple but hides one small rule.

If you’ve been searching for Java output questions for freshers, tricky Java interview questions, Java predict the output with answers, Java code snippets interview questions or Java output based questions for placements, this is a practice set. You’ll get 20 snippets, each with a “think first” pause, the answer and the reason, followed by the rules behind them and a way to practise.

HOW TO USE THIS SET

  1. Cover the answer. Read the code and write your prediction on paper.

  2. Check the answer and the reason. The reason matters more than the output.

  3. Mark each one: ✔ got it, ✖ missed it.

  4. Revisit the ✖ ones after two days and predict again without looking.

Scorecard (Fill It In)




What your score suggests (a rough guide, not an official grading)

  • 0 to 8: revise basics before applying

  • 9 to 14: decent base, practise the missed topics

  • 15 to 20: strong, keep explaining the “why” aloud

Where These Questions Usually Come From: An Illustrative View

This is an illustrative guide, not a survey.




SECTION A: STRINGS AND INTEGERS (Q1 to Q5)

Q1. String comparison

java

String a = "java";
String b = "java";
String c = new String("java");

System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
System.out.println(a == c.intern());
String a = "java";
String b = "java";
String c = new String("java");

System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
System.out.println(a == c.intern());
String a = "java";
String b = "java";
String c = new String("java");

System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
System.out.println(a == c.intern());

Think first.

Output




Why: String literals go into the String Pool, so a and b point to the same object. new String("java") creates a separate object on the heap, so == (reference comparison) is false. equals() compares content, so it’s true. intern() returns the pooled version, so a == c.intern() is true.

Remember: use equals() for content, == for references.

Q2. Integer caching

java

Integer x = 127, y = 127;
Integer p = 128, q = 128;

System.out.println(x == y);
System.out.println(p == q);
System.out.println(p.equals(q));
Integer x = 127, y = 127;
Integer p = 128, q = 128;

System.out.println(x == y);
System.out.println(p == q);
System.out.println(p.equals(q));
Integer x = 127, y = 127;
Integer p = 128, q = 128;

System.out.println(x == y);
System.out.println(p == q);
System.out.println(p.equals(q));

Think first.

Output




Why: By default, Java caches Integer objects from -128 to 127. Values in that range share the same object, so == is true. 128 is outside the cache, so autoboxing creates two different objects and == is false. equals() compares values, so it’s true.

Q3. String immutability

java

String s = "hello";
s.concat(" world");
System.out.println(s);
String s = "hello";
s.concat(" world");
System.out.println(s);
String s = "hello";
s.concat(" world");
System.out.println(s);

Think first.

Output

Why: Strings are immutable. concat() returns a new string, but you didn’t store it. To keep the result: s = s.concat(" world");.

Q4. Concatenation order

java

System.out.println("a" + 1 + 2);
System.out.println(1 + 2 + "a");
System.out.println('a' + 1);
System.out.println((char) ('a' + 1));
System.out.println("a" + 1 + 2);
System.out.println(1 + 2 + "a");
System.out.println('a' + 1);
System.out.println((char) ('a' + 1));
System.out.println("a" + 1 + 2);
System.out.println(1 + 2 + "a");
System.out.println('a' + 1);
System.out.println((char) ('a' + 1));

Think first.

Output




Why: Expressions evaluate left to right. "a" + 1 becomes "a1", then + 2 gives "a12". In 1 + 2 + "a", 1 + 2 is added first (3), then joined with “a”. 'a' + 1 is integer arithmetic (97 + 1 = 98). Casting back to char gives 'b'.

Q5. Integer division and overflow

java

System.out.println(10 / 3);
System.out.println(10 % 3);
System.out.println(10 / 3.0);
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(10 / 3);
System.out.println(10 % 3);
System.out.println(10 / 3.0);
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(10 / 3);
System.out.println(10 % 3);
System.out.println(10 / 3.0);
System.out.println(Integer.MAX_VALUE + 1);

Think first.

Output




Why: Integer division drops the decimal part. % gives the remainder. If either operand is a double, the result is a double. Integer.MAX_VALUE + 1 overflows and wraps around to the minimum int value.

SECTION B: OPERATORS AND FLOW (Q6 to Q10)

Q6. Pre and post increment

java

int i = 5;
int j = i++ + ++i;
System.out.println(i + " " + j);
int i = 5;
int j = i++ + ++i;
System.out.println(i + " " + j);
int i = 5;
int j = i++ + ++i;
System.out.println(i + " " + j);

Think first.

Output

Why: i++ uses 5, then i becomes 6. ++i makes i 7 and uses 7. So j = 5 + 7 = 12, and i is 7.

Q7. Short-circuit vs non-short-circuit

java

int a = 5;
boolean r1 = (a > 3) || (a++ > 5);
System.out.println(a);

boolean r2 = (a > 3) | (a++ > 5);
System.out.println(a);
int a = 5;
boolean r1 = (a > 3) || (a++ > 5);
System.out.println(a);

boolean r2 = (a > 3) | (a++ > 5);
System.out.println(a);
int a = 5;
boolean r1 = (a > 3) || (a++ > 5);
System.out.println(a);

boolean r2 = (a > 3) | (a++ > 5);
System.out.println(a);

Think first.

Output




Why: || short-circuits. Since a > 3 is true, the right side never runs, so a stays 5. | always evaluates both sides, so a++ runs and a becomes 6.

Q8. Switch fall-through

java

int x = 2;
switch (x) {
    case 1: System.out.println("one");
    case 2: System.out.println("two");
    case 3: System.out.println("three"); break;
    default: System.out.println("other");
}
int x = 2;
switch (x) {
    case 1: System.out.println("one");
    case 2: System.out.println("two");
    case 3: System.out.println("three"); break;
    default: System.out.println("other");
}
int x = 2;
switch (x) {
    case 1: System.out.println("one");
    case 2: System.out.println("two");
    case 3: System.out.println("three"); break;
    default: System.out.println("other");
}

Think first.

Output




Why: Without break, execution continues into the next case. It starts at case 2, falls into case 3, then hits break.

Q9. Ternary type promotion

java

System.out.println(true ? 1 : 2.0);
System.out.println(true ? 1 : 2.0);
System.out.println(true ? 1 : 2.0);

Think first.

Output

Why: In a ternary expression with an int and a double, both operands are promoted to double, so 1 becomes 1.0.

Q10. Floating-point precision

java

System.out.println(0.1 + 0.2);
System.out.println(0.1 + 0.2 == 0.3);
System.out.println(0.1 + 0.2);
System.out.println(0.1 + 0.2 == 0.3);
System.out.println(0.1 + 0.2);
System.out.println(0.1 + 0.2 == 0.3);

Think first.

Output




Why: Decimal fractions like 0.1 can’t be represented exactly in binary floating point. Never compare doubles with ==. Use a tolerance, or BigDecimal for money.

SECTION C: OOP AND INITIALISATION (Q11 to Q15)

Q11. Initialisation order

java

class A {
    static { System.out.println("static A"); }
    { System.out.println("instance A"); }
    A() { System.out.println("constructor A"); }
}

public class Main {
    public static void main(String[] args) {
        new A();
        new A();
    }
}
class A {
    static { System.out.println("static A"); }
    { System.out.println("instance A"); }
    A() { System.out.println("constructor A"); }
}

public class Main {
    public static void main(String[] args) {
        new A();
        new A();
    }
}
class A {
    static { System.out.println("static A"); }
    { System.out.println("instance A"); }
    A() { System.out.println("constructor A"); }
}

public class Main {
    public static void main(String[] args) {
        new A();
        new A();
    }
}

Think first.

Output




Why: The static block runs once when the class is loaded. For each object, the instance initialiser block runs before the constructor body.

Q12. Parent and child constructors

java

class Parent {
    Parent() { System.out.println("Parent"); }
}
class Child extends Parent {
    Child() { System.out.println("Child"); }
}
public class Main {
    public static void main(String[] args) {
        new Child();
    }
}
class Parent {
    Parent() { System.out.println("Parent"); }
}
class Child extends Parent {
    Child() { System.out.println("Child"); }
}
public class Main {
    public static void main(String[] args) {
        new Child();
    }
}
class Parent {
    Parent() { System.out.println("Parent"); }
}
class Child extends Parent {
    Child() { System.out.println("Child"); }
}
public class Main {
    public static void main(String[] args) {
        new Child();
    }
}

Think first.

Output




Why: The child constructor implicitly calls super() first, so the parent constructor runs before the child’s body.

Q13. Overriding vs variable hiding

java

class P {
    String name = "Parent";
    String get() { return "P"; }
}
class C extends P {
    String name = "Child";
    String get() { return "C"; }
}
public class Main {
    public static void main(String[] args) {
        P obj = new C();
        System.out.println(obj.name);
        System.out.println(obj.get());
    }
}
class P {
    String name = "Parent";
    String get() { return "P"; }
}
class C extends P {
    String name = "Child";
    String get() { return "C"; }
}
public class Main {
    public static void main(String[] args) {
        P obj = new C();
        System.out.println(obj.name);
        System.out.println(obj.get());
    }
}
class P {
    String name = "Parent";
    String get() { return "P"; }
}
class C extends P {
    String name = "Child";
    String get() { return "C"; }
}
public class Main {
    public static void main(String[] args) {
        P obj = new C();
        System.out.println(obj.name);
        System.out.println(obj.get());
    }
}

Think first.

Output




Why: Methods are resolved at runtime based on the actual object (polymorphism), so get() returns "C". Fields are resolved at compile time based on the reference type, so obj.name is "Parent".

Q14. Overload resolution with null

java

static void print(Object o) { System.out.println("Object"); }
static void print(String s) { System.out.println("String"); }

public static void main(String[] args) {
    print(null);
}
static void print(Object o) { System.out.println("Object"); }
static void print(String s) { System.out.println("String"); }

public static void main(String[] args) {
    print(null);
}
static void print(Object o) { System.out.println("Object"); }
static void print(String s) { System.out.println("String"); }

public static void main(String[] args) {
    print(null);
}

Think first.

Output

Why: Java picks the most specific matching method. String is more specific than Object. (If you add a third overload like print(Integer i), the call becomes ambiguous and won’t compile.)

Q15. Pass by value

java

static void change(int x) { x = 100; }
static void change(StringBuilder sb) { sb.append(" world"); }

public static void main(String[] args) {
    int n = 10;
    StringBuilder sb = new StringBuilder("hello");
    change(n);
    change(sb);
    System.out.println(n);
    System.out.println(sb);
}
static void change(int x) { x = 100; }
static void change(StringBuilder sb) { sb.append(" world"); }

public static void main(String[] args) {
    int n = 10;
    StringBuilder sb = new StringBuilder("hello");
    change(n);
    change(sb);
    System.out.println(n);
    System.out.println(sb);
}
static void change(int x) { x = 100; }
static void change(StringBuilder sb) { sb.append(" world"); }

public static void main(String[] args) {
    int n = 10;
    StringBuilder sb = new StringBuilder("hello");
    change(n);
    change(sb);
    System.out.println(n);
    System.out.println(sb);
}

Think first.

Output




Why: Java is always pass by value. For primitives, the copy changes and the original doesn’t. For objects, the value passed is a copy of the reference, so modifying the object through it is visible outside, but reassigning the reference isn’t.

SECTION D: COLLECTIONS, EXCEPTIONS AND MISC (Q16 to Q20)

Q16. HashSet without equals and hashCode

java

class Person {
    String name;
    Person(String name) { this.name = name; }
}

Set<Person> set = new HashSet<>();
set.add(new Person("Amit"));
set.add(new Person("Amit"));
System.out.println(set.size());
class Person {
    String name;
    Person(String name) { this.name = name; }
}

Set<Person> set = new HashSet<>();
set.add(new Person("Amit"));
set.add(new Person("Amit"));
System.out.println(set.size());
class Person {
    String name;
    Person(String name) { this.name = name; }
}

Set<Person> set = new HashSet<>();
set.add(new Person("Amit"));
set.add(new Person("Amit"));
System.out.println(set.size());

Think first.

Output

Why: Without overriding equals() and hashCode(), two objects are considered different, even with identical content. Override both to make the set treat them as duplicates.

Q17. ArrayList remove(int) vs remove(Object)


List<Integer> list = new ArrayList<>(List.of(10, 20, 30));
list.remove(1);
System.out.println(list);
list.remove(Integer.valueOf(10));
System.out.println(list);
List<Integer> list = new ArrayList<>(List.of(10, 20, 30));
list.remove(1);
System.out.println(list);
list.remove(Integer.valueOf(10));
System.out.println(list);
List<Integer> list = new ArrayList<>(List.of(10, 20, 30));
list.remove(1);
System.out.println(list);
list.remove(Integer.valueOf(10));
System.out.println(list);

Think first.

Output

[10, 30]
[30]
[10, 30]
[30]
[10, 30]
[30]

Why: remove(1) removes the element at index 1 (the value 20). remove(Integer.valueOf(10)) removes the object 10.

Q18. finally with return

java

static int test() {
    try {
        return 1;
    } finally {
        System.out.println("finally");
    }
}

public static void main(String[] args) {
    System.out.println(test());
}
static int test() {
    try {
        return 1;
    } finally {
        System.out.println("finally");
    }
}

public static void main(String[] args) {
    System.out.println(test());
}
static int test() {
    try {
        return 1;
    } finally {
        System.out.println("finally");
    }
}

public static void main(String[] args) {
    System.out.println(test());
}

Think first.

Output




Why: The finally block runs before the method actually returns. The value 1 is prepared, finally prints, then 1 is returned. (A return inside finally would override the earlier return, which is considered bad practice.)

Q19. try, catch, finally flow

java

try {
    int a = 10 / 0;
    System.out.println("after");
} catch (ArithmeticException e) {
    System.out.println("catch");
} finally {
    System.out.println("finally");
}
try {
    int a = 10 / 0;
    System.out.println("after");
} catch (ArithmeticException e) {
    System.out.println("catch");
} finally {
    System.out.println("finally");
}
try {
    int a = 10 / 0;
    System.out.println("after");
} catch (ArithmeticException e) {
    System.out.println("catch");
} finally {
    System.out.println("finally");
}

Think first.

Output




Why: The exception occurs on the division line, so “after” never prints. The matching catch runs, then finally always runs.

Q20. Default values and final collections

java

int[] nums = new int[3];
String[] names = new String[2];
final List<String> list = new ArrayList<>();
list.add("Java");

System.out.println(nums[0]);
System.out.println(names[0]);
System.out.println(list);
int[] nums = new int[3];
String[] names = new String[2];
final List<String> list = new ArrayList<>();
list.add("Java");

System.out.println(nums[0]);
System.out.println(names[0]);
System.out.println(list);
int[] nums = new int[3];
String[] names = new String[2];
final List<String> list = new ArrayList<>();
list.add("Java");

System.out.println(nums[0]);
System.out.println(names[0]);
System.out.println(list);

Think first.

Output

0
null
[Java]
0
null
[Java]
0
null
[Java]

Why: Array elements get default values: 0 for int, null for object types. final on a reference means it can’t point to another object, but the object’s contents can still change, so add() works.

THE RULES BEHIND THE ANSWERS (QUICK REVISION TABLE)

Rule

Where it appeared

== compares references, equals() compares content

Q1, Q2

Strings are immutable

Q3

Expressions evaluate left to right

Q4

Integer division truncates, int overflow wraps

Q5

i++ uses then increments, ++i increments then uses

Q6

`


Switch falls through without break

Q8

Ternary promotes to the wider type

Q9

Doubles aren’t exact, avoid ==

Q10

Static block, then instance block, then constructor

Q11

Parent constructor runs before child

Q12

Methods are runtime-bound, fields compile-time

Q13

Most specific overload wins

Q14

Java is always pass by value

Q15

Override equals() and hashCode() together

Q16

remove(int) vs remove(Object)

Q17

finally runs before the method returns

Q18, Q19

Default values and final references

Q20

MISTAKES FRESHERS MAKE ON OUTPUT QUESTIONS

Mistake

Fix

Guessing from “how it looks”

Trace line by line on paper

Ignoring evaluation order

Write each intermediate value

Forgetting autoboxing effects

Check whether == compares objects

Missing a compile error

Check for ambiguity, missing returns, unreachable code

Stating the output without the reason

Always add one sentence of “why”

Rushing in online tests

Spend about 90 seconds, then move on

HOW TO EXPLAIN YOUR ANSWER IN AN INTERVIEW

Don’t just say the output. Use this pattern.




Example (Q2): “It prints true, false, true. Java caches Integer objects between -128 and 127, so 127 shares one object, but 128 creates two. That’s why we should use equals() to compare wrapper objects.”

A 5-DAY PRACTICE PLAN

Day

Focus

Task

1

Strings and integers (Q1 to Q5)

Predict, run in your IDE, compare

2

Operators and flow (Q6 to Q10)

Predict, run, then write two similar snippets yourself

3

OOP and initialisation (Q11 to Q15)

Draw the execution order on paper

4

Collections and exceptions (Q16 to Q20)

Predict, run, then explain aloud

5

Mixed revision

Redo every missed question without looking at the answer

Tip: always run the code in your IDE after predicting. Seeing the real output sticks better than reading about it.

HOW VIBRANTMINDS HELPS YOU MASTER JAVA FUNDAMENTALS

VibrantMinds Technologies Pvt. Ltd. trains freshers in Pune to understand Java, not just recite it.

  • Core Java and Full Stack Java training: The Core Java course (about 1 to 2 months) and the Full Stack Java Development programme (about 4 to 6 months) cover syntax, OOP, collections, exceptions, strings, JDBC, SQL, Spring Boot and REST APIs. Please check the candidate portal for the latest syllabus.

  • Practice and explanation: Working through code, predicting output and explaining the “why” builds the skill output-based questions test.

  • Aptitude, communication and interview preparation are part of the programme, so you can explain your answers clearly.

  • Placement support: The placement pipeline includes screening, drive coordination and interview support. Check the campus-drives portal at https://candidates.vibrantminds.in/ for current openings.

  • Honesty first: VibrantMinds offers placement support, not a guaranteed job, and no legitimate recruiter should ask you to pay to be interviewed.

VibrantMinds works as a talent ecosystem connecting candidates, colleges and companies, with 1900+ partner colleges and 600+ hiring companies. Learn more on the official website: https://vibrantminds.in/

VIBRANTMINDS COURSES AT A GLANCE


Course

Duration

Key topics

Full Stack Java Development

4 to 6 months

Core Java, OOP, JDBC, SQL, Spring Boot, REST APIs, front-end basics, projects, aptitude and interview preparation

Software Testing (Manual + Automation)

2 to 5 months

Test cases, STLC, bug tracking, SQL, Selenium basics

Core Java

1 to 2 months

Syntax, OOP, collections, exceptions, strings

Python & Data Science

2 to 4 months

Python basics, data handling, analysis fundamentals

Fees, syllabus and batch dates are on https://candidates.vibrantminds.in/. Please confirm them directly before enrolling.

THE VIBRANTMINDS PLACEMENT PIPELINE: SIX STEPS









REAL OUTCOMES: WHAT TO EXPECT AND WHAT NOT TO

Past students have shared positive feedback about the training and support, and have gone on to roles at companies such as Aress Software and Education Technologies, Saama Technologies and Three M Software. Results vary by candidate, skill level, effort and market conditions, so please confirm details directly with the VibrantMinds team. No one can honestly promise a guaranteed job.

JOIN THE VIBRANTMINDS WHATSAPP GROUPS FOR FRESHER JOB AND DRIVE ALERTS

Fresher openings, campus drives and walk-in interviews are shared here as they come in. Join whichever group has space; all three carry the same type of updates.

Never pay to apply. Legitimate recruiters do not charge candidates for tests, interviews or offer letters. Do not share sensitive personal or financial details in any group chat.

VISIT OR CONTACT VIBRANTMINDS IN PUNE

FREQUENTLY ASKED QUESTIONS

What are Java output-based interview questions?
They give you a short code snippet and ask what it prints, testing whether you understand Java rules such as String Pool, operator precedence, initialisation order, overriding and exception flow.

Why do interviewers ask output prediction questions?
They show whether you can trace code mentally and understand how Java actually behaves, rather than only memorising definitions.

What is the difference between == and equals() in Java?
== compares references (whether two variables point to the same object) for objects, while equals() compares content when it’s overridden, as in String. For primitives, == compares values.

Why does Integer 127 == 127 return true but 128 == 128 return false?
Java caches Integer objects from -128 to 127, so values in that range share one object, while larger values create separate objects through autoboxing. Use equals() to compare wrapper values.

Does finally always execute in Java?
Almost always, even when a return statement is reached in try or when an exception is caught. Exceptions to this include situations such as the JVM exiting through System.exit() or crashing.

Is Java pass by value or pass by reference?
Java is always pass by value. For objects, the value passed is a copy of the reference, so changes to the object are visible outside, but reassigning the reference isn’t.

How do I prepare for tricky Java interview questions?
Practise predicting output on paper, run the code to confirm, write down the rule behind each answer and explain it aloud.

Are these questions asked in written tests too?
Yes. Many online and written tests include output-based multiple-choice questions, so practise under time limits.

How many output questions should I practise daily?
Five to ten focused questions a day, with a review of the reasons, is usually more effective than rushing through many.

Does VibrantMinds teach Core Java with this kind of practice?
VibrantMinds offers Core Java and Full Stack Java training with hands-on practice and interview preparation, along with placement support. VibrantMinds does not guarantee a job, so please confirm current details directly with the team.

FINAL CHECKLIST

  • ☐ I predicted each answer before reading the solution

  • ☐ I can explain == vs equals() and the Integer cache

  • ☐ I know initialisation order: static, instance block, constructor

  • ☐ I understand short-circuit operators and pre/post increment

  • ☐ I can explain pass by value with an example

  • ☐ I redo missed questions after two days

Output-based questions reward calm, line-by-line thinking more than clever guessing. Practise a few daily, always say the rule aloud, and they’ll stop feeling like tricks. If you’re in Pune and want guided Java training with placement support, visit https://vibrantminds.in/, check the candidate portal at https://candidates.vibrantminds.in/, and join the WhatsApp groups above for the next drive.