50 Java Output-Based Interview Questions with Answers
Welcome to one of the most important interview prep posts you'll ever read! 🎯
Output-based questions are the secret weapon of Java interviews. They don't just test whether you memorized syntax — they test whether you truly understand how Java works under the hood.
These 50 questions cover variables, data types, operators, type casting, static blocks, instance initializers, bitwise operations, and more. Every question here has appeared in real interviews at companies like TCS, Infosys, Wipro, Amazon, and product-based startups.
How to use this post: Read each question, guess the output in your head, then click "Show Answer" to check. Don't run the code first — that's cheating! 😄
💡 Important: Pay close attention to the difference between compilation errors, runtime exceptions, and actual outputs. Most interviewers love this trap!
Questions 1–11: Static vs Instance Context
Question 1
What is the output of the below program?
public class First {
int i;
public static void main(String[] args) {
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. You cannot make a static reference to the non-static field i. Since main is static, it can't access instance variables directly.
💡 Interview Tip: Interviewers love this one — it tests if you understand the difference between static and instance context.
Question 2
What is the output?
public class First {
int i;
public static void main(String[] args) {
int i;
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. Variable i might not have been initialized. Local variables must be assigned before use — Java doesn't give default values to local variables.
Question 3
What is the output?
public class First {
int i;
static int i;
public static void main(String[] args) {
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. Duplicate field i. You cannot declare two variables with the same name in the same class — even if one is static and one is instance.
Question 4
What is the output?
public class First {
int i = 100;
static int j = i;
public static void main(String[] args) {
System.out.println(j);
}
}
✅ Show Answer
Compilation Error. Cannot make a static reference to the non-static field i. Static variables cannot use instance variables in their initializer.
Question 5
What is the output?
public class First {
int i = 100;
static int j = 10;
public static void main(String[] args) {
int k = i + j;
System.out.println(k);
}
}
✅ Show Answer
Compilation Error. Cannot make a static reference to non-static field i from main (static context). Only j can be accessed directly here.
Question 6
What is the output?
public class First {
public static void main(String[] args) {
int i;
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. Variable i might not have been initialized.
💡 Key Rule: Class-level variables get default values, but local variables do NOT.
Question 7
What is the output?
public class First {
public static void main(String[] args) {
int i = 10, k;
System.out.println(k);
}
}
✅ Show Answer
Compilation Error. Variable k might not have been initialized. Declaring without assignment means it has no value.
Question 8
What is the output?
public class First {
public static void main(String[] args) {
int i, k = 10;
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. Variable i might not have been initialized. Only k was assigned 10.
Question 9
What is the output?
public class First {
static {
System.out.println(i);
}
static int i = 109;
public static void main(String[] args) {
}
}
✅ Show Answer
Compilation Error. Cannot reference a field before it is defined — this is called an illegal forward reference.
💡 Why: Static blocks run in the order they appear. Since i is declared AFTER the static block, it doesn't exist yet when the block runs.
Question 10
What is the output?
public class First {
int i = 10;
static {
System.out.println(i);
}
public static void main(String[] args) {
}
}
✅ Show Answer
Compilation Error. Cannot make a static reference to the non-static field i. Static blocks cannot access instance variables.
Question 11
What is the output?
public class First {
{
System.out.println(i);
}
public static void main(String[] args) {
}
int i = 10;
}
✅ Show Answer
Compilation Error. Illegal forward reference. Instance initializer block cannot reference i before its declaration.
Questions 12–18: Increment & Type Promotion
Question 12
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
System.out.println(i++);
}
}
✅ Show Answer
Output: 10
Post-increment: the original value (10) is returned first, then i becomes 11. But since we're printing the returned value, it prints 10.
Question 13
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
System.out.println(++i);
}
}
✅ Show Answer
Output: 11
Pre-increment: i becomes 11 first, then the value is returned.
Question 14
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
System.out.println(++i + i++);
}
}
✅ Show Answer
Output: 22
Step by step:
++i→ i becomes 11, returns 11i++→ returns 11, then i becomes 12- 11 + 11 = 22
💡 Interview Tip: This is a favorite! Track each increment carefully.
Question 15
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
System.out.println(++i + "i++");
}
}
✅ Show Answer
Output: 11i++
First ++i makes i = 11, then it's concatenated with the string "i++". Java concatenates when any operand is a String.
Question 16
What is the output?
public class First {
public static void main(String[] args) {
int i = 1;
char c = 1;
System.out.println(i + c);
}
}
✅ Show Answer
Output: 2
char is promoted to int (its ASCII/Unicode value). So 1 + 1 = 2.
Question 17
What is the output?
public class First {
public static void main(String[] args) {
int i = 1;
char c = 1 + 1;
System.out.println(i + c);
}
}
✅ Show Answer
Output: 3
char c = 1 + 1 is allowed because both operands are compile-time constants. c = 2. Then 1 + 2 = 3.
Question 18
What is the output?
public class First {
public static void main(String[] args) {
int i = 1;
char c = 1 + i;
System.out.println(i + c);
}
}
✅ Show Answer
Compilation Error. Cannot assign int to char without explicit cast. Since i is a variable, the expression is not a compile-time constant.
Fix: char c = (char)(1 + i);
Questions 19–24: byte, char & short Arithmetic
Question 19
What is the output?
public class First {
public static void main(String[] args) {
byte a = 1;
byte b = 1;
byte c = a + b;
System.out.println(c);
}
}
✅ Show Answer
Compilation Error. byte + byte results in int due to integer promotion. Requires explicit cast.
Fix: byte c = (byte)(a + b);
Question 20
What is the output?
public class First {
public static void main(String[] args) {
byte a = 1;
byte b = 1;
byte c = (byte)a + (byte)b;
System.out.println(c);
}
}
✅ Show Answer
Compilation Error. Casting each operand separately doesn't help — the result of addition is still promoted to int.
Fix: Cast the whole expression: byte c = (byte)(a + b);
Question 21
What is the output?
public class First {
public static void main(String[] args) {
byte a = 1;
byte b = 1;
byte c = (byte)(a + b);
System.out.println(c);
}
}
✅ Show Answer
Output: 2
The cast applies to the whole result — so int 2 becomes byte 2. Works perfectly.
Question 22
What is the output?
public class First {
public static void main(String[] args) {
byte a = 1;
byte b = 1;
short c = a + b;
System.out.println(c);
}
}
✅ Show Answer
Compilation Error. byte + byte = int. Cannot assign int to short without explicit cast.
Question 23
What is the output?
public class First {
public static void main(String[] args) {
byte a = 1;
char b = 1;
short c = a + b;
System.out.println(c);
}
}
✅ Show Answer
Compilation Error. byte + char = int. Cannot assign int to short without cast.
Question 24
What is the output?
public class First {
public static void main(String[] args) {
char a = 'a';
char b = 'b';
int k = a + b;
System.out.println(k);
}
}
✅ Show Answer
Output: 195
'a' = 97, 'b' = 98. So 97 + 98 = 195.
💡 Remember: 'a' = 97, 'A' = 65, '0' = 48 in ASCII.
Questions 25–32: Wrapper Classes & Autoboxing
Question 25
What is the output?
public class First {
static float i = 10.0;
public static void main(String[] args) {
System.out.println(f);
}
}
✅ Show Answer
Compilation Error. Two issues:
10.0is adoubleliteral — cannot assign tofloatwithoutfsuffix.- Variable
fis never declared. Should bei.
Question 26
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
Float b = i;
System.out.println(b);
}
}
✅ Show Answer
Compilation Error. Cannot convert int to Float (wrapper class) without autoboxing or cast.
Fix: Float b = (float) i; or float b = i;
Question 27
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
Long b = i;
System.out.println(b);
}
}
✅ Show Answer
Compilation Error. Cannot convert int to Long directly. int → long works, but int → Long (wrapper) needs cast.
Fix: Long b = (long) i;
Question 28
What is the output?
public class First {
public static void main(String[] args) {
Long i = 10;
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. 10 is an int literal. Cannot assign int to Long directly.
Fix: Long i = 10L;
Question 29
What is the output?
public class First {
public static void main(String[] args) {
Double i = 10.9f;
System.out.println(i);
}
}
✅ Show Answer
Compilation Error. Cannot assign float to Double (wrapper class).
Fix: Double i = 10.9; or double i = 10.9f;
Question 30
What is the output?
public class First {
public static void main(String[] args) {
Integer i = null;
int k = i;
System.out.println(k);
}
}
✅ Show Answer
Runtime Exception: NullPointerException
Unboxing a null Integer to int throws NPE at runtime — compiles fine!
💡 Interview Tip: This is a classic autoboxing trap.
Question 31
What is the output?
public class First {
static int i;
static Integer j;
public static void main(String[] args) {
System.out.println(i + j);
}
}
✅ Show Answer
Runtime Exception: NullPointerException
i is 0 (default), but j is null. Unboxing j for addition throws NPE.
Question 32
What is the output?
public class First {
static int i = 10;
static {
i++;
}
{
i++;
}
First() {
i++;
}
static void get() {
i++;
}
public static void main(String[] args) {
new First();
System.out.println(i);
}
}
✅ Show Answer
Output: 13
Step-by-step trace:
- Static variable init: i = 10
- Static block runs: i = 11
- main() starts
new First()triggers:- Instance initializer block: i = 12
- Constructor: i = 13
- Print i → 13
💡 Explanation: This tests your knowledge of initialization order — static → instance → constructor.
Questions 33–40: Number Literals
Question 33
What is the output?
public class First {
public static void main(String[] args) {
int i = 1010;
System.out.println(i);
}
}
✅ Show Answer
Output: 1010
Decimal literal. Just a normal number.
Question 34
What is the output?
public class First {
public static void main(String[] args) {
int i = 0101;
System.out.println(i);
}
}
✅ Show Answer
Output: 65
Leading 0 means octal (base 8). 0101 in octal = 1×64 + 0×8 + 1 = 65.
Question 35
What is the output?
public class First {
public static void main(String[] args) {
char c = 0101;
System.out.println(c);
}
}
✅ Show Answer
Output: A
0101 octal = 65 decimal. ASCII 65 = 'A'.
Question 36
What is the output?
public class First {
public static void main(String[] args) {
char a = 0101;
char b = 101;
char c = a + b;
System.out.println(c);
}
}
✅ Show Answer
Compilation Error. char + char = int. Cannot assign to char without cast.
Fix: char c = (char)(a + b); (would output char with ASCII 65+101=166)
Question 37
What is the output?
public class First {
public static void main(String[] args) {
int i = 5_6;
System.out.println(i);
}
}
✅ Show Answer
Output: 56
Underscores in numeric literals are ignored by the compiler (Java 7+). Helps readability.
Question 38
What is the output?
public class First {
public static void main(String[] args) {
int i = 0x1a;
System.out.println(i);
}
}
✅ Show Answer
Output: 26
0x prefix means hexadecimal. 0x1a = 1×16 + 10 = 26.
Question 39
What is the output?
public class First {
public static void main(String[] args) {
int i = 0b11010;
System.out.println(i);
}
}
✅ Show Answer
Output: 26
0b prefix means binary. 11010 = 16+8+2 = 26.
Question 40
What is the output?
public class First {
public static void main(String[] args) {
int i = 0x5a;
int k = 10;
System.out.println(i + k);
}
}
✅ Show Answer
Output: 100
0x5a = 90 decimal. 90 + 10 = 100.
Questions 41–44: Bitwise Operators
Question 41
What is the output?
public class First {
public static void main(String[] args) {
System.out.println(9 | 8);
}
}
✅ Show Answer
Output: 9
Bitwise OR: 9 = 1001, 8 = 1000. OR = 1001 = 9.
Question 42
What is the output?
public class First {
public static void main(String[] args) {
System.out.println(9 & 8);
}
}
✅ Show Answer
Output: 8
Bitwise AND: 9 = 1001, 8 = 1000. AND = 1000 = 8.
Question 43
What is the output?
public class First {
public static void main(String[] args) {
int i = 19;
i = i >> 2;
System.out.println(i);
}
}
✅ Show Answer
Output: 4
Right shift by 2 = divide by 4. 19 / 4 = 4 (integer division).
Question 44
What is the output?
public class First {
public static void main(String[] args) {
int i = 19;
i = i << 2;
System.out.println(i);
}
}
✅ Show Answer
Output: 76
Left shift by 2 = multiply by 4. 19 × 4 = 76.
Questions 45–50: Equality & Reference Comparison
Question 45
What is the output?
public class First {
public static void main(String[] args) {
int i = 10;
char j = 10;
System.out.println(i == j);
}
}
✅ Show Answer
Output: true
char is promoted to int for comparison. 10 == 10 → true.
Question 46
What is the output?
public class First {
public static void main(String[] args) {
int i = 97;
char j = 'a';
System.out.println(i == j);
}
}
✅ Show Answer
Output: true
'a' = 97 in ASCII. 97 == 97 → true.
Question 47
What is the output?
public class First {
public static void main(String[] args) {
int i = (Integer)null;
char j = 'a';
System.out.println(i == j);
}
}
✅ Show Answer
Runtime Exception: NullPointerException
Casting null to Integer and then auto-unboxing to int causes NPE at runtime.
Question 48
What is the output?
public class First {
public static void main(String[] args) {
Integer i = null;
char j = 'a';
System.out.println(i == j);
}
}
✅ Show Answer
Output: false
No NPE here! i is Integer (object), j is char (primitive). Java unboxes i to compare, but since i is null and j is a primitive, it uses reference comparison — null != anything → false.
💡 Tricky: This compiles and runs — no exception! Because the comparison doesn't force unboxing when types differ.
Question 49
What is the output?
public class First {
public static void main(String[] args) {
Integer i = null;
Character j = 'a';
System.out.println(i == j);
}
}
✅ Show Answer
Output: false
Both are objects (wrapper types). Reference comparison. null != any object → false.
Question 50
What is the output?
public class First {
public static void main(String[] args) {
Integer i = 97;
Character j = 'a';
System.out.println(i + j);
}
}
✅ Show Answer
Output: 194
Both are unboxed to int for addition. i = 97, j = 97 (ASCII 'a'). 97 + 97 = 194.
Final Thoughts
Congratulations on completing all 50 questions! 🎉
If you got most of them right, you're well on your way to cracking Java interviews. If some caught you off guard, don't worry — that's exactly why output-based questions are so valuable. They reveal gaps in your understanding that syntax alone can't fix.
🔑 Key Takeaways from This Post
- Static vs Instance: Static methods can't access instance variables directly.
- Default Values: Class fields get defaults (0, null, false), local variables don't.
- Integer Promotion: byte + byte = int, char + char = int.
- Type Casting: Widening is automatic, narrowing needs explicit cast.
- Autoboxing/Unboxing: Watch out for NullPointerException on null wrappers.
- Number Literals: 0x = hex, 0b = binary, 0 = octal, _ = separator.
- Bitwise Operators: &, |, <<, >> work on binary representations.
📚 Practice More
Try to modify these programs and predict new outputs. The more you practice, the sharper your Java intuition becomes.
Want the next level? Check out:
- Java 8 Stream API interview questions
- Java collections output-based questions
- Multithreading tricky programs
- Exception handling output questions
If this post helped you, share it with a friend preparing for interviews!
1 comment:
Nice questions sir👍👍😊
Post a Comment