What is the output of this program?
class Variable6
{
public static void main(String[] alt)
{
int i=0;
for(int j=0;j<5;j++)
i=i+j;
System.out.println(i+" "+j);
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- variable "j" is declared inside for loop so it becomes local variable and local
variable has scope only that block where it is declared. After that block execution
completion, automatically local variables will be destroyed.
*/
{
public static void main(String[] alt)
{
int i=0;
for(int j=0;j<5;j++)
i=i+j;
System.out.println(i+" "+j);
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- variable "j" is declared inside for loop so it becomes local variable and local
variable has scope only that block where it is declared. After that block execution
completion, automatically local variables will be destroyed.
*/
0 comments: