Hello everyone! Hope you’re sipping your coffee just right—because I’ve brewed something equally strong for your brain.
Welcome to a blog that dives into 5 deceptively tricky Java interview questions—the kind that test not just what you know, but how well you understand Java under the hood. If you, like me, share a love-hate relationship with this language, this blog might just hit home.
So, let's get started...
Question 1: What will be the output of the following code? Why?
class Parent {
static void staticMethod() {
System.out.println("Static method in Parent");
}
private void privateMethod() {
System.out.println("Private method in Parent");
}
void show() {
staticMethod();
privateMethod();
}
}
class Child extends Parent {
static void staticMethod() {
System.out.println("Static method in Child");
}
private void privateMethod() {
System.out.println("Private method in Child");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
If your answer is "Private method in Child"—Bingoo!! you got it wrong!
Private methods and static methods cannot be inherited, and hence cannot be overridden. The method calls in the show()
method will resolve to the parent class versions.
Hence the output is :
Static method in Parent
Private method in Parent
Question 2: What is the output of this finally
block example?
public class Test {
public static void main(String[] args) {
System.out.println(test());
}
static int test() {
try {
return 1;
} finally {
return 2;
}
}
}
Answer should be 1
, right?
I mean, we do have a return statement in the try
block!
Haha—might have happened in any ordinary language, but this is Java.
Even the return from a try
block gets overridden if a finally
block returns something.
So it's the finally
block that has the final say in this matter.
So the output is :
2
Question 3: What does this print? (Static block + main method twist)
public class Main {
static {
System.out.println("Static block");
}
public Main() {
System.out.println("Constructor");
}
public static void main(String[] args) {
System.out.println("Main method");
Main obj = new Main();
}
}
If anything, you might be thinking—void main
is the heart and soul of a Java program, so execution must start from here, right?
We should see Main method printed first.
But huh, Java has got its priorities different!
Before main()
even gets a chance to speak, Java ensures that all the static blocks are executed. And not just that—they're executed only once, when the class is loaded into memory.
After that, main()
takes the stage, and finally, when we create an object, the constructor joins the party.
Here we are with the output :
Static block
Main method
Constructor
After that, main()
takes the stage, and finally, when we create an object, the constructor joins the party.
Question 4: What happens if you call System.exit()
inside a try
block?
public class Main {
public static void main(String[] args) {
try {
System.out.println("Try block");
System.exit(0);
} finally {
System.out.println("Finally block");
}
}
}
If you think finally
always runs no matter what—Alas!! you got it wrong this time.
System.exit(0)
forcefully terminates the JVM. That means the program ends right there—no more execution, no cleanup, and yes, even your loyal finally
block is left behind.
Did I mention loyalty being traded off? Déjà vu?
So the output is :
Try block
Even Java doesn't believe in goodbyes when it exists!
Question 5: What’s the output of this string comparison?
public class Main {
public static void main(String[] args) {
String a = "hello";
String b = "he" + "llo";
String c = new String("hello");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
}
}
Let’s guess: you're thinking true true true
, right? Because "hello"
is "hello"
, and Java should be smart enough to recognize that?
Bingoo!! you got it (partly) wrong.
Here’s what’s happening under the hood:
-
a == b
→ true: because both are compile-time string literals. Java optimizes and interns them to the same memory reference. -
a == c
→ false:new String("hello")
creates a new object in heap, so it’s not the same reference. -
a.equals(c)
→ true: because.equals()
checks value, and yep, both say "hello".
true
false
true
So if you’ve made it this far—congratulations!
You're learning... ahem, we are learning. Together.
I’ll catch you soon with more... okay, let's not rather say it now.
Until then, keep sipping that coffee ☕
And hey, drop a comment if you enjoyed reading—or even if you didn’t. I’ll still pretend I didn’t cry.
Informative!
ReplyDelete