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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User » Fri Mar 01, 2013 2:14 pm

I can't see how question 3 would throw an ArrayIndexOutOfBoundsException using while(values<100) as values will never be more than the length of the array. Should it not be (values.length < 100).

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

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

Post by admin » Fri Mar 01, 2013 2:52 pm

The program will throw ArrayIndexOutOfBoundsException every time if all the values in the array are < 100, because it does not check the length of the array. It just keeps trying to access values.
If you like our products and services, please help us by posting your review here.

vclortho

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

Post by vclortho » Mon Mar 11, 2013 3:00 pm

Hello. I am not able to get an ArrayIndexOutOfBoundsException with the below code and several variations I have tried. Please let me know your thoughts. Thanks in advance for your help.

Code: Select all

public class TestClass {


		public static void processArray(int[] values){
	        int sum = 0;
	        int i = 0;
	        try{
	            while(values[i]<100){
	                sum = sum +values[i];
	                i++;
	            }
	        }
	        catch(Exception e){ }
	        System.out.println("sum = "+sum);
				}
		
		public static void main(String[] args) {
		int[] testArray = {6,9,14,7,8,96,678,246,15};
		processArray(testArray);
		}
}

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

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

Post by admin » Mon Mar 11, 2013 3:10 pm

You have an empty catch block. Put e.printStackTrace() in your catch block and you will see the exception.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest » Mon Mar 11, 2013 3:20 pm

Thanks for responding so quickly. I did as you said, but I'm still not seeing it. I must be missing something else.

The result of running the code is "sum = 140".

Code: Select all

public class TestClass {


		public static void processArray(int[] values){
	        int sum = 0;
	        int i = 0;
	        try{
	            while(values[i]<100){
	                sum = sum +values[i];
	                i++;
	            }
	        }
	        catch(Exception e){e.printStackTrace();}
	        System.out.println("sum = "+sum);
				}
		
		public static void main(String[] args) {
		int[] testArray = {6,9,14,7,8,96,678,246,15};
		processArray(testArray);
		}
}

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

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

Post by admin » Mon Mar 11, 2013 4:26 pm

Please see the message posted above:
admin wrote:The program will throw ArrayIndexOutOfBoundsException every time if all the values in the array are < 100, because it does not check the length of the array. It just keeps trying to access values.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest » Mon Mar 11, 2013 5:17 pm

O.K., I see. You were pretty clear on that. Thanks!

Jellybean
Posts: 1
Joined: Sun Aug 25, 2013 4:58 am
Contact:

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

Post by Jellybean » Sun Aug 25, 2013 5:02 am

The text "sums all the integers that are less than 100" is incorrect.

If you have an array:

int[] testArray = {6,9,14,700,8,96,678,246,15};

Then the while loop will end when it gets to 700, and not carry on adding any of the other numbers less than 100.

It should say "sums all the integers that are less than 100 until an integer of 100 or more is encountered".

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

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

Post by admin » Sun Aug 25, 2013 6:30 am

Yes, but that is ok because
1. the programmer is a new java programmer -
2. the answer to the question does not depend on the the intention of the programmer with respect to how he wants to handle input that is more than 100. i.e. whether he wants to continue with the rest of the numbers or end the processing as soon as he gets a number > 100.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dscarrol
Posts: 1
Joined: Thu Feb 20, 2014 3:17 pm
Contact:

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

Post by dscarrol » Thu Feb 20, 2014 3:18 pm

A correct answer was: "Use flow control to terminate the loop."

Part of the reason given for this was: "It is considered bad practice to use exceptions to control the flow of execution."

Given that reasoning, all other options are incorrect answers (i.e. they are considered bad practice, not "best practice" as the question asked for)

:)

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

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

Post by admin » Thu Feb 20, 2014 8:41 pm

Best practices is valid here because there are multiple things involved. "Use flow control to terminate the loop." is a "best practice" and so is "Use ArrayIndexOutOfBoundsException for the catch argument and add code in the catch block to log or print the exception."

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

bluebox
Posts: 1
Joined: Fri Jul 04, 2014 4:56 am
Contact:

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

Post by bluebox » Fri Jul 04, 2014 5:01 am

I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.

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

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

Post by admin » Fri Jul 04, 2014 5:14 am

That's a fair point, Bluebox.
If you like our products and services, please help us by posting your review here.

FruityPebbles
Posts: 3
Joined: Fri Oct 03, 2014 2:29 pm
Contact:

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

Post by FruityPebbles » Mon Oct 06, 2014 9:21 am

bluebox wrote:I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.
I agree.

prasanna_ls
Posts: 7
Joined: Wed Nov 11, 2015 11:59 pm
Contact:

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

Post by prasanna_ls » Wed Nov 25, 2015 10:10 am

Since, 5th option is correct, and let's say the programmer does use flow control. That would mean that he would replace the while loop with a for loop(or a while loop which acts similar to a for loop). Since he is taking the extra measure to make sure that the code doesn't go outside the range of the array, why would he want to change the catch argument to ArrayIndexOutOfBoundsException? That's never going to happen given a code like this -

Code: Select all

for(int i = 0; i < arr.length; i++)
    if(arr[i] < 100)
        sum += arr[i];
That's never going to throw an ArrayIndexOutOfBoundsException since we're using flow control. So wouldn't option 3("Add code in catch block to handle the exception") be more appropriate? That code could, for example, throw a NullPointerException.

yrelhan
Posts: 5
Joined: Wed Jun 07, 2017 3:13 am
Contact:

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

Post by yrelhan » Thu Jun 15, 2017 2:22 pm

Why is the first option not correct? It is one of the things that could be done independently.

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

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

Post by admin » Thu Jun 15, 2017 9:49 pm

Because between option 1 and 2, option 2 is better and you have to select 2 correct options only.
If you like our products and services, please help us by posting your review here.

philippe
Posts: 5
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

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

Post by philippe » Mon Jul 31, 2017 3:54 pm

bluebox wrote:I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.
I Think bluebox makes a good point here. It's a good practice to let a user know about exceptions that occur, at least when these exceptions are used for what they were invented for. In this case, exceptions are (mis)used to stop the iteration over the int array. Telling the user of the processArray method that the iteration had stopped would be undesirable for that user.

redvee78
Posts: 2
Joined: Wed Jul 12, 2017 2:38 pm
Contact:

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

Post by redvee78 » Tue Aug 01, 2017 8:07 am

bluebox wrote:I don't agree that putting code in the catch block is best practice in this example - using an exception to exit from the loop is bad practice, but if you are going to do this then the exception is expected to occur (it will occur every time you run the code!) so it isn't something that you should show to the user. I agree that in general you shouldn't have empty catch blocks, but this is a strange example and I think the code would be better with just an empty block.
This is the correct answer in my opinion. There is no reason at all to log the exception because it is 100% expected, and the code does the right thing (albeit we all can agree it's a poor use of loops/exceptions).

There is nothing to be logged here - you may as well be logging "sum calculated correctly" as logging the exception.

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

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

Post by admin » Tue Aug 01, 2017 8:31 pm

The question has been updated considering the feedback above.

thanks for the feedback!
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind » Thu Jan 10, 2019 4:42 pm

Which one is the answer? I mean Option 2 is definitely a best practice. It is kind confusing to compare one best practice with another best practice. I don't get this one. Does real exam has this kinda of question? Thanks.

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

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

Post by admin » Thu Jan 10, 2019 11:55 pm

The given answer is correct. Yes, it is possible to get such question in the exam where you have to select best option. Please go through the given explanations carefully.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest