Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Fri Aug 24, 2012 9:34 am
by ETS User
Will option 2 not throw an exception if zero arguments are passed?

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Fri Aug 24, 2012 10:40 am
by admin
The line

Code: Select all

System.out.println(args[args.length-1]);
will indeed throw an ArrayIndexOutOfBoundsException if no argument is passed. However, it is caught in the catch block.

Therefore, the program on the whole and the main method does not throw out any exception.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Tue Jan 28, 2014 5:05 am
by ml.vilen
But the program actually throws an exception. Yes it is then caught with catch clause but the fact doesn't change from it. The question doesn't say print exception it says "exit without any exceptions". So I guess the question formulated incorrect.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Tue Jan 28, 2014 5:52 am
by admin
No, the program doesn't throw an exception. Even the method doesn't throw an exception. When you say that an entity (whether a program or a method) throws an exception, it implies that the exception is thrown out of the entity that you are talking about i.e. the exception is seen from the caller's perspective.

Here, the question is clearly talking about the behavior of the program from command line perspective and no exception is seen from this perspective. What happens inside the program or the class is irrelevant.

Even so, the problem statement has now been updated to say, "...output or exception stack trace ..." to avoid any confusion.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Tue Jan 28, 2014 11:48 am
by ml.vilen
yes as you mentioned it is a matter of perspectives.
thank you for fixing it.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Fri Aug 08, 2014 3:19 am
by Marthinus
I don't understand why 4 is wrong. When you I run the code it doesn't throw and exception or print anything. i is 0, so i > 0 is false and thus it doesn't print anything/throw an exception.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Fri Aug 08, 2014 5:07 am
by admin
The question asks you to print the last argument.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Thu Sep 25, 2014 1:49 pm
by jbilkes
im afraid i have to discuss the fact that option 5 does not print out anything except the correct argument as asked...maybe its the Kepler Eclipse, maybe its JRE 1.8.0_20 i dont know, but as it is, the answer is simply incorrect

My guess would be its the debug perspective in the same window as the console in Eclipse. Whether i run option 2 or option 5, i dont have any print in my console view and both times i got the right exception in the debug perspective...quite confusing if you ask me becuz clearly option 2 catches it while option 5 does not...anyhow, as it is now, the question should be refrased

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Thu Sep 25, 2014 2:07 pm
by jbilkes
ok i installed newest eclipse (luna) and played again with it...to my big suprise the following happens:

running option 5 with an argument runs as it should
running option 5 without an argument (THE FIRST TIME) gives an aoobe in the console (!) and debug perspective
running option 5 without an argument thereafter shows nothing in console although the debug shows the aoobe

as i said before, when running option 2 no console print but debug of course gives aoobe...

i guess this is an Eclipse issue but nevertheless does the question rely on the IDE implementation and that should be avoided i guess

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Thu Sep 25, 2014 11:58 pm
by admin
The question does not rely on any IDE specific behaviour. You are relying on IDE specific behavior.

For the purpose of the exam, you have to use the command line way of compiling and running the program. What any IDE shows is immaterial.

On a side note, your ide is probably NOT showing you combined output of out stream and error stream that is why you are not seeing any output with option 5.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Tue Nov 04, 2014 8:04 am
by DAlley
I would think that as the question is stated that option 4 would also be correct. The question says that "no arguments" are given. Therefore arg.length - 1 would be -1. Since -1 is not greater than 0, nothing prints.

The explanation says that the if will fail if 1 argument is given, but the question already says no arguments are given.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Tue Nov 04, 2014 1:00 pm
by admin
No, the question asks that the code should print the last argument and "if" no arguments are given, then it should end without any exceptions. So there are two cases that the code should work with.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Thu Dec 10, 2015 5:32 pm
by deniscapeto
I´ve read the explanations above but I´m still confused with the option 5.

As far as I understood (the moment I was taking the test), there are two acceptable possibilities:

1- you pass a pass one or more arguments and the program must print the last one.
2 - you pass nothing and the program must throw exception.

In this case, option 5 is incorrect since it catches the Exception so the program will print nothing.

Another interpretation to this question (the correct one) could be the following possibilities:

1 - you pass a pass one or more arguments and the program must print the last one.
2 - you pass nothing and the program exits without any output or exception stack trace


now I understand but I think the question is a little confusing....

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Thu Dec 10, 2015 10:12 pm
by admin
deniscapeto wrote:I´ve read the explanations above but I´m still confused with the option 5.

As far as I understood (the moment I was taking the test), there are two acceptable possibilities:

1- you pass a pass one or more arguments and the program must print the last one.
2 - you pass nothing and the program must throw exception.

In this case, option 5 is incorrect since it catches the Exception so the program will print nothing.

Another interpretation to this question (the correct one) could be the following possibilities:

1 - you pass a pass one or more arguments and the program must print the last one.
2 - you pass nothing and the program exits without any output or exception stack trace


now I understand but I think the question is a little confusing....
I am not sure how you arrived at the first interpretation when the problem statement clearly says, " and exit without any output or exception stack trace if no arguments are given".

Request anyone reading this to please suggest an improvement so that this confusion can be avoided.

thank you,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1176 :

Posted: Mon Feb 15, 2016 4:14 pm
by NickWoodward
I personally don't see the problem with the question but maybe this will help:

Which code fragments can:
a) output the last argument, if any are given, from the command line.
and
b) when no arguments are provided, print nothing, which includes not printing any exception stack trace.

Nick