Java Output Prediction Questions 2026: 30 Tricky Snippets

What changed in 2026 drives
Mass-recruiter offer letters are flatter for 2026 batch - the 4-5 LPA ASE band has barely budged in three years while inflation eats real wages. Premium tracks (Digital, Pro, Elite, Specialist) are still where the differential lives, and they are entirely test-driven. If you are aiming higher than the default offer, the coding round is not optional pageantry - it is the entire interview.
What I'd actually study for this
- 01Two solid coding-round answers (1 medium-hard DSA each, with edge-case discussion) > five half-baked ones
- 02One real project you can defend end-to-end - file paths, design decisions, and what you would change
- 03One DBMS schema you actually built (not a textbook ER diagram), with at least 3 join-heavy queries written from memory
- 04Three behavioural STAR stories: failure recovered, conflict handled, ownership taken
Where most candidates trip up
The single biggest mistake is treating company-specific guides as primary prep and DSA as secondary. It is the opposite. Mass recruiters use the test as a filter, but premium tracks at every IT services company use coding to allocate offer band. Spend 70% of prep time on DSA + system fundamentals, 20% on company-specific patterns, 10% on HR rehearsal. Reverse that ratio and you collect the default offer.
Editorial commentary by Aditya Sharma · written for PapersAdda · not generated, not aggregated.
Last Updated: June 2026 | Level: Freshers to Mid-Level | Read Time: ~16 min
Predict-the-output questions are how interviewers test whether you truly understand Java semantics rather than memorising definitions. Candidates report these dominate aptitude-style coding rounds and MCQ screens. This guide gives 30 carefully chosen snippets with the exact output and a precise explanation of why. Every behaviour shown is verified against the official Oracle Java specification; confirm any edge case on the JLS, since output traps often hinge on a single spec rule.
Pair this with Java Interview Questions 2026 and Java String Interview Questions 2026.
Table of Contents
- Operators and Precedence (Q1 to Q8)
- Autoboxing and Caching (Q9 to Q14)
- Static and Inheritance (Q15 to Q22)
- Strings and Misc (Q23 to Q30)
- Quick Trap Table
- Frequently Asked Questions
Operators and Precedence
Q1.
int x = 5;
System.out.println(x++ + ++x);
Output: 12
Why: x++ yields 5 (then x becomes 6), ++x makes x 7 and yields 7. 5 + 7 = 12.
Q2.
System.out.println(10 + 20 + "Java" + 10 + 20);
Output: 30Java1020
Why: Left to right: 10 + 20 = 30, then string concatenation begins, so the trailing numbers append as text.
Q3.
int a = 1;
System.out.println(a == 1 ? "one" : a == 2 ? "two" : "other");
Output: one
Why: The ternary is right-associative but evaluates the first condition first; a == 1 is true, so "one".
Q4.
System.out.println(5 / 2);
System.out.println(5.0 / 2);
System.out.println(5 % 2);
Output:
2
2.5
1
Why: Integer division truncates to 2. Mixing with a double promotes to 2.5. Modulus gives the remainder 1.
Q5.
boolean b = false;
if (b = true) System.out.println("yes"); else System.out.println("no");
Output: yes
Why: b = true is assignment, not comparison; it assigns true and the expression evaluates to true. A frequent typo-based trap.
Q6.
int i = 10;
System.out.println(i > 5 & i++ > 8);
System.out.println(i);
Output:
true
11
Why: & is non-short-circuit, so both sides evaluate; i++ runs and i becomes 11.
Q7.
System.out.println(1 << 3);
System.out.println(-8 >> 1);
System.out.println(-8 >>> 28);
Output:
8
-4
15
Why: 1 << 3 = 8. Signed right shift preserves the sign giving -4. Unsigned right shift fills with zeros, exposing the high bits as 15.
Q8.
char c = 'A';
System.out.println(c + 1);
System.out.println((char)(c + 1));
Output:
66
B
Why: c + 1 promotes char to int (65 + 1). Casting back to char yields 'B'.
Autoboxing and Caching
Q9.
Integer a = 100, b = 100;
Integer c = 200, d = 200;
System.out.println(a == b);
System.out.println(c == d);
Output:
true
false
Why: Integer caches -128 to 127, so 100 reuses one object (==true). 200 is outside the cache, so two distinct objects (==false). The single most asked output trap.
Q10.
Integer x = null;
int y = x;
Output: NullPointerException
Why: Unboxing a null Integer to a primitive int calls x.intValue() on null, throwing NPE at runtime.
Q11.
Long l = 1L;
Integer i = 1;
System.out.println(l.equals(i));
Output: false
Why: equals checks type first; a Long is never equal to an Integer even with the same numeric value.
Q12.
int i = 1000;
Integer boxed = i;
System.out.println(boxed.equals(1000));
Output: true
Why: equals compares value, not identity, so it ignores the cache and returns true.
Q13.
double d = 0.1 + 0.2;
System.out.println(d == 0.3);
System.out.println(d);
Output:
false
0.30000000000000004
Why: Binary floating point cannot represent 0.1 and 0.2 exactly, so the sum has a tiny error. Never compare doubles with ==; use a tolerance.
Q14.
System.out.println(0.1 + 0.2 == 0.3);
System.out.println(Math.abs(0.1 + 0.2 - 0.3) < 1e-9);
Output:
false
true
Why: Direct equality fails due to representation error; an epsilon comparison is the correct pattern.
Static and Inheritance
Q15.
class A {
A() { print(); }
void print() { System.out.println("A"); }
}
class B extends A {
int x = 5;
void print() { System.out.println("x=" + x); }
}
new B();
Output: x=0
Why: The base constructor calls the overridden print before B's field initialiser runs, so x is still 0.
Q16.
class P { static void who() { System.out.println("P"); } }
class C extends P { static void who() { System.out.println("C"); } }
P ref = new C();
ref.who();
Output: P
Why: Static methods are not polymorphic; resolution uses the reference's static type.
Q17.
class A { int x = 1; }
class B extends A { int x = 2; }
A a = new B();
System.out.println(a.x);
Output: 1
Why: Fields are not polymorphic; field access uses the static type of the reference, so A's x (1) is read.
Q18.
static int count;
static { count = 5; }
public static void main(String[] a) { System.out.println(count); }
Output: 5
Why: The static initialiser block runs at class load, before main, setting count to 5.
Q19.
class A {
static int x = 10;
static { x = 20; }
}
System.out.println(A.x);
Output: 20
Why: Static field initialisers and static blocks run top to bottom; the block runs after the field assignment, leaving 20.
Q20.
int test() {
try { return 1; }
finally { return 2; }
}
Output: 2
Why: A return in finally overrides the try's return value.
Q21.
class Base { Base() { System.out.print("B"); } }
class Derived extends Base { Derived() { System.out.print("D"); } }
new Derived();
Output: BD
Why: The implicit super() runs the Base constructor first, then the Derived body.
Q22.
final int[] arr = {1, 2, 3};
arr[0] = 99;
System.out.println(arr[0]);
Output: 99
Why: final makes the reference unchangeable, not the contents. You can still mutate elements.
Strings and Misc
Q23.
String a = "x";
String b = "x";
System.out.println(a == b);
Output: true
Why: Both literals share the same pooled reference.
Q24.
String s = "abc";
s.toUpperCase();
System.out.println(s);
Output: abc
Why: Strings are immutable; the returned uppercase string is discarded.
Q25.
System.out.println("a,b,,".split(",").length);
Output: 2
Why: Trailing empty strings are dropped by default split, leaving "a" and "b".
Q26.
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
list.remove(1);
System.out.println(list);
Output: [1, 3]
Why: remove(int) removes by index, so index 1 (value 2) is removed, not the value 1.
Q27.
System.out.println(Integer.parseInt("10", 2));
Output: 2
Why: The second argument is the radix; "10" in base 2 equals decimal 2.
Q28.
int x = 5;
switch (x) {
case 5: System.out.print("five ");
case 6: System.out.print("six ");
default: System.out.print("def");
}
Output: five six def
Why: No break, so execution falls through every subsequent case.
Q29.
System.out.println((Object) "1" == (Object) "1");
System.out.println(Integer.valueOf(127) == Integer.valueOf(127));
Output:
true
true
Why: Both string literals are pooled, and 127 is within the Integer cache, so both comparisons share references.
Q30.
int i = 0;
i = i++;
System.out.println(i);
Output: 0
Why: i++ returns the old value 0, which is then assigned back to i, overwriting the increment. Classic self-assignment trap.
Quick Trap Table
| Trap | Rule to remember |
|---|---|
| Integer cache | -128 to 127 cached, == true; outside, false |
| Unbox null | NPE on Integer to int when null |
| Fields | not polymorphic, use static type |
| Static methods | hidden, not overridden |
| finally return | overrides try return |
| split trailing | empty strings dropped |
| list.remove(int) | removes by index, not value |
| i = i++ | increment lost |
| double == | representation error, use epsilon |
Frequently Asked Questions
Are predict-the-output questions common in Java interviews in 2026?
Yes. Candidates report that aptitude-style coding rounds at service companies and MCQ screens at many product companies lean heavily on output prediction for autoboxing, integer caching, and operator precedence.
What is the most common output trap in Java?
The Integer cache trap: values from -128 to 127 are cached so == returns true, but outside that range each box is a new object so == returns false. This single behaviour catches the most candidates.
How do I get fast at predicting output?
Trace evaluation strictly left to right, watch the static versus runtime type for method dispatch, and memorise the special cases: integer cache, string pool, finally over return, and floating point rounding.
Why does i = i++ leave i unchanged?
i++ evaluates to the old value before incrementing, and that old value is then assigned back to i, overwriting the incremented result. The post-increment side effect is lost to the assignment.
Should I write break in every switch case?
In the classic switch statement, yes, unless you intend fall-through. The newer arrow-form switch expression does not fall through, which removes this entire class of bug.
Related Articles
- Java Interview Questions 2026, the master 50-question set
- Java String Interview Questions 2026, string-pool traps in depth
- Java OOPs Interview Questions 2026, inheritance and dispatch
- Java Collections Interview Questions 2026, list and map behaviour
Every output above is verified against the official Oracle Java specification. Confirm any edge case on the JLS. This guide reflects candidate-reported patterns and public preparation resources as of June 2026.
Methodology applied to this articlelast verified 8 Jun 2026
- No fabricated salary numbers or success rates. If we quote a range, it's sourced.
- No noun-substituted templates. This article was not generated by swapping company names in a stock prompt.
- No paid placements, sponsored coaching links, or affiliate-shilled course pushes.
Explore this topic cluster
More resources in Interview Questions
Use the category hub to browse similar questions, exam patterns, salary guides, and preparation resources related to this topic.
Paid contributor programme
Sat this this year? Share your story, earn ₹500.
First-person experience reports help future candidates prep smarter. We pay verified contributors ₹500 via UPI per accepted story - with byline.
Submit your story →Ready to practice?
Take a free timed mock test
Put what you learned into practice. Our mock tests match the 2026 pattern with timer, navigator, reveal, and score breakdown. No signup.
Start Free Mock Test →Related Articles
Airbnb Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing Airbnb's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical, behavioural,...
Airtel Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing Airtel's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical, behavioural,...
AMD Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing AMD's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical, behavioural,...
Atlassian Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing Atlassian's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical,...
Barclays Interview Questions 2026
_Last verified by [Aditya Sharma](/author/aditya-sharma/) · cross-checked against PapersAdda Hiring Pulse and...
More from PapersAdda
Accenture Interview Process 2026: Rounds & Prep
Accenture Interview Questions 2026 (with Answers for Freshers)
Adobe Interview Process 2026: Rounds, OA & Aptitude
Amazon Interview Process 2026: Full Loop + Bar Raiser