Menu
Topics Index
...
`

Variable Scope2605


If you need explanation Read this topic

If you need Answer Take test on this topic

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

A. 10, 20 - 15, 10 - 15, 10
B. 10, 20 - 15, 20 - 10, 20
C. 10, 20 - 15, 20 - 15, 20
D. Compilation Error
E. 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.

D

Posted by Janakiram Gupta    2014-09-08 12:46:33


D since variable y is dead and not declared in the main scope

Posted by Vicky Maduri    2014-09-08 12:52:50


D

Posted by Hari Haran S    2014-09-08 12:53:19


D

Posted by Raju VarshnEy    2014-09-08 12:56:15


D....as y is local to the blocks which ends before d 3rd print statemnt

Posted by Rajat Panjwani    2014-09-08 13:36:57


d.

Posted by Shubham Bansal    2014-09-08 14:30:03


D. as variable y is accesible in the second block and not outside that..

Posted by Ankur Verma    2014-09-08 18:59:13


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

Posted by Merit Campus    2014-09-09 04:06:07


© meritcampus 2019

All Rights Reserved.

Open In App