How to score a 5 for an AP computer science test

KNOW THE COMMON PITFALLS!! Like the following.

Type conversion errors – refer to 2019 FRQ #2 and 2018 FRQ #1(b). In these questions, you have things like

  1. public class SomeClass{ 
  2. public int someData; 
  3. public int someOtherData; 
  4.  
  5. /* some constructor things */ 
  6.  
  7. /** Returns the ratio between someData and someOtherData */ 
  8. public double someMethod(){ 
  9. // does something 
  10. return ___________; 

In the blank they ask for a datatype “double”. Integer division truncates, and you should ALWAYS check for types. Line 10 should be

  1. return someData / (double)someOtherData; 

or equivalent forms.

Also, another pitfall lies in ArrayList datatype’s remove(int) method that removes the object at a certain index. Like:

  1. public class MyClass{ 
  2. private ArrayList myList; 
  3. /* some constructor things */ 
  4.  
  5. /** Removes all «1»s in myList. 
  6. public void removeAllOnes(){ 
  7. // CODE HERE 
  8. _____________ 
  9. _____________ 

Remember that the “remove” method also shortens the overall length, so you CANNOT DO THINGS LIKE THIS:

  1. for(int x = 0; x < myList.size(); x++){ 
  2. if(myList.get(x) == 1){ 
  3. myList.remove(x); 

You either do this:

  1. for(int x = 0; x < myList.size(); /* NO INCREMENT HERE */){ 
  2.  
  3. if(myList.get(x) == 1){ 
  4. myList.remove(x); 
  5. } else x++; 

OR this:

  1. for(int x = myList.size() – 1; x >= 0; x–){ 
  2. // go reverse. 
  3. if(myList.get(x) == 1){ 
  4. myList.remove(x); 

EVERY YEAR PEOPLE MAKE MISTAKES ON THESE. BOTH IN MULTIPLE CHOICE AND FRQS.

BE CAREFUL ON THESE PITFALLS.