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

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

Moderator: admin

Prachi

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

Post by Prachi »

In this question, there is no 'public static void main(String[] args)' statement. How will the program then even run, and give an output? Can you please clarify on this what are we supposed to do in such a case?

Thanks

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

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

Post by admin »

In this case, the question doesn't say that it is executed from the command line. It just says, "what will be the output if 0 (integer value zero) is passed to loopTest()". So the absence main method is not an issue here.

The loopTest method can be executed from another class (which may in turn have a main method) using:
new TestClass().loopTest(0);
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Why is that compiler will not complain about unreachable code?

System.out.println(j);//for this line

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

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

Post by admin »

Because compiler doesn't know that it is unreachable. Compiler doesn't compute values of variables. JVM does.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Hi,
Is there a particular technique for articulating loops on paper? A best practice, or something like "UML" suitable for working out loops quickly and accurately by hand?
Thanks

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

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

Post by admin »

Unfortunately there is no special technique other than to write down the value of each variable as you execute each iteration of the loop.
Sorry :(
Paul.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Ok, that's actually quite helpful information. Thank you.

Sai Divya sree
Posts: 14
Joined: Mon Jun 20, 2016 11:16 pm
Contact:

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

Post by Sai Divya sree »

Code: Select all

public class TestClass{
   public void loopTest(int x){
      loop: for (int i = 1; i < 5; i++){
         for (int j = 1; j < 5; j++){
            System.out.println(i);
            if (x == 0) {  continue loop;  }
            System.out.println(j);
         }
      }
   }
}

In this program the statement System.out.println(j); is not reachable?so the code shouldn't compile?Am I wrong?I have read in the earlier reply that compiler doesn't know that its not reachable.I just dint get that.Please if you could help..

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

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

Post by admin »

Think about it this way. How do "you" know that it is unreachable? You know it because you know that x is 0 and x==0 will be false. You have executed the code in your mind and made that determination.

But the compiler doesn't know that x will be 0 at runtime. It cannot execute code. It can only make use of the information that it is 100% sure about at compile time. This means if you have a final variable, then the compiler can make use of that fact because it knows that its value will be the same at run time also. But a normal variable's value can be different.

You may go through this interesting discussion: http://stackoverflow.com/questions/3795 ... iler-error
If you like our products and services, please help us by posting your review here.

6EczPZZNmQ3p
Posts: 1
Joined: Sat Apr 23, 2016 3:44 pm
Contact:

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

Post by 6EczPZZNmQ3p »

You said "you know that x is 0 and x==0 will be false"? Isn't this a false statement, pun intended? Did you perhaps mean to say "x is 0 and x==0 will be true"?

Run the following code:
int x;
x = 0;
if(x == 0)
{
System.out.println("x == 0 evaluates to true");
}
else
{
System.out.println("x == 0 evaluates to false");
}

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

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

Post by admin »

Yes, sorry, I meant x==0 is true.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

I tried to run it by adding package and main method like this:

Code: Select all

package happeningPackage;

public class TestClass {

   public static void main(String[] args) {}
   public void loopTest(int x){
      loop: for (int i = 1; i < 5; i++){
         for (int j = 1; j < 5; j++){
            System.out.println(i);
            if (x == 0) {  continue loop;  }
            System.out.println(j);
         }
      }}}
I'm not getting any output. What is wrong with the code?
(I'm a newbie. Studied and then started with mocks but new to practical coding. Please help!)
(When I tried to include loopTest within the main method, there were other errors, like: loopTest cannot have void return type.) So, how could I change the code?

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

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

Post by admin »

Please post exact error message that you get. Screenshot would be even better.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

Here is the snap when I ran it.
(Hope its not too difficult to read. Had to reduce resolution and compress it to keep it within the limit:256kb)
Attachments
Screen Shot 2018-03-10 at 7.23.08 PM.zip
(249.13 KiB) Downloaded 537 times

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

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

Post by admin »

I don't see any meaningful information in the Eclipse screen shot that you've posted.
As I mentioned to you before in another thread, you need to use the command line to compile and run the code. So please try compiling it on command line and post the exact error message that you get.

If you are a beginner, you should simply avoid using any IDE at this stage.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

Yeah. I'll do that, although I have managed to run many other programs on eclipse. I couldn't figure as to why this wasn't giving any output :|

Thank you, for all the guidance though! :)

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

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

Post by admin »

Well, from the code that you've posted the main method is empty. I don't see loopTest method being called. So no output.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

Oh right. Thank you.. :)

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Why is that the compiler does not complain about the System.out.println(j); being unreachable?

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

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

Post by admin »

OCAJO1 wrote:
Tue Nov 27, 2018 7:56 pm
Why is that the compiler does not complain about the System.out.println(j); being unreachable?
Please go through this thread from the beginning. It is explained in detail.
If you like our products and services, please help us by posting your review here.

juliusacar
Posts: 2
Joined: Fri Dec 21, 2018 4:01 am
Contact:

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

Post by juliusacar »

When x is 0, the statement continue loop; is executed. Note that loop: is for the outer loop.

Can anyone explain this to me:

loop: for (int i = 1; i < 5; i++){

I'm used to simply seeing nested for loops.

But this one have "loop:" before it. How does it work? thank you.

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

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

Post by admin »

You can apply a label to any statement. Since a for statement is just like any other statement, you can apply a label to a for statement as well. That is exactly what loop: is in this case.

You can read more about labels here: https://howtodoinjava.com/java/basics/l ... s-in-java/

Labels are use mostly in conjunction with break and continue statements. You can read more about it here: https://www.journaldev.com/980/java-bre ... ment-label
If you like our products and services, please help us by posting your review here.

vilasa
Posts: 7
Joined: Tue Apr 16, 2019 5:40 pm
Contact:

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

Post by vilasa »

admin wrote:
Mon Jul 04, 2016 1:25 am
Think about it this way. How do "you" know that it is unreachable? You know it because you know that x is 0 and x==0 will be false. You have executed the code in your mind and made that determination.

But the compiler doesn't know that x will be 0 at runtime. It cannot execute code. It can only make use of the information that it is 100% sure about at compile time. This means if you have a final variable, then the compiler can make use of that fact because it knows that its value will be the same at run time also. But a normal variable's value can be different.

You may go through this interesting discussion: http://stackoverflow.com/questions/3795 ... iler-error
So I tried the code by making x final. It still doesn't give unreachable code error.Below is my code

public class TestClass6{
final int x=0;
public void loopTest(){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}
}

public static void main(String args[]){
new TestClass6().loopTest();
}

}

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

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

Post by admin »

Please go through the SOF link given in the post that you've quoted. if statement is an exception. Java language allows an unreachable if block to support conditional compilation.
If you like our products and services, please help us by posting your review here.

vilasa
Posts: 7
Joined: Tue Apr 16, 2019 5:40 pm
Contact:

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

Post by vilasa »

Thank you . Yeah now I remember from other post of your's.if statement is an exception. I understand now. So for infinite loop case if there is statement after the loop ,it is unreachable . But for if condition it is exception. Is my understanding right?

Post Reply

Who is online

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