About Question enthuware.ocajp.i.v7.2.1010 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1010 :

Post by ETS User »

Sun being lazy here :o
Why String label allowed but not for?? just curious!!

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

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by admin »

Because String is not a keyword. for is a keyword.
If you like our products and services, please help us by posting your review here.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by tn1408 »

Hi Paul,
It's correct if we replaced the break point "for" by "String", the program will print "hello" 2 times.
My question is why does the outer loop keep on going and the program ultimately prints "hello" 10 times. I don't see what's keeping it from continue looping. It loops only three times and exits without printing the 3rd time?

Thanks

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

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by admin »

That is because of the break. It is pointing to the outer for loop. So as soon as i+j is >10, break String will cause the outer for loop to break.
If you like our products and services, please help us by posting your review here.

prasanna_ls
Posts: 7
Joined: Wed Nov 11, 2015 11:59 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by prasanna_ls »

I did execute the code that was given in the explanation for the question. Sort of mind blown! So... we can have the same identifier for the class name, variable name, method name aaannnd label name?!

How exactly does this work? For my understanding, I was imagining a separate identifier list for classes, methods and variables till now. Now it seems like I'll have to add the label list in my mind as well. How many of these "lists" are there? ("lists" might not be how they work but I try to imagine it in that way to help me understand stuff. I hope you get what I mean to say i.e. how many different things can have the same identifier?)

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

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by admin »

Only keywords have a special meaning for the compiler. That is why they cannot be used as variable names etc. The meaning of everything else depends on the context and so it is possible to use the same name for a class, a variable, and a label.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Chandu
Posts: 7
Joined: Thu Mar 30, 2017 7:15 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by Chandu »

How come Hello is printed Twice :o

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

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by admin »

Which code are you talking about? The one in the problem statement will not compile. The one in explanation will print hello twice. You will need to execute it step by step and work out the values of each variable at each step.
Paul.
If you like our products and services, please help us by posting your review here.

Chandu
Posts: 7
Joined: Thu Mar 30, 2017 7:15 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by Chandu »

class TestClass{
public static void main(String[] args){
String : for(int i = 0; i< 10; i++){
for (int j = 0; j< 10; j++){
if ( i+ j > 10 ) break String;
}
System.out.println( "hello");
}
}
}
// How hello is printed twice please explain

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

Re: About Question enthuware.ocajp.i.v7.2.1010 :

Post by admin »

When i is 0 or 1, i+j can never be > 10, so that prints a hello in first two iterations of outer for loop.
In the third iteration of outer for loop, break String; will get to execute and both the loops will be terminated.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

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