Skip to main content

Think You Know Java? These 5 Questions Say Otherwise

 


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 == btrue: because both are compile-time string literals. Java optimizes and interns them to the same memory reference.

  • a == cfalse: 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".

So here's what we got : 


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. 

Comments

Post a Comment

Popular posts from this blog

Are we happy enough?

 Hey Mitra! how's everything? What was that hilarious thing that made you laugh absurdly today? Who was the person who filled your day with all the love in the world just by a small act of kindness? Let me reverse the question how many lives did you fill with positivity or humbleness today ? If the answer is none, then this blog may have something to add  to your life. Let me share with you an interesting event that just took place. Two children were playing a game which they called "How many". Rules were simple, one had to ask the questions by adding "how many" like, " how many planets are there in our solar system ?" Another had to answer. The game was quite interesting  and suddenly a question appeared "how many muscles does it take to smile?" The child seeking the answer smiled curiously and tried to locate all the muscles involved in smiling right then at the moment and his friend helped him too in a manner like," ohh look, there...

The Lost Childhood

The lost childhood Hello everybody...Our lives have been quite clumsy, unusual and tiring but but but do you remember the time when our only tension was the pending homework?! Ofc you do remember...why not take a dive into what special we had back then TV shows Netflix and all is cool but do you remember when all you needed was Shaka laka BOOM BOOM pencil or Ben 10 Omnitrix. When you waited for whole week just for that one hour episode of M.A.D. When you internally wept when Ishan from Tare Zameen Par was sent to hostel and jumped out of joy when Aamir Khan entered singing Bam Bam Bole! Life right then was so simple ugh? Ohh did I not mention about Shinchan, Doraemon, Noddy, DBZ and on and on. I guess list wouldn't end any soon. Sharat! Uff. The Games we played You may have played PUBG, COD, Free Fire or Among Us in recent times right? But what about playing Pakdam Pakdai with your paltan Or Barafpani? Or just gathering together and playing Marbles and that game with one leg up (te...