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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

Will option 2 not throw an exception if zero arguments are passed?

Thanks

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ml.vilen
Posts: 4
Joined: Mon Jan 13, 2014 11:01 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ml.vilen
Posts: 4
Joined: Mon Jan 13, 2014 11:01 am
Contact:

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

Post by ml.vilen »

yes as you mentioned it is a matter of perspectives.
thank you for fixing it.

Marthinus
Posts: 7
Joined: Tue Jul 22, 2014 5:07 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The question asks you to print the last argument.
If you like our products and services, please help us by posting your review here.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post 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

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

DAlley
Posts: 3
Joined: Sun Oct 19, 2014 3:55 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

deniscapeto
Posts: 6
Joined: Wed Dec 02, 2015 5:50 pm
Contact:

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

Post 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....

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 109 guests