Java interviews often contain tricky output-based questions that test your understanding of core Java concepts rather than your ability to memorize syntax.
These questions typically cover String Pool, Wrapper Classes, Method Overloading, Inheritance, Exception Handling, finally blocks, Increment Operators and Java Memory concepts.
Let's challenge your Java knowledge with 20 popular interview questions.
Question 1
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
Output
true
Explanation
String literals are stored in the String Pool. Both variables point to the same memory location.
Question 2
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);
Output
false
Explanation
Two separate objects are created in heap memory. Therefore reference comparison returns false.
Question 3
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1.equals(s2));
Output
true
equals() compares content, not memory references.
Question 4
Integer a = 127;
Integer b = 127;
System.out.println(a == b);
Output
true
Explanation
Integer values from -128 to 127 are cached by Java. Both references point to the same cached object.
Question 5
Integer a = 128;
Integer b = 128;
System.out.println(a == b);
Output
false
Explanation
128 falls outside Integer Cache range. Separate objects are created.
Question 6
String str = null;
System.out.println(str instanceof String);
Output
false
Explanation
instanceof never throws NullPointerException. It simply returns false.
Question 7
System.out.println(10 + 20 + "Java");
Output
30Java
Addition occurs before String concatenation.
Question 8
System.out.println("Java" + 10 + 20);
Output
Java1020
Once a String appears, subsequent operations become concatenations.
Question 9
int x = 10;
System.out.println(x++ + ++x);
Output
22
Explanation
x++ returns 10 then x becomes 11. ++x makes x 12 and returns 12. Result = 10 + 12.
Question 10
int x = 5;
System.out.println(x+++x);
Output
11
Explanation
Compiler interprets this as:
x++ + x
Result becomes 5 + 6.
Question 11
try {
return;
}
finally {
System.out.println("Finally");
}
Output
Finally
finally block executes before method exits.
Question 12
try {
System.exit(0);
}
finally {
System.out.println("Finally");
}
Output
No Output
JVM terminates immediately.
Question 13
StringBuilder sb =
new StringBuilder("Java");
sb.append(" 8");
System.out.println(sb);
Output
Java 8
StringBuilder is mutable.
Question 14
final StringBuilder sb =
new StringBuilder("Java");
sb.append(" 21");
System.out.println(sb);
Output
Java 21
Explanation
final prevents reference reassignment, not object modification.
Question 15
String s = "Java";
s.concat(" 8");
System.out.println(s);
Output
Java
String objects are immutable. concat() returns a new String.
Question 16
System.out.println(Math.round(2.5));
System.out.println(Math.round(3.5));
Output
3
4
Question 17
String s = null;
System.out.println(String.valueOf(s));
Output
null
String.valueOf() safely handles null references.
Question 18
List<String> list =
Arrays.asList("Java","Spring");
list.add("Kafka");
Output
UnsupportedOperationException
Explanation
Arrays.asList() returns a fixed-size list.
Question 19
String s = "Java";
System.out.println(s.charAt(4));
Output
StringIndexOutOfBoundsException
Valid indexes are 0 to 3.
Question 20
String s1 = "Ja" + "va";
String s2 = "Java";
System.out.println(s1 == s2);
Output
true
Explanation
Compiler performs String constant folding and uses the String Pool.
Most Common Topics Covered
- String Pool
- Wrapper Classes
- Integer Cache
- StringBuilder
- Exception Handling
- finally Block
- instanceof Operator
- Collection Framework
- Immutability
- Operators
Interview Tips
- Never assume == compares content.
- Know Integer cache behaviour.
- Understand String Pool concepts.
- Remember finally block execution rules.
- Practice output-based questions regularly.
- Focus on Java memory concepts and object references.
Conclusion
Output-based Java questions reveal how well a developer understands JVM behavior, String Pool, Collections, Wrapper Classes and Exception Handling. Mastering these questions can significantly improve interview performance for both mid-level and senior Java developers.
Keywords
Java Output Questions, Java Interview Puzzles, Tricky Java Questions, Java Coding Questions, Java Interview Preparation, Java String Pool Questions, Java Integer Cache, Java instanceof Examples, Java Output Programs, Core Java Interview Questions, Java Will It Work Questions, Java Puzzle Examples, Java Coding Interview Questions, Java Senior Developer Interview, Java Technical Interview Preparation
Reviewed by 13thGhost
on
3:42 PM
Rating:

No comments: