aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assert Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assert Question" Watch "Assert Question" New topic
Author

Assert Question

Ruben Lunda
Greenhorn

Joined: Aug 27, 2004
Posts: 5
I have this code:



What cause compilation to fail?

A) Line 13
B) Line 14
C) Line 18
D) Line 20.


The correct answer is (D) . Why???
Donny Nadolny
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
My compiler produces an error on all 4 of the options.


- Donny Nadolny<br />The pen is mightier than the sword, and considerably easier to write with.
Keith Tingle
Greenhorn

Joined: Jan 12, 2004
Posts: 22
Unless you inform the java compiler that you want 1.4 source compatabilty (-source 1.4) it will not consider 'assert' a keyword.

The only error I get on this code is 'unreachable code' for the the final assert.

IBM JDK 1.4.2.


SCJP - 8/31/2004
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I have this code


Please always quote where you found the code. That is what book, author, or mock exam.

Did you actually complie the code? What happened?

Thankyou
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ruben Lunda
Greenhorn

Joined: Aug 27, 2004
Posts: 5
Thanks Keith,
I did not see the error!
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Then why does'nt line 14 give the same error..
after assert false in line 13..line 14 will never be reached!!


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

Then why does'nt line 14 give the same error..
after assert false in line 13..line 14 will never be reached!!



Because assertions can be disabled at runtime (probably).
K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
I think the problem is with while loop because the error given is
unreachable statement.But why


SCJWSD, SCBCD,SCWCD, SCJP
If I am destined to fail, then I do have a purpose in my life: To fail my destiny.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by K Gupta:
I think the problem is with while loop because the error given is
unreachable statement.But why


It's because the while(true) loop cannot be exited. There is no break statement to get out of that loop. So any instruction after that loop cannot be reached. The compiler can detect that problem.
[ September 01, 2004: Message edited by: Barry Gaunt ]
K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
But if i compile following code
class A{
public void f(){
while(true){
}
}
it compiles!!
Then what is the problem with assert code
K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
But if i compile following code
class A{
public void f(){
while(true){
}
}
}
it compiles!!
Then what is the problem with assert code
Arun Yadav
Greenhorn

Joined: Mar 14, 2003
Posts: 19

Originally posted by K Gupta

But if i compile following code
1.class A{
2. public void f(){
3. while(true){
4. }
5. }
6.}
it compiles!!
Then what is the problem with assert code



The above code compiles because there is nothing which compiler can see as unreachable, however if we put any statement between line 4 and line 5, wheather its assert or any statement, you will find compiler complaning unreachable statement. hope this explain why its compiling..

Arun


Arun (SCJP-1.4, IBM-141, SCWCD-1.4)
"How hard you try is rooted to how often you try" - Doug Hall"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assert Question