Menu
Topics Index
...
`

Scope of variables


If you need explanation Read this topic

If you need Answer Take test on this topic

Scope of variables
What will be the output of the following program?
public class ScopeOfVariables {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        {
            System.out.print(x + ", " + y);
        }
        {
            x = 15;
            System.out.print(" - " + x + ", " + y);
        }
        System.out.print(" - " + x + ", " + y);
    }
}

A. 10, 20 - 15, 20 - 15, 20
B. 10, 20 - 15, 20 - 10, 20
C. Compilation Error
D. Runtime Error
Topic: Scope Of Variables In Nested/Multiple Blocks

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.

Answer is A.

Posted by Amar Kumar Biradar    2014-01-20 12:52:26


A.is the answer.as simply x vvalue  changes as its scope changes in diffrent blocks.

Posted by Shabaaz Shaikz    2014-01-20 12:55:55


ans is a because in java print statement takes the nearest asigned values to the variables

Posted by Kiran Karrolla    2014-01-20 12:56:21


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

Posted by Merit Campus    2014-01-21 03:45:17


© meritcampus 2019

All Rights Reserved.

Open In App