AP Computer Science A Study Guide

AP Computer Science A Study Guide Overview

  • AP Computer Science A is designed to be equivalent to a first-semester college-level computer science course.
  • The exam comprises 44 questions: 40 multiple choice (50%) and 4 free responses (50%).

Exam Content Breakdown

  • Primitive Types: 2.5%–5%
  • Using Objects: 5%–7.5%
  • Boolean Expressions and if Statements: 15%–17.5%
  • Iteration: 17.5%–22.5%
  • Writing Classes: 5%–7.5%
  • Array: 10%–15%
  • ArrayList: 2.5%–7.5%
  • 2D Array: 7.5%–10%
  • Inheritance: 5%–10%
  • Recursion: 5%–7.5%

Primitive Types

  • Data Types: Java has 8 primitive types; only three are most relevant for AP:

    • int: Integer numbers (e.g., 3, -14, 21860)
    • double: Floating point numbers (e.g., 3.14, -1.0, 48.7662)
    • boolean: Values true and false.
  • Arithmetic Operations: +, -, *, /, %.

    • Division of two integers results in an integer (e.g., 7/4=17 / 4 = 1).
  • Variable Declaration: Must declare type (e.g., int age;) and can optionally combine it with assignment (e.g., int age = 18;).

  • Final Variables: Use final keyword to denote constants (e.g., final int max = 100;).

  • Compound Operators: Shortcuts for common operations (e.g., x += 3 is equivalent to x = x + 3).

  • Casting: Use (int) or (double) for data type conversion (e.g., int myVar = (int)6.3;).

Using Objects

  • Object Creation: Objects are created using a constructor (e.g., Rectangle myRectangle = new Rectangle(5, 6);).
  • Reference Variables: Store addresses of objects; can be null.
  • Method Calls: Use dot operator (e.g., myRectangle.grow(4, 4);).

Strings

  • String literals in double quotes (e.g., "Hello").
  • Strings are immutable (no changes directly to original strings).
  • Common string methods:
    • length(): Returns the number of characters.
    • substring(int from, int to): Returns a substring.
    • equals(String other): Checks for equality.
  • Escape sequences: ", \, .

Arrays

  • Fixed size and indexed collections of values with type (e.g., int[] myArray = new int[5];).
  • Access using indices [index], starting from 0.
  • Use length to get array size. Example traversal:
  for (int i = 0; i < myArray.length; i++) {
    // Process myArray[i]
  }

ArrayList

  • Dynamic arrays, size can change. Declare with ArrayList<Type> listName = new ArrayList<Type>();.
  • Key methods include add(), remove(), size(), and get().

Boolean Expressions and if Statements

  • Boolean vs. relational operators: ==, !=, <, >, <=, >=.
  • Use &&, ||, ! for compound conditions.
  • Use if, else if, else for decision control structures.

Iteration

  • While Loops: Executes as long as condition true; for loops use initialization, condition, increment.
  • Nested Loops: A loop within another; useful for matrix traversal.

Inheritance

  • Classes can extend other classes. Sub- and superclass relationships defined: public class Child extends Parent { }.
  • Constructors are not inherited but can be called using super().

Recursion

  • A method that calls itself. Must have a base case. Helpful for repeated tasks or traversing data structures (e.g., binary search).
  • Example: Recursive Factorial:
  public int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
  }

Practice Questions

  • Multiple-choice questions to test understanding should follow the above topics, allowing students to apply their knowledge and prepare for the exam effectively.