This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is the first line to cause error?

 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I can not understand the answer to this question, concerning the following code (from Whizlabs):


A line 9
B line 12
c line 16
D line 5
E line 18

my answer was A, but the simulator says D. Explanation:

Option D is correct because of the for loop at line 5, other statements become unreachable as there is no way to get out from the first loop. Option A is incorrect because it will not give you error for unreached statement since the value is being supplied from boolean variable flag. Hence no compilation error.

Thank for the help.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simulator is correct. The for-loop will run endlessly, so line 5 is unreachable code and that's a compiler error.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the compiler analyses this piece of code, it realizes that the statements following the for loop is unreachable since the for loop will be executed indefinitely, therefore the code won't compile unless you provide code to break out of the for loop or remove the statements that follow.
 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help and for your patience. I know that the first cycle is endless and all that follows line 8 is unreachable code. If I delete the code from line 8 to 18, the program compiles correctly. And that's ok.

My problem is that I do not understand why the line of code that produces the error is 5 and not 9. If I copy the program in an IDE error is signaled at line 9. If I compile the program from the command line I always get line 9 :

Train.java:9: error: unreachable statement
while(!flag)
^
1 error

Sorry but I just can not understand why it is giving error in line 5 and not 9.
There is compilation error in the code because there are pieces that will not be executed and the first line of code that is not executed is 9. What is wrong with this reasoning?

Thank you again
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on where the numbering in the Whizlabs mock question starts. If it starts (with 1) at the class declaration, then line 9 should be the correct answer. If it starts (with 1) at the declaration and initialization of boolean variable flag (which I assume based on their correct answer), then the correct answer would be line 5.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the wording of the question along with the answers?... You probably interpreted the question wrongfully.
 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your help. I really appreciate it.

This is an exact copy of the question with the same indentation and the same line numbers:

Which will be the first line to cause an error in the following code?



A. Line no. 9
B. Line no. 12
C. Line no. 16
D. Line no. 5
E. Line no. 18
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I would have also chosen A as the answer knowing how the compiler works... That's a fuzzy question
 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel relieved
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:This is an exact copy of the question with the same indentation and the same line numbers:

If that's the case, then the correct answer is obviously wrong! The correct one should be A. line 9

Based on many user experiences (like this one) I must say I'm not surprised to see such errors/mistakes in the Whizlabs mock questions.
 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel!
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the question is not clear.

They ask which is the first line that first "causes" an error. I automatically picked line 5. Surprisingly I was "right".

But it is reasonable to select the line that is flagged by the compiler as unreachable code!

Then people must wonder if the real exam writers agree with the mock exam writers…

What does the term "cause error" mean then? Is it the unreachable code that causes the error, or the code that causes it to be unreachable??

Disturbing!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Black wrote:Then people must wonder if the real exam writers agree with the mock exam writers…

What does the term "cause error" mean then? Is it the unreachable code that causes the error, or the code that causes it to be unreachable??

I agree there could be some doubt about which line produces the compiler error: the line that's unreachable or the line that causes another line to be unreachable. I think it makes sense the error is shown on the unreachable line, because that one makes the program not compile (if you remove it, the program will compile). If the compiler would give the error on the line causing another line to be unreachable you could think you have to change that particular line while it might be perfectly legal java code.
I don't agree with the part about the exam writers. Both just have to adhere to the compiler's behavior. Because the compiler gives an error on the unreachable line, the mock exam writers should have the line number of the unreachable line as correct answer (and not the line number of the line that causes another line to be unreachable or even worse: a runtime exception). If that's not the case, the mock exam is of poor quality (and should be avoided if this mistake is not fixed).
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know the right answer, so I defer to you, Roel.
So, we will agree that Whizlabs is wrong, and most likely the real exam writers would flag the unreachable line of code?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Black wrote:I don't know the right answer, so I defer to you, Roel.

Let's see what happens if we compile the following class:


Output of the console:
Train.java:9: error: unreachable statement
while (!flag) // 9
^
1 error


So obviously WhizLabs is wrong!
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interesting footnote, which I discovered while researching this topic, is that Java does allow unreachable code caused by "if" statements--I believe only when used ith variables--not just "if (FALSE)".
That would be "conditional compilation".
The reason for this is to allow flag variables for debugging.

static final boolean DEBUG = false;
if (DEBUG) { x=3; }

Of course someone might use an "if" to cause unreachable code accidentally, not for debugging purposes, and the compiler would not flag an error!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You even don't need a flag, just a statement like if (2 < 1) { x=3; } will also result in dead code (not an unreachable code compiler error).
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is interesting! Yet if I do "while (2 <1)" it causes a compile error!

I did read somewhere on stack overflow that any if statement would compile that way, but then another person said it had to be a flag. So I guess that person was wrong.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Black wrote:So I guess that person was wrong.

Yeah, I don't argue anymore with the compiler, I know after all these years he's always correct. If he gives me an error, I'll be a good boy and just change my code, otherwise I don't get the so desired class files

And what do you think of this one?


And now let's add some if-statement to the while loop. What will the result of this snippet be?
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is too much fun!
The first snippet causes an unreachable statement error at the println after the endless loop.
The second snippet DOESN'T because the "if" statement makes the compiler think there is a chance there will be a break.

But there's more!
I tried changing the "if" to a "while" in the second snippet.


But now there are TWO errors. I got a first error, unreachable statement at the the left bracket at the start of the statement after the inner "whie". And again, the error at the println after the endless loop.
 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
just for completeness, I show the response received from the Whizlabs team:

"Yes, it is true that compiler complain that lines after for loop at line can't be reached, so it is line 5, makes them unreachable, questions asks,

Which will be the FIRST LINE TO CAUSE an error in the following code?

This is bit tricky questions.

Thanks for contacting us, if you have any doubts we are here to help!"

I do not think the word "first" can change the meaning of the problem and neither will help you choose a code line rather than another. I believe that when in doubt the best logic is to leave the final word to the compiler.

Thank for the attention.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:just for completeness, I show the response received from the Whizlabs team:

"Yes, it is true that compiler complain that lines after for loop at line can't be reached, so it is line 5, makes them unreachable, questions asks,

Which will be the FIRST LINE TO CAUSE an error in the following code?

This is bit tricky questions.

Thanks for contacting us, if you have any doubts we are here to help!"

I do NOT agree with the WhizLabs explanation. The 1st line to cause an error in the following code is still line 9, not line 5! Line 5 is just legal java code (an endless for loop), that line is not causing any error at all! If lines 9 until 18 (included) would be deleted, the program would compile and run just fine (which would not be the case if line 5 had been invalid). If they don't understand they are just giving the wrong answer (and thus fixing this question), I would strongly advice against using the WhizLabs mock exams! Reason is simple: their exams clearly contain errors (and they are not eager to fix them) which might result in learning stuff the wrong way.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Black wrote:This is too much fun!

It was too much fun indeed, but for me it was time for some With a small snippet and some imagination you can have lots of fun (and in the meantime learn a lot)!
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Whizlabs stands by their answer! I would really like to hear what Oracle would say, just for the official resolution.

Would you believe I first started taking mock exams only 5 days before the exam, and mostly Whizlabs? Then I realized I was wasting too much time trying figure out what they were saying in their broken English (both questions and explanations) and finally two days before, switched to Enthuware? I think with better preparation and starting mock exams sooner, I would have gotten a much better score.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Black wrote:I would really like to hear what Oracle would say, just for the official resolution.

I'm absolutely convinced Oracle will follow the Java compiler. So if you got this question on the actual exam, the correct answer would be (without any doubt) line 9.
 
Andrea Black
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, good to know. Because I'm still hoping to take the OCPJP eventually and there are a lot of questions to the effect: "Which is the first line that causes an error?".
 
Emanuele Mazzante
Ranch Hand
Posts: 60
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Emanuele Mazzante wrote:just for completeness, I show the response received from the Whizlabs team:

"Yes, it is true that compiler complain that lines after for loop at line can't be reached, so it is line 5, makes them unreachable, questions asks,

Which will be the FIRST LINE TO CAUSE an error in the following code?

This is bit tricky questions.

Thanks for contacting us, if you have any doubts we are here to help!"

I do NOT agree with the WhizLabs explanation. The 1st line to cause an error in the following code is still line 9, not line 5! Line 5 is just legal java code (an endless for loop), that line is not causing any error at all! If lines 9 until 18 (included) would be deleted, the program would compile and run just fine (which would not be the case if line 5 had been invalid). If they don't understand they are just giving the wrong answer (and thus fixing this question), I would strongly advice against using the WhizLabs mock exams! Reason is simple: their exams clearly contain errors (and they are not eager to fix them) which might result in learning stuff the wrong way.


I totally agree with you Roel, thanks for your help. As I wrote, it is a pity that Whizlabs team doesn't want to review the test questions, I don't think it is a huge job and it's a real shame because it would become a product more than good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic