0 votes
156 views
in JAVA by (98.9k points)
edited

What will be the output of the following Java program?

  1.     class variable_scope 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int x;
  6.             x = 5;
  7.             {
  8. 	        int y = 6;
  9. 	        System.out.print(x + " " + y);
  10.             }
  11.             System.out.println(x + " " + y);
  12.         } 
  13.     }

a) Compilation error
b) Runtime error
c) 5 6 5 6
d) 5 6 5

1 Answer

0 votes
by (98.9k points)
edited
Answer: a
Explanation: Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.

Related questions

0 votes
1 answer 153 views
asked Jul 20, 2022 in JAVA by Doubtly (98.9k points)
0 votes
1 answer 99 views
asked Jul 20, 2022 in JAVA by Doubtly (98.9k points)
0 votes
0 answers 114 views
asked Aug 9, 2022 in JAVA by Doubtly (98.9k points)
0 votes
1 answer 248 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

557 users

...