• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sybex CSG 11: Line numbers and code fragments...

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 Hi Coderanchers.

I am currently learning for my 1Z0-819 or 1Z0-817 (still unsure which one to take, I hold an SCJP-6)

When taking the review questions for NIO.2 today I encountered some possible inconsistencies, which I am not able
to clarifiy for myself:

The questions that really confuse me are the second, third and sixth (pages 1014 and 1015):

The second question ist just a code fragment of a method:

The answer: The code does not compile because of the checked exception by Files.deleteIfExists(path)

The third question contains line numbers...

A given choice to answer is (B) that the code will not compile beacuse of the line 5. But in the answers in the appendix that choice is not valid.
So I assume because that code fragment has line numbers the exception could be declared or captured on another line?


When reading the code fragment of the sixth question I got even more confused:


The second line would not compile since Files.walk(..) throws an IOException and needs to be handled with...
But again (like in Question 3) that is not an option in the possible answers. Is it because its a try-with-resources statement?


In Summary:

  Code fragments WITHOUT line numbers...
  Do I just have to look for checked Exceptions if there is a whole method which has a missing throws declaration?

  Code fragments WITH line numbers...
  Can I assume that Exceptions are dealt with when ther are missing line numbers?


And what happens in the real exam? Are there code fragments with numbers and without?


Hope you guys can help me :-)

Cheers, Smon

















 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way that the 6th question compiles if there is an unseen catch block that would deal with the IOException or if the method containing that fragment declares itself to throw IOException.  The fact that it is a try with resources statement doesn't explicitly help us or hurt us in this regard, I think...

The answer to the second question is fine because there is no place they could have done those things "off-stage", as the whole method is shown right there, so it is definitely bad.

On the third question, I think you nailed it:
So I assume because that code fragment has line numbers the exception could be declared or captured on another line?

Or, more precisely because it has line numbers that make it clear there are definitely other lines before what is shown that could contain a throws clause.
If the method declaration may have a throws clause (because we don't see it), or there may be a catch block (because we don't see either the end of the method or any code that comes after the last catch block or finally), we don't know if it is bad.

These seem to be somewhat trying cases (UGH!) but the rules I'd memorized for both real and mock exams:

If there are line numbers provided for fragments, and they begin at 1, you are to presume there are no lines above.
If there are line numbers provided for fragments, and they begin at some value higher than 1, you are to presume there are some other lines above, likely not just comments or whitespace.

Most of the places I remember seeing this concern import statements or package statements, and want to test that you know that the source begins with 0 or 1 package statement, 0 or more import statements, and then the definitions (not including whitespace and comments).

I'd add a third obvious one here, if you see the end of the method in the code fragment, there is no code after the code fragment in the method, if you don't there may be some more code there...
I think the three rules I'd memorized are generally good, but don't make fully clear what to presume in absence of any line numbers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic