AP Computer Science Exam: concepts and strategies to study

Jan 28, 2021 - 15 min read
Educative
editor-page-cover

AP CS (Advanced Placement Computer Science) is a group of courses and exams about computer science. High school students who take AP courses can earn college-level credit and be more prepared for college learning. AP Computer Science A (AP CS A) is an introductory college course taught in Java that helps you prepare for the AP exams.

This exam can be stressful since there is so much to study. That’s why today, we will walk you through the main concepts you need to know. This article is intended as a primer for taking an AP CS course. We’ll explain the main concepts and introduce some essential test-taking strategies to guide your learning.

This guide at a glance:


Your one stop shop to acing your AP Computer Science A exam

Learn the fundamentals of programming and get hands-on practice coding in Java. You’ll get access to 3 practice exams inside your browser.

Ace AP Computer Science A


Overview of the AP CS A course and exam

AP Computer Science A is an introductory, college-level CS course for high school students who want to earn college credit and learn Java programming before entering college. The course is essentially an equivalent to a first-semester college CS course.

AP students cover the basics of object-oriented programming and focus on Java algorithm development, data structures, abstraction, and programming problem solving in Java. At the end of the course, students take the AP Computer Science A exam, which includes both multiple-choice and free-response questions on Java programming.

Note: The AP CS A course/exam is different than the AP Computer Science Principles course. AP Computer Science Principles focuses on broader aspects of programming and how programmers build for the world.


Parts of the AP exam

The Multiple Choice section includes 40 questions that equal 50% of your final score. You have 1 Hour and 30 Minutes for this section. Multiple-choice questions assess Computational Thinking Practices 1, 2, 4, and 5.

The Free Response section includes 4 Questions that equal 50% of your final score. You also have 1 Hour and 30 Minutes for these questions. All free response questions test Computational Thinking Practice 3: Code Implementation. The questions fall into 4 categories:

  • Question 1: Methods and Control Structures. You must write a program to create objects of a class and call methods.
  • Question 2: Classes. You must write a program that defines a new type using a class.
  • Question 3: Array/ArrayList. You must write a program to create and manipulate elements in 1D array or ArrayList objects.
  • Question 4: 2D Array. You must write to create and manipulate elements in 2D array objects.

What topics are on the exam?

The AP Computer Science A course covers the topics listed below. We will briefly define each and then explore them in more detail in later sections.

  • Java programming language: The AP CS A course is taught and tested in Java. Java is a general-purpose programming language. It is one of the most popular and widely used programming languages in the world.

  • Primitive Types: In programming, data types define what type of data we are using. It is how we tell a computer how a programmer intends to use the data. Primitive data types refer to the predefined data types for a language.

  • Objects and Classes: In programming, an object has state and behavior. Objects belong to a class. A class describes a type, or classifies it.

  • Boolean expressions and if Statements: Conditional statements give a program the ability to decide and respond. Boolean expressions (which represent a yes or no decision) are used to make conditional statements.

  • Iteration: In programming, iteration is the repetition of certain instructions. You can either iterate a specified number of times or until the condition is met.

  • Arrays: An array is a block of memory that can store data elements of the same type and under the same name. Think of this like a collection of related data.

  • ArrayList: ArrayLists can change their size dynamically, so you can add and remove elements whenever there is a need.

  • 2D Arrays: A 2D array is an array of arrays. Elements of the outer array stores a reference to an inner array.

  • Inheritance: Inheritance allows you to derive a class from another class, creating a hierarchy.

  • Recursion: Recursion means defining a thing in terms of itself. This is method of problem solving where we break down a problem into smaller instances of itself.


Introduction to Java

Java is a general-purpose programming language. It is one of the most popular and widely used languages around the world. Java is used in all kinds of domains. The most common uses are:

  • Mobile application development
  • Web applications
  • Desktop applications
  • Game development
  • Distributed applications

Java is a common first programming language for programmers and has many features that other popular languages do not have, such as Python, JavaScript, and C++.

To get a sense of how Java works, let’s look at its “Hello World” program. Take a look at the code and hit “Run” to see the result.

class HelloWorld {
    public static void main( String args[] ) {
        System.out.println( "Hello World!" );
    }
}
Java Hello World
  • At line 1, we add the keyword class
  • Then, we add the title of the program, which is HelloWorld.
  • Next, we add {}
  • At line 3, public static void main( String args[] ) represents the main method of this program
  • System.out.println is used to to print Hello World to the screen
  • We add {} again to wrap up the program

The main() method and class declaration

In Java, every line of code that runs must be inside a class (see line 1 above). The class name must match the filename, and it ends with the file suffix .java. So, for example, our file name could be MyClass.java.

Once the file is located with a proper name, you can compile it with the Java compiler.

Any Java program starts by executing the main() method of a class. You choose the name of the class you want to execute, but the method must always be main(). The main() method is required for every Java program (see line 2 above).

For our MyClass.java file from before, here is how it would look with the main() method:

public class MyClass {

    public static void main(String[] args) {

    }
}

Commenting

Reading Java code can be a challenge for new and expert programmers alike. To aid with this, we can add comments. This is a way of defining parts of our code, but our comments are ignored by the compiler. We can add comments in Java code in three ways:

  • Block comment /* … */: to define the function of multiple lines of code
  • Line comment //...: to describe the function of a line
  • Javadoc comment /**…*/: to describe important parts of the code
class Introduction 
{
    public static void main(String args[]) 
    {
        String name = "Matt";
        int age = 16;
        // This line prints the introduction on to screen.
        System.out.format("My name is %s and I am %d years old.%nI am learning Java.", name, age);
    }
}
Example of line comment

Primitive types

Data types define what type of data we are using. This is how you tell a computer/compiler how a you intends to use a piece of data. In Java, primitive data types refer to the predefined data types. Think of these as the building blocks of data manipulation.

There are 8 primitive data types available in Java, but the most commonly used are:

  • int for integers
  • double for decimal numbers
  • String for text
  • boolean for boolean values (a value that is either true or false)

There are some data types that are not bound by size, like String. These are called reference data types.

We use data types to create variables. A variable is the name we associate with a memory location. This allows us to access a variable just by calling its name rather than remembering where it is stored. In Java, we need three things to create (declare) a variable:

  • Type: defines the type of data being used
  • Name: defines the name so you can call it again
  • Value: defines where you want to store the data
class VariableDeclaration 
{
    public static void main(String args[]) 
    {
        // int type variable stores an integer number
        int integerVariable = 4;
        System.out.println(integerVariable);

        // double type variable stores decimal number
        double decimalVariable = 2.01;
        System.out.println(decimalVariable);

        //boolean type variable can save either true or false, nothing else
        boolean booleanVariable = false;
        System.out.println(booleanVariable);

        // String type variable saves text literals
        String textLiteralVariable = "Hello World!";
        System.out.println(textLiteralVariable);
    }
}
Variable examples in Java

When it comes to variables, there are two types of memory:

  • Stack memory
  • Heap memory

Stack memory is where we store primitive variables and the addresses of reference variables. This is used during the execution of a program. All variables stored on it must have fixed sizes.

Heap memory is for larger stacks of memory that are not fixed in size. We use this to store the data for reference data types, like String.


Object Oriented Programming (OOP)

Object-oriented programming (OOP) is a popular style, also called a paradigm, of programming. It structures a program into simple, reusable pieces of code blueprints, called classes. We use these to create individual instances of a class, called objects.

A class is an abstract blueprint. Classes represent broad categories, like Car or Dog that all share similar attributes that the objects will have, such as car type, color, or breed. They do not store the specific values of each object.

These are defined with the following syntax:

class name
{

}

If classes are the blueprints, the objects are the individual items within that class. Objects represent specific examples of the abstract class, like myCar or goldenRetriever. Each object has unique values based on the class.

These are defined with the following syntax:

className obj = new className();

Let’s put these together with an example:

