enthuware.ocajp.i.v8.2.1327

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sulakshana
Posts: 14
Joined: Thu Dec 22, 2016 8:55 pm
Contact:

enthuware.ocajp.i.v8.2.1327

Post by sulakshana »

how can it print 0 0;
i = 0 and j = 0 are out of scope after for statement
int i, int j are local variables in main method since it cant get default values like instance variables
please explain............
thanx.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v8.2.1327

Post by admin »

i and j are defined before the for loop. So why do you think they will go out of scope after the for loop?
If you like our products and services, please help us by posting your review here.

sulakshana
Posts: 14
Joined: Thu Dec 22, 2016 8:55 pm
Contact:

Re: enthuware.ocajp.i.v8.2.1327

Post by sulakshana »

Yes it is defined before for loop but values are assigned within for loop which will go out of scope when reaching second println statement. Since local variables are not assigned default 0 values how can it print 0 0s by using second println statement?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v8.2.1327

Post by admin »

You seem to be confused between the scope of variable and assignment. If a variable is in scope, you can assign it a value, which is what the initialization section of the for loop is doing in this case. The value that you assign doesn't go out of scope. So you will see those values even after the for loop.
If you like our products and services, please help us by posting your review here.

scyber
Posts: 3
Joined: Sat Jul 08, 2017 4:42 am
Contact:

Re: enthuware.ocajp.i.v8.2.1327

Post by scyber »

The answer out of scope would not appear if look carefully
for( j=0,i=0; i<j; ++j,i++)

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 65 guests