You will be Often challenged on your Knowledge based on few tricky interview questions from interviewers. Here we have listed down all such questions. If you spend some time with this, you will definitely crack any interview of any Experience level.

Scenario 1:

You have two overloaded methods named m1:

  1. m1 with parameter m1(String s1)
  2. m1 with parameter m1(Object o1)

In the main method, you call m1 by passing a null argument.

Question:

Which method will be called, and why?

Explanation:

In Java, when you have overloaded methods and you call one of them with an argument, the compiler looks for the most specific method that matches the argument type.

In this scenario, you’re passing a null argument. Both String and Object can accept null, so they are both valid candidates. However, Java will choose the most specific method, which in this case is m1(String s1) because String is more specific than Object.

Therefore, the m1 method with parameter m1(String s1) will be called.

Example code:

public class MethodOverloadingExample {
    public static void m1(String s1) {
        System.out.println("Method with String argument called");
    }
    
    public static void m1(Object o1) {
        System.out.println("Method with Object argument called");
    }
    
    public static void main(String[] args) {
        m1(null); // Output: Method with String argument called
    }
}

Output:

Method with String argument called

In this code, when m1(null) is called from the main method, the m1(String s1) method is invoked because it’s more specific than the m1(Object o1) method.

Scenario 2:

You have a class hierarchy consisting of a base class named Animal and two derived classes named Dog and Cat. Each class has a method named makeSound() which represents the sound each animal makes.

Question:

You have a method named performAction that takes an instance of the Animal class as an argument and calls the makeSound() method of that animal. If you pass an instance of Dog or Cat to this method, which makeSound() method will be called, and why?

class Animal {
    public void makeSound() {
        System.out.println("Animal makes some sound");
    }
}

class Dog extends Animal {
    public void makeSound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    public void makeSound() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void performAction(Animal animal) {
        animal.makeSound();
    }

    public static void main(String[] args) {
        Animal dog = new Dog();
        Animal cat = new Cat();

        performAction(dog); // Output: Dog barks
        performAction(cat); // Output: Cat meows
    }
}

Output:

Dog barks
Cat meows

Explanation:

In Java, method invocation is determined by the actual type of the object, not the reference type. So when you pass an instance of a subclass to a method that accepts an instance of the superclass, the method to be invoked is determined by the actual type of the object.

In this scenario, if you pass an instance of Dog or Cat to the performAction method, the makeSound() method of the respective subclass (Dog or Cat) will be called. This is because Java’s dynamic method dispatch ensures that the appropriate method based on the actual type of the object is invoked.

In this code, when performAction(dog) and performAction(cat) are called, the makeSound() method of Dog and Cat is invoked respectively, based on the actual types of the objects passed.

Scenario:

You have a method named manipulateString that takes a String argument and performs some string manipulations on it.

Question:

Consider the following code snippet:

public class StringManipulationExample {
    public static void manipulateString(String str) {
        str = str.toUpperCase(); // This will not modify the original string
    }
    
    public static void main(String[] args) {
        String str = "hello";
        manipulateString(str);
        System.out.println(str); // Output: hello
    }
}

What will be the output, and why?

Output:

hello

Explanation:

In Java, strings are immutable, meaning their values cannot be changed after they are created. When you pass a string to a method, you’re passing a reference to the string object, not the string itself. Any changes made to the string inside the method will not affect the original string outside the method.

In this scenario, even if manipulateString modifies the string, it will not affect the original string referenced by str. Therefore, the output will be "hello".