class Introduction 
{
    // A class can have its own variables
    String name;

    // This constructor requires user to pass a String type variable or value
    Introduction(String enteredName)
    {
        // we can read/update the value of a class's variable in the constructor
        name = enteredName; 
    }
    
    // This class has the following method
    public void greet() 
    {
        // we can read/update the value of a class's variable in the class's methods
        System.out.format("Hello %s! This is Educative's AP CS A primer.", name );  
    }

    public static void main (String args[])
    {
        // Introduction class has only one constructor that takes String as input
        Introduction user = new Introduction("Jane Doe");
        // user is now an object of the Introduction class. We can call all the class methods on user, e.g. greet() method
        user.greet();
    }
}

Prepare for your exam the smart way

Get hands-on practice for your AP CS A exam without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments, making learning quick and efficient.

You’ll even get access to 3 tests right in your browser!

Ace AP Computer Science A


Arrays in Java

Java arrays are a big part of the AP CS A course and exam. An array is essentially a block of memory that stores a collection of data elements under one type and one name. Think of this as a collection of related data.

When we have many elements of data of the same type but we don’t need to name each one, we can use the array name and the index for the position of an item held within that array. An array is not resizable once it’s created.

widget

Let’s see how this looks in Java code. Imagine a teacher has to grade 5 papers. Instead of making 5 different variables, we can store the scores in an array.

class CreateArray
{
    public static void main(String args[]) 
    {
        int[] scores = {100, 87, 93, 76, 45};        // Creating an array
    }
}
  • int: type of the values you want in an array, which are integers
  • []: square brackets right after the type
  • scores: name of the array variable
  • {}: used to hold each data point

The square brackets [ ] are used to access or modify an element using its index. Below, we ask the program to print the first value. As you can see from our diagram above, the first element in an array is actually indexed at [0].

class AcessArray
{
    public static void main(String args[]) 
    {
        int[] marks = {100, 87, 93, 76, 45};    // Creating an array
        System.out.println(marks[0]);           // Acessing an element
    }
}

ArrayList

Since an array is not resizable, you cannot add more space to it after its made. But sometimes we need to delete or add elements later. To do so, Java provides a functionality of resizable arrays using a class called ArrayList.

Below, we create an empty ArrayList.

import java.util.ArrayList;

class CreateArrayList 
{
    public static void main(String args[]) 
    {
        ArrayList<String> students = new ArrayList<String>();
    }
}
  • We first import the functionality for this tool using the import java.util.ArrayList. This is the package that the ArrayList class is provided in.
  • Then we write the class name ArrayList
  • We add <> write the data type of our elements within it
  • Then, we write the name of the object

Note: An ArrayList only holds the object references.


2D Arrays

Two-dimensional arrays (2D arrays) are basically arrays that are build on top of other arrays, like boxes that are stacked. Each element of the outer array stores a reference to an inner array. In a 2D array, the first index is used for rows, and the second index is for columns.

Check out the Java code below to see an example of declaring a 2D array.

class Create2DArray 
{
    public static void main(String args[]) 
    {
        // Creating 2D arrays
        int[][] array1 = new int[2][4];

        int[][] array2;
        array2 = new int[3][2];
    }
}
  • int: the type of the values you want
  • [][]: double square brackets right after the type
  • array1: name of the 2D array variable
  • new int[2][4]: a 2D array is created with two rows and four columns
  • At line 8, we declare another 2D array, called array2
  • At line 9, new int[3][2] creates the 2D array with three rows and two columns

From there, we can assign values to the array like in this example below:

class ArrayWithValues
{
    public static void main(String args[]) 
    {
        // Creating 2D array with values
        int[][] array1 = {{1, 2}, {3, 4}, {5, 6}};
        System.out.println(array1[2][1]);
    }
}

Boolean expressions and if statements

Boolean expressions fall under the category of conditional statements. A conditional statement basically asks if a certain condition holds. Conditional statements express that if a Boolean expression is true, a certain action must be performed.

Conditional statements are used to control the flow of a Java program.

A boolean expression compares the primitive or reference values using relational operators, which is basically just a mathematic expression. In other words, you are asking to compare two things based on a provided condition and return a response if that things if true or not.

A boolean expression will always evaluate to either true or false, which we call boolean value. The basic syntax looks like this:

operand1 operator operand2

The data type of operand1 and operand2 must be the same. operator can be any relational operator. For example, if we set operator to ==, the expression will evaluate if operand1 and operand2 are equal. If they are, it will return true. If not, then it will return false.

We can use all kinds of operators, such as checking if things are not equal, greater than, less than, and so on.


if statements

An if statement affects the flow of control by executing different things based on the return value of a boolean expression. For example, a one-way if statement is used when there is a set of statements to execute under a particular condition. They will only execute if the boolean expression evaluates to true.

if (boolean expression) 
{
   statement(s)  // evaluated if boolean expression is true
}
statement(s)     // outside if body

Let’s see an example to understand this better. Below, we want to print the message, x is even, only if x is an even number.

class IfDemo 
{
    public static void main(String args[]) 
    {
        int x = 4;
        if (x % 2 == 0)                   // Applying if condition
        {
            System.out.println("x is even");  // executed only if condition is true  
        }    
        System.out.println("Program ended");
    }
}
  • At line 5, we declare the x variable. Since it’s an integer, we use int.
  • We use the keyword if
  • We add parentheses () for our statement.
  • Line 6 is where we put our statement x % 2 == 0. Here, if x %2 is 0, then the condition will be true.
  • Then, we print the message Program ended.
  • Line 10 will execute no matter what because it’s outside the if body.

Basics of iteration in Java

In Java, iteration means continually repeating a set of instructions. We do this using loops. There are two main types of iterations:

  • Iterate until a specific condition is met.
  • Iterate a certain number of times.

Essentially, we can ask the program to continue doing something until a condition is met, or we can just ask it to do something a certain number of times. The two types of loops you should know about are for loops and while loops.

  • The for loop runs a set of instructions for a certain number of iterations.
  • The while loop runs a set of instructions as long as a specified condition is true. So, while that condition, is true, do this thing.

Here is how we define white loops:

while (condition)
{
    statement(s)     // statements to be executed
}

Here is how we define for loops:

for (initialization; bool-expression; increment/decrement)
{
   statement(s)   // statements to be executed
}
  • The bool-expression defines the condition for executing the code block.
  • The increment/decrement statement is executed after the code block has been executed.

Test taking strategies and what to learn next

Now that we understand some of the basics of Java and what is expected on the AP CS A exam, let’s discuss some test taking strategies. When you are reading questions for the first time or preparing, keep these things in mind:

  • Look for keywords: Keywords can give you a lot of clues for solving the problem. Take a mental note of these keywords, especially methods, as you read so you know where to start looking.
  • Don’t dwell on syntax accuracy too much: Don’t waste time trying to every piece of syntax perfect. You may be unable to answer all the questions. Instead, aim for almost perfect code, and you won’t lose as many points.
  • Address every part of the question: Questions are about meeting every requirement. So you need a solution that fulfills all the requirements to some degree rather than perfectly fulfilling only one.
  • Don’t go through the questions in order: You can answer the questions in any order to play to your strengths. Answer the ones you are best at first to get some points, then delineate time for the rest.
  • Practice hands-on: It’s crucial to have hands-on practice coding, not just rote memorization. Try building projects or taking an online course.

If you want to keep your learning going, we recommend Educative’s course Ace AP Computer Science A. This is your one stop shop to acing your exam. It is a comprehensive run-through of everything you need to know to fly through your exam with confidence. You’ll also get the chance to test your knowledge with 3 practice exams inside your browser.

Happy learning!


Continue reading about Java


WRITTEN BYEducative

Join a community of more than 1.3 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.

Learn in-demand tech skills in half the time

Copyright ©2022 Educative, Inc. All rights reserved.

soc2