What is the output and why??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output and why???

class A { public static void main(String args[]) { int x; x = 5; { int y = 6; System.out.print(x +""+y); } System.out.println(x +""+y); } }

14th Sep 2021, 3:56 AM
Charizard
Charizard - avatar
1 Answer
+ 4
An error will be thrown, bcz of scope of variables, scope of variable is valid only in a block (braces), you declared y in a block,. So it's scope/validation will only be in this block, you can not use this y outside of that block, so it will throw an error.
14th Sep 2021, 4:09 AM
Rupali Haldiya
Rupali Haldiya - avatar