Java Programs and their outputs

Java Programs and their outputs

  1. Question:
class A {
    int x = 10;
    A() {
        print();
    }
    void print() {
        System.out.println("x = " + x);
    }
}
class B extends A {
    int x = 20;
    void print() {
        System.out.println("x = " + x);
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

x = 0
x = 20

In the main method, an object of class B is created and assigned to a reference of type A. When the constructor of A is called, the print() method is invoked. However, since the object being constructed is of type B, the print() method of B is invoked, resulting in the output x = 0. When obj.print() is called in the main method, it calls the print() method of B, giving the output x = 20.

  1. Question:
class A {
    static void print() {
        System.out.println("Hello");
    }
}
class B extends A {
    static void print() {
        System.out.println("World");
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

Hello

Even though the reference obj is of type A, the object being created is of type B. However, since the print() method is declared as static in both classes A and B, the method is resolved at compile time based on the reference type. In this case, the reference type is A, so the static method print() of class A is called, resulting in the output Hello.

  1. Question:
class A {
    void method1() {
        System.out.println("A");
    }
}
class B extends A {
    void method1() {
        System.out.println("B");
    }
    void method2() {
        method1();
    }
}
class C extends B {
    void method1() {
        System.out.println("C");
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new C();
        obj.method1();
        ((B) obj).method1();
        ((B) obj).method2();
    }
}

What is the output? Explanation: The output will be:

C
C
C

In the main method, an object of class C is created and assigned to a reference of type A. When obj.method1() is called, the overridden method method1() in class C is invoked, resulting in the output C. Casting obj to type B and calling ((B) obj).method1() still invokes the overridden method method1() in class C, so the output remains C. Similarly, calling ((B) obj).method2() also invokes the method1() of class C, resulting in the output C.

  1. Question:
class A {
    static int x = 10;
    void print() {
        System.out.println("A: " + x);
    }
}
class B extends A {


    static int x = 20;
    void print() {
        System.out.println("B: " + x);
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

B: 20

Even though the reference obj is of type A, the object being created is of type B. However, the field x is resolved at compile time based on the reference type. Since the field x is declared as static, it is resolved based on the reference type A. Hence, the output is B: 20.

  1. Question:
class A {
    A() {
        System.out.println("A");
    }
}
class B extends A {
    B() {
        System.out.println("B");
    }
}
class C extends B {
    C() {
        System.out.println("C");
    }
}
class Main {
    public static void main(String[] args) {
        C obj = new C();
    }
}

What is the output? Explanation: The output will be:

A
B
C

When the object of class C is created, the constructor chain is executed in order. First, the constructor of class A is called, printing A. Then, the constructor of class B is called (since B extends A), printing B. Finally, the constructor of class C is called (since C extends B), printing C.

  1. Question:
class A {
    int x = 10;
    void print() {
        System.out.println("A: " + x);
    }
}
class B extends A {
    int x = 20;
    void print() {
        System.out.println("B: " + x);
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        System.out.println(obj.x);
        obj.print();
    }
}

What is the output? Explanation: The output will be:

10
B: 20

Even though the reference obj is of type A, the object being created is of type B. However, when accessing the field x, it is resolved at compile time based on the reference type. Hence, obj.x prints the value of x from class A, which is 10. However, when invoking the print() method, dynamic method dispatch occurs, and the overridden method print() in class B is invoked, resulting in the output B: 20.

  1. Question:
class A {
    static void print() {
        System.out.println("A");
    }
}
class B extends A {
    static void print() {
        System.out.println("B");
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
        B.print();
    }
}

What is the output? Explanation: The output will be:

A
B

Even though the reference obj is of type A, the object being created is of type B. However, since the print() method is declared as static in both classes A and B, the method is resolved at compile time based on the reference type. In this case, the reference type is A, so the static method print() of class A is called when obj.print() is invoked

, resulting in the output A. When calling B.print(), the static method print() of class B is directly called, resulting in the output B.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        super.print();
        System.out.println("B");
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

A
B

In the print() method of class B, the super.print() statement invokes the print() method of class A, printing A. After that, it continues with the remaining statements in class B's print() method, printing B. Therefore, the output is A followed by B.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        System.out.println("B");
    }
}
class C extends B {
    void print() {
        super.print();
        System.out.println("C");
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new C();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

B
C

When the print() method is called on obj, which is of type A, dynamic method dispatch occurs. Since the actual object being created is of type C, the print() method of class C is invoked. In class C's print() method, the super.print() statement invokes the print() method of class B, resulting in the output B. After that, it continues with the remaining statements in class C's print() method, printing C. Therefore, the output is B followed by C.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        super.print();
        System.out.println("B");
    }
}
class C extends B {
    void print() {
        super.print();
        System.out.println("C");
    }
}
class Main {
    public static void main(String[] args) {
        B obj = new C();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

A
B
C

When the print() method is called on obj, which is of type B, dynamic method dispatch occurs. Since the actual object being created is of type C, the print() method of class C is invoked. In class C's print() method, the super.print() statement invokes the print() method of class B, resulting in the output A (from super.print()) followed by B. After that, it continues with the remaining statements in class C's print() method, printing C. Therefore, the output is A, B, and C in sequence.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        System.out.println("B");
    }
}
class Main {
    public static void main(String[] args) {


        A obj1 = new A();
        A obj2 = new B();
        obj1.print();
        obj2.print();
    }
}

What is the output? Explanation: The output will be:

A
B

In the main method, obj1 is of type A and obj2 is of type B. When calling obj1.print(), it invokes the print() method of class A, resulting in the output A. When calling obj2.print(), dynamic method dispatch occurs, and the print() method of class B is invoked (since the actual object being created is of type B), resulting in the output B. Therefore, the output is A followed by B.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        System.out.println("B");
    }
}
class Main {
    public static void main(String[] args) {
        B obj1 = new B();
        A obj2 = obj1;
        obj1.print();
        obj2.print();
    }
}

What is the output? Explanation: The output will be:

B
B

In the main method, obj1 is of type B, and obj2 is of type A but references the same object as obj1. When calling obj1.print(), it invokes the print() method of class B, resulting in the output B. Similarly, when calling obj2.print(), dynamic method dispatch occurs, and the print() method of class B is invoked (since the actual object being referenced is of type B), resulting in the output B. Therefore, the output is B twice.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        System.out.println("B");
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        B b = (B) obj;
        b.print();
    }
}

What is the output? Explanation: The output will be:

B

In the main method, an object of type B is created and assigned to a reference of type A. However, when obj is explicitly cast to type B (B b = (B) obj;), it successfully downcasts the object to type B. Consequently, when calling b.print(), it invokes the print() method of class B, resulting in the output B.

  1. Question:
class A {
    void print() {
        System.out.println("A");
    }
}
class B extends A {
    void print() {
        System.out.println("B");
    }
}
class Main {
    public static void main(String[] args) {
        B obj = new B();
        A a = obj;
        a.print();
    }
}

What is the output? Explanation: The output will be:

B

In the main method, an object of type B is created and assigned to a reference of type B. When obj is assigned to A (A a = obj;), it is upcasted to type A, but the actual object being referenced is still of type B. Therefore, when calling a.print(), dynamic method dispatch occurs

, and the print() method of class B is invoked, resulting in the output B.

  1. Question:
class A {
    int x = 10;
}
class B extends A {
    int x = 20;
}
class C extends B {
    int x = 30;
    void print() {
        System.out.println(super.super.x);
    }
}
class Main {
    public static void main(String[] args) {
        C obj = new C();
        obj.print();
    }
}

What is the output? Explanation: The code will produce a compilation error. Explanation: In Java, it is not possible to access the grandparent's instance variable using the super keyword. The super keyword is used to access the immediate superclass's members. Therefore, the code will result in a compilation error, stating that super.super.x is not valid.

  1. Question:
class A {
    int x = 10;
}
class B extends A {
    int x = 20;
    void print() {
        System.out.println(super.x);
    }
}
class C extends B {
    int x = 30;
    void print() {
        System.out.println(super.x);
    }
}
class Main {
    public static void main(String[] args) {
        C obj = new C();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

20

In class C's print() method, super.x refers to the instance variable x in class B (the immediate superclass of C). Therefore, when calling obj.print(), it prints the value of x from class B, which is 20.

  1. Question:
class A {
    static int x = 10;
    void print() {
        System.out.println(x);
    }
}
class B extends A {
    static int x = 20;
    void print() {
        System.out.println(x);
    }
}
class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

10

Even though the object being created is of type B, the static variable x is resolved at compile time based on the reference type, which is A. Hence, when calling obj.print(), it invokes the print() method of class A, and the value of x printed is the value of x from class A, which is 10.

  1. Question:
class A {
    static int x = 10;
    void print() {
        System.out.println(x);
    }
}
class B extends A {
    static int x = 20;
    void print() {
        System.out.println(super.x);
    }
}
class Main {
    public static void main(String[] args) {
        B obj = new B();
        obj.print();
    }
}

What is the output? Explanation: The output will be:

10

In class B's print() method, super.x refers to the static variable x in class A (the superclass of B). Therefore, when calling obj.print(), it prints the value of x from class A, which is 10.

  1. Question:
class A {
    static int x = 10;
    void print() {
        System.out.println(x);
    }
}
class B extends A {
    static int x = 

20;
    void print() {
        System.out.println(x);
    }
}
class Main {
    public static void main(String[] args) {
        B obj = new B();
        A a = obj;
        a.print();
    }
}

What is the output? Explanation: The output will be:

10

Even though the object being created is of type B, the static variable x is resolved at compile time based on the reference type. In this case, a is of type A, so when calling a.print(), it invokes the print() method of class A, and the value of x printed is the value of x from class A, which is 10.

  1. Question:
class A {
    static int x = 10;
    void print() {
        System.out.println(x);
    }
}
class B extends A {
    static int x = 20;
    void print() {
        System.out.println(super.x);
    }
}
class Main {
    public static void main(String[] args) {
        B obj = new B();
        A a = obj;
        a.print();
    }
}

What is the output? Explanation: The output will be:

10

In class B's print() method, super.x refers to the static variable x in class A (the superclass of B). Even when the object is upcasted to A and a.print() is called, the method resolution is still based on the reference type A, so the print() method of class A is invoked. Consequently, it prints the value of x from class A, which is 10.