• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Assert Question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler produces an error on all 4 of the options.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ruben Lunda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith,
I did not see the error!
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why does'nt line 14 give the same error..
after assert false in line 13..line 14 will never be reached!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is with while loop because the error given is
unreachable statement.But why
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if i compile following code
class A{
public void f(){
while(true){
}
}
}
it compiles!!
Then what is the problem with assert code
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic