Sign in to follow this  

[java] CS AP Practice Problem (New Problem)

This topic is 4242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think I'm going to have a series of questions in this area, and I don't want to completely fill the board with closely-related topics... See latest post (relatively) for question OLD POST: I'm doing some last-minute reviewing for the Computer Science AB AP Test tomorrow, and I'm somewhat confused (although it may be due to my lack of consciousness): I'm on problem #3 on this curriculum set, though I had to register to get there, so I don't know if y'all will be able to get there. Anyways, the problem has a simple ArrayList (set by "add()"-ing), and you iterate through that list and remove all Integer values of zero (Integer(0)). I, however, do not understand what is happening (my logic is flawed right now): Basic code for the question: The supplied input is [0 0 4 2 5 0 3 0], and the most logical choice is answer B, [4 2 5 3], but it's not correct. The correct answer they give is C, [0 4 2 5 3]. I've looked through the code and basic documentation for ArrayList.add(), and I'm not finding anything profound. Here's the code I'm compiling:
  ArrayList num=new ArrayList();
  num.add(new Integer(0)); new Integer(1); num.add(new Integer(0));
  num.add(new Integer(4)); num.add(new Integer(2));
  num.add(new Integer(5)); num.add(new Integer(0));
  num.add(new Integer(3));
  num.add(new Integer(0));
  System.out.printf("%s\n",num);
  int k=0;
  Integer zero=new Integer(0);
  while (k<num.size()) {
   //System.out.printf("[%d] = %d\n",k,num.get(k));
   if (num.get(k).equals(zero)) /*{ System.out.printf("  Removed\n");*/ num.remove(k); //}
   k++;
  }
  num.remove(0);
  System.out.printf("%s",num);



Code they're using (can't directly copy & paste because of Adobe restrictions):
private ArrayList nums;
public void numQuest() {
 int k=0;
 Integer zero=new Integer(0);
 while (k<nums.size()) {
  if (nums.get(k).equals(zero)) nums.remove(k);
 }
}

Program Output: [0, 0, 4, 2, 5, 0, 3, 0] [4, 2, 5, 3] I'm compiling with "javac [classname].java", and running with "java [classname]" (no special flags). Is my output being distorted by compiler optimizations??? *Stress'd* [Edited by - deadimp on May 1, 2006 10:32:39 PM]

Share this post


Link to post
Share on other sites
1. k = 0; list contains [0 0 4 2 5 0 3 0]

2. element 0 equals 0, so remove it.

3. now, the list contains [0 4 2 5 0 3 0]

4. increment k, so now k = 1

5. element 1 equals 4, do not remove it ...

as you can see, the second zero is skipped

Share this post


Link to post
Share on other sites
Ach! I get it now... For some reason I thought that it would magically shift everything after I was done...
And as for that extra "remove()", I had put it there for 'debug' reasons... Forgot to remove it.

Thanks!

Share this post


Link to post
Share on other sites
NEW QUESTION:
When they talk of speed and such, what all is taken into consideration???
In a question, they have two processes almost identical, which loops from 1 to k and fills "ArrayList someList" with a value of "Integer(k)".
However, one is "someList.add(new Integer(k))" (1) and the other is "someList.add(k,new Integer(k))" (2).
What they want to know is which will be faster, process 1 or 2.
The answer is that they are the same speed, but in my mind I'm thinking of how they would still have to loop somewhere in ArrayList.add(int,Object) to shift the right-sided values to the right, which would take some time, but...

Share this post


Link to post
Share on other sites
YAY. Im not the only one studying for this monster AB exam. Everybody else in my 30 person class is taking the A exam.

Can someone post some questions on O() notation? Also, a couple questions on Trees. I've already done all the questions I can find.

Share this post


Link to post
Share on other sites
[Probably a little late by now...]
Try signing up with CollegeBoard AP and looking at the CS AP Curriculum for this year. It has two 'simple' practice tests, one for A and one for AB. Includes some free response questions also...

Share this post


Link to post
Share on other sites

This topic is 4242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this