Menu
Topics Index
...
`

Variable Scope


If you need explanation Read this topic

If you need Answer Take test on this topic

Variable Scope
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;
    }
}

A. 14 10 3 0
B. 14 10 3 16
C. 120 10 3 61
D. Some other output not shown here
E. Compilation Error
Topic: Static Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

D is ans such as 126 0 3 61 bcz v r at starting B.j returns value of 120 nxt 10.

Posted by Shabaaz Shaikz    2014-08-06 12:57:49


Ans is B.
As B.i becomes 14, a.i becomes 16, i will be 3, B.j will be 10.

Posted by Gopinath Manchikanti    2014-08-06 13:55:00


ans c.
B.i 120,B.j =10,
(15-5)
i=3,insid main i=3 for loop goes out of loop so i s 3
a.i s 61

Posted by Maheshwari Natarajan    2014-08-06 14:00:16


answer c.
for loop iterates upto 3,
B.i assign with method add(operat) at the end value of B.i 120,
B.j prints static value previously assigned
i s 3 in main
upto 2 nd iterat B.i value 61 n assgnd to a.i

Posted by NagaRaj Nataraj    2014-08-06 14:07:03


d........som other output not shown here

Posted by Arul Palanisamy    2014-08-07 03:06:54


Congratulations Maheshwari Natarajan You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-07 04:48:04


© meritcampus 2019

All Rights Reserved.

Open In App