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

Java & Spring Interview Questions: What Really Happens Under the Hood

  Java & Spring Interview Questions: What Really Happens Under the Hood There is a phase in every developer's career where you can use something without actually understanding what is happening underneath. You know how to use ConcurrentHashMap . You know what @Transactional does. You know that Spring injects dependencies. You know @SpringBootApplication starts your application. And then the interviewer asks: "Okay, but what actually happens internally?" Suddenly, knowing the syntax isn't enough. That is exactly what this blog is about. I'm going to break down some of the Java and Spring concepts that are frequently asked in interviews, but more importantly, I'll try to understand why they work the way they do . Let's start with one of my favourites. 1. ConcurrentHashMap in Java 8: What Actually Happens? Let's first establish the problem. A normal HashMap is not thread-safe. Imagine two threads doing this at the same time: map.put("A",...

Spring & Hibernate Interview Questions: What Really Happens Under the Hood?

  Spring & Hibernate Interview Questions: What Really Happens Under the Hood? After my previous blog on Java & Spring internals , one thing became pretty clear: Knowing the annotation is easy. Explaining what happens because of that annotation is where the real interview begins. You can write: @Transactional But do you know how Spring actually starts the transaction? You can add a Spring Boot dependency. But do you know why Spring suddenly creates a bunch of beans? You can modify a JPA entity without explicitly calling save() . But do you know why Hibernate still sends an UPDATE query? And here's the one I really like: Two users try to buy the last item in stock at exactly the same time. What happens? These are the kinds of questions that move an interview from "Do you know Spring?" to "Do you actually understand Spring?" So, let's go under the hood again. 🔹 Spring & Spring Boot 1. @Bean vs @Component — What's Actually Different? At firs...