๐ง 10 Java Output Questions That Will Sharpen Your Mind
One of the best ways to strengthen your Java skills is by predicting what a program prints without running it. These snippets test your understanding of concepts like operators, memory, method overloading, strings, and more. Ready for a brain workout?
✅ 1. String Pool Puzzle
public class Test1 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
๐ก Output:
false
true
✅ 2. Post-Increment Confusion
public class Test2 {
public static void main(String[] args) {
int x = 5;
int y = x++;
System.out.println(x);
System.out.println(y);
}
}
๐ก Output:
6
5
✅ 3. Static vs Instance Method
class Demo {
static void print() {
System.out.println("Static");
}
void show() {
System.out.println("Instance");
}
public static void main(String[] args) {
Demo d = null;
d.print();
// d.show(); // Uncomment this line and see what happens
}
}
๐ก Output:
Static
(Calling static method via null object is allowed, but not instance methods.)
✅ 4. String Immutability Trap
public class Test4 {
public static void main(String[] args) {
String s = "abc";
s.concat("def");
System.out.println(s);
}
}
๐ก Output:
abc
(Strings are immutable. concat
doesn’t change the original unless assigned.)
✅ 5. Switch Without Break
public class Test5 {
public static void main(String[] args) {
int day = 2;
switch(day) {
case 1: System.out.println("Mon");
case 2: System.out.println("Tue");
case 3: System.out.println("Wed");
}
}
}
๐ก Output:
Tue
Wed
✅ 6. Overloading Mystery
public class Test6 {
void test(int a) {
System.out.println("int");
}
void test(Integer a) {
System.out.println("Integer");
}
public static void main(String[] args) {
new Test6().test(10);
}
}
๐ก Output:
int
(Primitive match is preferred over wrapper type.)
✅ 7. Array Printing
public class Test7 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arr);
}
}
๐ก Output:
[I@15db9742
(Default toString()
on array shows object reference. Use Arrays.toString()
for readable output.)
✅ 8. Final Parameters
public class Test8 {
public static void main(final String[] args) {
args[0] = "Changed";
System.out.println(args[0]);
}
}
๐ก Output:
Changed
(You can change contents of final
reference, not the reference itself.)
✅ 9. Null Argument Overloading
public class Test9 {
void print(Object o) {
System.out.println("Object");
}
void print(String s) {
System.out.println("String");
}
public static void main(String[] args) {
new Test9().print(null);
}
}
๐ก Output:
String
(Most specific method is chosen when calling with null
.)
✅ 10. Wrapper Caching
public class Test10 {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer x = 200;
Integer y = 200;
System.out.println(a == b);
System.out.println(x == y);
}
}
๐ก Output:
true
false
(Integer caching works for values between -128 and 127.)
๐ก Final Thoughts
These snippets aren't just trivia — they reveal how Java handles memory, types, and execution flow. Try modifying each example slightly to see how behavior changes!
Would you like this exported as an HTML/Markdown blog, or want an image for social sharing?
This Content Sponsored by Buymote Shopping app
BuyMote E-Shopping Application is One of the Online Shopping App
Now Available on Play Store & App Store (Buymote E-Shopping)
Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8
Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication