APExam PREP
APExam PREP
Exam Overview
No penalty for guessing!
Even though not explicitly listed, Strings are very popular on the Free Response part of
the AP exams. Know the string methods listed on the reference sheet really well!
Java Reference Sheet
or here:
https://apcentral.collegeboard.org/pdf/ap-computer-science-a-java-quick-reference.pdf
General Scoring
MCQ Score: x / 40
FRQ Score: y / 40
Total Score: (x + y) / 80
AP Score:
5 x + y >= 62 (at least 78%)
4 x + y >= 47 (at least 59%)
3 x + y >= 37 (at least 46%)
Multiple Choice
Goal: Everyone should be able to get at least 20 out of 40 questions
correct.
During the first pass through the exam, only spend time on “easy” problems:
problems are short and succinct and you know you can get right if you are
careful. SKIP hard or wordy questions that ask about sorting algorithms(merge,
selection, insertion) or binary search.
During the second pass, spend time on the “medium” questions; questions that
you are confident that you can get right but might need a bit more time.
Note: For some, strategically it might make sense to entirely skip 4-8 of the
hardest MC questions and instead spend more time on the other questions.
Guess the answers to these skipped questions at the end.
Free Response
General Strategies for FRQS:
1) Read the questions before you read the code! This will help you focus.
2) Unless a question specifically addresses efficiency, focus on clear, simple
code rather than complicated, efficient code.
3) Comments are not necessary. Use only to organize your thoughts.
4) Pay attention to the code they give you:
• Do you have more than one class? Use it!
• Did they give you methods? You will need them!
• Are there instance variables that you should use? If so, use them. Do not re-declare.
• Only use accessor and mutator methods to access private instance variables.
• Do not re-write or change method headers.
• Use parameters (names and types) as given
Free Response
General Strategies for FRQS:
5) If part (b) references a method written in part (a), you will likely need to
use this method. If it is not clear to you how, stop and think before you write.
6) Pay attention to the return type. If it is not void, be sure to include a return
statement!
7) If a method comment says /* Implementation not shown */. You do not to
implement the method. But you'll likely need to use the method in your code
at least once.
Free Response
General Strategies for FRQS:
8) Write down some code for every question! Leave Nothing Blank! Get partial credit
if not full credit!
• does it need a loop?
• an if statement?
• return statement or assignment statement?
Example 1:
public double funMethod(){
double result;
…
return result;
}
More Examples
Leave Nothing Blank! Get partial credit if not full credit!
public int[] funMethod(int length){
int[] list = new int[length];
…
return list;
}
public ArrayList<Bug> funMethod(){
ArrayList<Bug> buggies = new ArrayList<Bug>();
…
return buggies;
}
More Examples
Leave Nothing Blank! Get partial credit if not full credit!
public boolean funMethod(int something){
…
if(something > somethingElse)
return true;
// must have a default return; every return can’t be
// locked in an if
return false;
}
Question 1: Methods and Control Structures
See previous years' similar FRQs:
2018 #1, 2019 #1, 2017 #3, 2018 #2, 2021#1
Methods and Control Structures
Question 1 of the FRQ will likely ask you to write methods that utilize control
structures(for vs while loops, conditionals).
You are given (usually static) methods with parameters and will be asked to
write a method that calls one of the given methods in its implementation.
Likely your method will return some value. See its return type in its header.
Using Strings and its methods can be required here in this question(or
Question 2). See for example 2017 #3 for a String question that fits "Methods
and Control Structures" question.
You should know how to use substring(int beg, int end), substring(int
beg), indexOf, equals, length and compareTo methods. See the
College Board reference sheet.
String methods
Method name Description
String(String str) Constructs a new String object that represents the
same sequence of characters as str
int length() Returns number of characters in this string
substring(index1, index2) Returns the characters in this string from index1
or (inclusive) to index2 (exclusive);
substring(index1)
if index2 is omitted, grabs till end of string
boolean equals(String Returns true if this is equal to other; returns false
other) otherwise
int compareTo(String Returns a value < 0 if this is less than other; returns
other) zero if this is equal to other; returns a value > 0 if
this is greater than other
indexOf(str) Returns index where the start of the given string
appears in this string (-1 if not found)
String method examples
// index 0123456789012345678
String s1 = ”programming in java";
System.out.println(s1.length());
// 19
System.out.println(s1.indexOf(“i")); // 8
System.out.println(s1.indexOf(“gram")); // 3
System.out.println(s1.indexOf(“hi")); // -1
Answer: aia
Do Problem 2018 #2
Complete 2018 #2.
This is a good problem that uses 1D array, Arraylist, and Strings and is a good
Question 1(Methods/Control Structures) type of problem.
Also do the following 2018 #1, 2019 #1, 2017 #3, 2021 #1 to prepare for
Question 1.
Question 2: Writing Classes
See previous years' similar FRQs:
2019 #2, 2021 #2, 2016 #1, 2015 #2, 2021#2
Question 2
This question will ask you to write an entire class including private instance
variables, constructors and methods.
The Point class' implementation on the next slide provides an example of the
anatomy of a class.
An Implementation of Point
instance variables
public class Point {
private int x;
private int y; constructors to initialize instance
variables.
public Point(int newX, int newY){
x = newX;
y = newY;
mutator method
}
public void translate(int dx, int dy){
x += dx;
y += dy; accessor method
}
public double distanceToOrigin(){
return Math.sqrt(x * x + y * y);
}
}
Typically, the question will give you examples of method calls and
corresponding outputs and require you to create a class that satisfies
those specifications. 2019 # 2 is a great example of this.
explains you how to
write your constructor!
In this problem, you are given a SingleTable class with accessors and mutators
methods and you are asked to write the CombinedTable class that uses the
SingleTable class.
Try this problem. Here’s a hint: A subtle error to this problem is not using
SingleTable instance variables! If you use int and double instance variables, your
answer will not receive full credit because of:
Question 2
Thus, the take away is that if the problem gives you a class(SingleTable) and
requires you to use it another class(CombinedTable), it is likely that you will
need to make at least one instance variable(for example, SingleTable t1) in the
class that you are writing(CombinedTable).
Please do the following 2019 #2, 2021 #2, 2020 #2, 2016 #1, 2015 #2 and
check your solutions to prepare for Question 2.
Question 3: Arraylist/1D Array
See previous years' similar FRQs:
2010 #1, 2013 #1, 2017 #1, 2018 #2, 2021#3
ArrayList/1D Arrays
We will first prepare for the FRQs by reviewing ArrayList and 1D arrays. This
will be Question 3 on the Free Response.
From previous years' exams, it is likely that Question 3 is an ArrayList
question instead of/in addition to an 1D array question.(ArrayList or both,
unlikely to be just a 1D array question)
See the following problems 2010 #1, 2013 #1, 2017 #1, 2018 #2
It's very common to have a class(e.g Student) and another class which
contains either a 1D array of Student objects or an ArrayList of Student
objects.
public class Student {
…
public String getName(){..}
public double getGpa(){…}
}
ArrayList/1D Arrays
public class Course {
private ArrayList<Student> students;
public Course() {
students = new ArrayList<Student>();
...
}
}
OR
public class Course {
private Student[] students;
}
}
Traversing 1D Array vs. ArrayList
Arrays:
double sum = 0;
for(int i = 0; i < students.length; i++){
sum += students[i].getGpa();
}
ArrayLists:
double sum = 0;
for(int i = 0; i < students.size(); i++){
sum += students.get(i).getGpa();
}
Traversing 1D Array vs. ArrayList
Use for each loops if you are only traversing the arraylist and not removing
items from it.
Remember for Arraylists that if you remove an item, items to the right of it will
shift left!
And if you add an item to the list, items will shift right to accommodate!
Make sure to take this into account when you are adding or removing items.
ArrayList/1D Array Tips
When using the ArrayList method remove(), remember you can only REMOVE
BY INDEX (not by object), so you must use a traditional for loop (and NOT a
for each loop).
for(int i=0; i<list.size(); i++){
if(list.get(i).getScore() <= 50)
list.remove(i);
i--;//AND don’t forget to decrement the index!
}
//OR traverse the list backwards, i.e.
for(int i=list.size()-1, i>=0, i--){
if(list.get(i).getScore() <= 50)
list.remove(i--);
}
ArrayList/1D Array Tips
Avoid array and ArrayList out-of-bounds exceptions! Especially when
comparing consecutive elements.
Example :You need to find the Dog with the most fleas.
//returns the Dog object in pack with the most fleas
public Dog mostFleas(ArrayList<Dog> pack){
Dog most = pack.get(0);
for(Dog dog : pack)
if(dog.getNumFleas() > most.getNumFleas())
most = dog;
return most;
}
Manipulating an Array using Arraylist
An ArrayList can be helpful when you are trying to manipulate an array (i.e.
remove items, rearrange in random order, etc.).
Example:
If you need to remove several objects from an array based one some condition,
a strategy would be to save all the objects to an ArrayList and remove the
objects before storing the remaining objects back to the array.
Please do the following 2010 #1, 2013 #1, 2017 #1, 2018 #2, 2021 #3 and
check your solutions to prepare for Question 3.
Question 4: 2D Array
See previous years' similar FRQs:
2019 #4, 2018 #4, 2017 #4, 2016 #3, 2021#4
Row Major Order Traversal
Example : Count the number of Students objects whose GPA is above a
threshold. (row major order)
public class Course{
Student[][] students;
// constructors not shown
public int aboveThreshold(double threshold){
int count = 0;
for(int row = 0; row < students.length; row++){
for(int col = 0; col < students[0].length; col++){
if(students[row][col].getGpa() > threshold)
count++;
}}
return count;
}
Column Major Order Traversal
Example : Count the number of Students objects whose GPA is above a
threshold. (column major order)
public class Course{
Student[][] students;
// constructors not shown
public int aboveThreshold(double threshold){
int count = 0;
for(int col = 0; col < students[0].length; col++){
for(int row = 0; row < students.length; row++){
if(students[row][col].getGpa() > threshold)
count++;
}
return count;
}
Along One Row
Example : Count the number of Students objects whose GPA is above a
threshold in the given row.
public class Course{
Student[][] students;
// constructors not shown
Please do the following 2019 #4, 2018 #4, 2017 #4, 2016 #3, 2021 #4 and
check your solutions to prepare for Question 4.