File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Data Field and Method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Data Field and Method question" Watch "Data Field and Method question" New topic

Data Field and Method question

Daniel Lucas
Ranch Hand

Joined: Apr 24, 2006
Posts: 48
I'm having difficulty understanding the reasoning behind the answer to this AP Computer Science A multiple choice question. Here is the question:

I know the answer is E. But why? I asked my Java teacher, but she couldn't get the right answer nor explain to me why the correct answer is correct.
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

It has to do with this section of code...

Notice that the variable k get incremented regardless of whether the condition is sucessful or not. This means that when a number get removed from the nums list, one number will get skipped. So... all the numbers after a zero is encountered will not be checked -- including another zero.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
wow, nicely spotted!
Daniel Lucas
Ranch Hand

Joined: Apr 24, 2006
Posts: 48
ah I see. The incrementing business is the hardest part for me to understand.

OK so let me talk through this and see if I fully understand (correct me plz if I make mistakes):

So we start out with an ArrayList method declared private and named nums. Making it private means that this method is not accessible outside the class... I think.

We know, although it's not stated in this code(it's in a comment), that the nums.size method (am I right in saying method here? I'm assuming everything that has an opening and closed parenthese after it with nothing in it is a method) is always going to be larger than 0. We also know that nums contains all Integer objects.

We now have a public method called numQuest(what is the purpose of making it void? Why isn't it public static void numQuest()? I've never really understood the purpose of static or void, and using them together). We declare k to equal 0, and we declare zero as an integer equal to 0(that's why it's called zero! hehe...).

Now we have a while loop to test for when k is 0 or less (nums.size() is more than 0). If k actually equals 0, then we're going to remove that number. Then we're going to increment k(add one to it? I'm kinda iffy here about how this skips the number after zero in the ArrayList? I'm guessing that's just a behavior of the ArrayList in this case?)

While typing the question out above I think I have thought of the answer. If k equals zero, it will remove that zero. Then it will add 1 to k(0), making k equal to 1 for the next ArrayList number. Since k = 1 even though you see a 0, it will just go on to the next ArrayList number looking for a 0. That is why it skips a 0 after it finds a 0. On the other hand, a trick the collegeboard could use here(if I'm understanding this correctly), is to have an ArrayList of [0, -1, 3, 4, ...] In this case, it would skip the 0 and the -1 because adding 1 to -1 makes a 0. The answer would be [3, 4, ...]

Again, I'm a high school senior in AP Computer Science A reviewing for the AP test. Unfortunately, my teacher doesn't know Java well enough to answer this question and explain it to me. Thankfully I researched up on-line (yay for google!) and stumbled upon these forums, and I think I'll be discussing my Java problems more from now on Thanks guys!

P.S. Even if you guys don't read all of this cause you're lazy (like I am oftentimes), at least writing it out helped me think through the problem in my brain. I definitely understand it a heck of a lot better now than I did just eyeballing the question. Unfortunately, on the AP test I'll have lots and lots of questions like this to do in a timed environment. I don't expect to pass the AP test (no one my teacher taught last year got over a 2, and most got ones), but who knows maybe I'll be the first. I am the first student she's had to take the AP Comp Sci A class without taking the prerequisite Visual BASIC and C++ course (I took it over the summer on my own by answering the questions at the end of the chapters and doing all the programs -- for the first 6 chapters in the Visual BASIC book and C++ book, which is all she covered in her class. And yes I know that's pathetic when the book is like 12-15 chapters for both BASIC and C++!).
I agree. Here's the link:
subject: Data Field and Method question
It's not a secret anymore!