Study Guide OCA - Assessment Test #14

If house.getChickens().size() = 0, then the for loop is never executed, resulting in no output- happily so even.
I thought the update statements aren't touched until after the body executes, which it never does.

This compiles and runs without any output. What am I missing? Thank you

Thank you for your quick reply. But the book says that this will either have a runtime exception or produce at least one line of code.
That is what I don't understand. My example shows that it will successfully run and produce no output, which is in contradiction with
the answer in the book.
Thank you again, Lisa

Thank you for your quick reply. But the book says that this will either have a runtime exception or produce at least one line of code.
That is what I don't understand. My example shows that it will successfully run and produce no output, which is in contradiction with
the answer in the book.
Thank you again, Lisa

Remember that all exceptions occur at runtime. Beware of confusion with this class, one of the worst‑named classes in the standard API.

or produce at least one line of code.

You mean one line of output, not code. Please be precise with your nomenclature. If the book says, “code,” that would worry me a lot.

. . . . My example shows that it will successfully run and produce no output, which is in contradiction with the answer in the book.
Thank you again, Lisa

That either means you have changed something from what the book says, or there is a mistake in the book. Please check carefully because it is more likely that you haven't quite copied the page exactly.

Alisa Crowe wrote:I realize this has been explained, but I still have a question.

If house.getChickens().size() = 0, then the for loop is never executed, resulting in no output- happily so even.
I thought the update statements aren't touched until after the body executes, which it never does.

This compiles and runs without any output. What am I missing? Thank you

Thank you for your quick reply. But the book says that this will either have a runtime exception or produce at least one line of code.
That is what I don't understand. My example shows that it will successfully run and produce no output, which is in contradiction with
the answer in the book.
Thank you again, Lisa

condition is there, so body executed well but it is not updating rather it is the value of i is updated and assigned to calling method get(),as formal parameter, I am not sure that you are not adding values to arraylist,before for loop, may be get(), does add values but it should be before condition of the for loop, so that it check for condition in for loop, and define the variable i outside of for loop that you can write your code before condition of for loop if you are use commend line argument its OK, I am not sure get() will return the updated value because it is i has postfix increment operator and you are assigning to just variable j, not to list

Campbell Ritchie

Marshal

Posts: 64171

215

posted 3 weeks ago

sohail hussain wrote:. . . condition is there, so body executed well but it is not updating rather it is the value of i is updated and assigned to calling method get(),as formal parameter, I am not sure that you are not adding values to arraylist,before for loop, may be get(), does add values but it should be before condition of the for loop, so that it check for condition in for loop, and define the variable i outside of for loop that you can write your code before condition of for loop if you are use commend line argument its OK, I am not sure get() will return the updated value because it is i has postfix increment operator and you are assigning to just variable j, not to list

Please don't quote the whole of a previous thread; that adds nothing to the discussion.

I don't understand what you are saying there; it appears confused, but I don't think it is correct. Let's have another look at it:-

. . . condition is there, so body executed well . . .

That isn't correct. Because the List is empty, the body of the loop isn't entered nor executed, nor is thei++ part of the increment part reached.

the value of i is updated and assigned to calling method get(),as formal parameter,

No, i is not updated. It is not assigned as a formal parameter; if anything happened, the value of i would be passed as an argument. Please be careful with the nomenclature.

may be get(), does add values

No, the get() method does not add anything to a List.

define the variable i outside of for loop

That is usually not necessary; it is actually usually better to declare the loop variable as part of the loop; it will go out of scope sooner.

if you are use commend line argument its OK

What does that mean? What difference would a command line argument make?

I am not sure get() will return the updated value

If you managed to get into the increment part of the loop, you wouldn't get a value from the List; in fact it would throw an exception for trying to get a non‑existent value from a List.

. . . you are assigning to just variable j, not to list

Yes, that part is correct

sohail hussain

Ranch Hand

Posts: 40

1

posted 3 weeks ago

I compiled it, I got nothing output because an empty array list is created which is fine, and a variable j is declared, and in for loop we are checking the variable i is less than list size(), which is false so come out of the loop and display nothing, is that OK to read the code like this and according to the code output is different, may be wrong question

I've got no option but to sell you all for scientific experiments. Or a tiny ad: