• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing arrays

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, I'm having troubles with this question from Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 10841-10848). Enthuware. Kindle Edition about accessing array of size 0

Which code fragments will print the last argument given on the command line to the standard output, and exit without any output or exception stack trace if no arguments are given?
1.
2.
3.
4.
5.
Select 3 options A. 1
B. 2
C. 3
D. 4
E. 5

Right, so I guessed a and c but I couldn't guess the other on which apparently is B and I don't quite understand how that's possible. For example, say that you pass 0 arguments to B. What happens is that this statement then becomes ; --> and that surely throws an exception. Doesn't it?





 
Henry Wong
author
Marshal
Pie
Posts: 22122
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Did you learn about the try-catch statements yet?

Henry
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote: and that surely throws an exception. Doesn't it?

yes of course, it throws the exception-ArrayIndexOutOfBoundsException and when it propogates to main method,you handled it their by swallowing the exception(you did nothing inside body of catch block),so manually you stop the propogation on stack and in result JVM will exit without producing any output or any stack trace for given exception.

Hope this will help!
Kind Regards,
Praveen.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, I misread the question, sorry, I thought it said "without throwing an exception" rather than without stack trace, so yes, now I understand

Did you learn about the try-catch statements yet?
I did, yes, I just didn't read the question correctly, my bad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic