Menu
Question Index
...

What will be the output of the following program?

class B {

    static int i;
    static int j;
    static {
        i = 15;
        j = i - 5;
    }

    public static void main(String[] args) {
        int i = 0;
        A a = null;
        for (; i < 3; i++) {
            a = new A();
            a.i = B.i;
            B.i += a.add(a.operate(i));
        }
        System.out.println(B.i + " " + B.j + " " + i + " " + a.i);
    }
}

class A {

    int i = 0;

    int operate(int i) {
        if (B.j - i == i * i * i) return -i;
        return i * i;
    }

    int add(int i) {
        return this.i + i;
    }
}


14 10 3 0
14 10 3 16
120 10 3 61
Some other output not shown here
Compilation Error

Doubts

Problems

Topic: Static Keyword In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App