Output of Java Program | Set 7

Last Updated : 23 Jan, 2026

Predict the output of the following Java programs and understand the concepts involved.

Program 1 :

Java
public class Calculator
{
    int num = 100;
    public void calc(int num)  { this.num = num * 10;  }
    public void printNum()     { System.out.println(num); }

    public static void main(String[] args)
    {
        Calculator obj = new Calculator();
        obj.calc(2);
        obj.printNum();
    }
}

Options :

A) 20 B) 100 C) 1000 D) 2

Answer : A) 20

Explanation :The method parameter num shadows the instance variable num. this.num refers to the instance variable, while num refers to the local parameter whose value is 2.

Program 2

Java
public class MyStuff {
    String name;
    MyStuff(String n) {
        name = n;
    }
    public static void main(String[] args) {
        MyStuff m1 = new MyStuff("guitar");
        MyStuff m2 = new MyStuff("tv");
        System.out.println(m2.equals(m1));
    }
    @Override
    public boolean equals(Object obj) {
        MyStuff m = (MyStuff) obj;
        if (m.name != null) {
            return true;
        }
        return false;
    }
}

Options :

A)Output is true and fulfills equals() contract B) Output is false and fulfills equals() contract
C)Output is true and does NOT fulfill equals() contract. D)Output is false and does NOT fulfill equals() contract

Answer : C) Output is true and does NOT fulfill the equals() contract

Explanation : The overridden equals() method does not compare object states meaningfully. It violates the equals() contract by returning true for any non-null name, regardless of object equality.

Program 3

Java
class Alpha {
    public String type = "a ";
    public Alpha() {
        System.out.print("alpha ");
    }
}
public class Beta extends Alpha {
    public Beta() {
        System.out.print("beta ");
    }
    void go() {
        type = "b ";
        System.out.print(this.type + super.type);
    }
    public static void main(String[] args) {
        new Beta().go();
    }
}

Options :

A) alpha beta b b B) alpha beta a b C) beta alpha b b D) beta alpha a b

Answer : A) alpha beta b b

Explanation : The Alpha constructor executes first due to implicit super() call. There is only one type variable, so both this.type and super.type refer to the same field.  

Program 4

Java
public class Test {
    public static void main(String[] args) {
        StringBuilder s1 = new StringBuilder("Java");
        String s2 = "Love";

        s1.append(s2);
        s1.substring(4);
        int foundAt = s1.indexOf(s2);
        System.out.println(foundAt);
    }
}

Options :

A) -1 B) 3 C) 4 D) StringIndexOutOfBoundsException

Answer : C) 4

Explanation : append() modifies the StringBuilder object, resulting in "JavaLove". The return value of substring() is ignored, so indexOf("Love") returns 4.

Program 5

Java
class Writer {
    public static void write() {
        System.out.println("Writing...");
    }
}
class Author extends Writer {
    public static void write() {
        System.out.println("Writing book");
    }
}
public class Programmer extends Author {
    public static void write() {
        System.out.println("Writing code");
    }
    public static void main(String[] args) {
        Author a = new Programmer();
        a.write();
    }
}

Options :

A) Writing... B) Writing book C) Writing code D) Compilation fails

Answer : B) Writing book

Explanation : Static methods are hidden, not overridden. Method selection depends on the reference type, not the object type.

Comment