AP Computer Science A
AP Computer Science A
Section titled “AP Computer Science A”Course Overview
Section titled “Course Overview”AP Computer Science A is a college-level course that introduces students to computer science through Java programming. The course covers fundamental programming concepts, object-oriented programming, data structures, and algorithms.
Exam Format
Section titled “Exam Format”- Duration: 3 hours
- Sections:
- Section I: 40 multiple-choice questions (1 hour 30 minutes)
- Section II: 4 free-response questions (1 hour 30 minutes)
- Score: 1-5 (credit policy is per-institution: most state universities grant credit for 3+; selective colleges commonly require 4-5 or award placement instead)
Topics Covered
Section titled “Topics Covered”| Topic | Weight | Questions |
|---|---|---|
| Primitive Types | 2.5-5% | 1-2 |
| Using Objects | 5-7.5% | 2-3 |
| Control Flow | 15-17.5% | 6-7 |
| Arrays | 10-12.5% | 4-5 |
| Classes | 5-7.5% | 2-3 |
| Inheritance | 5-7.5% | 2-3 |
| Recursion | 5-7.5% | 2-3 |
| ArrayList | 10-12.5% | 4-5 |
| 2D Arrays | 5-7.5% | 2-3 |
Java Fundamentals
Section titled “Java Fundamentals”Primitive Types
Section titled “Primitive Types”Java has 8 primitive data types:
// Integer typesbyte myByte = 127; // 8-bit, -128 to 127short myShort = 32000; // 16-bit, -32768 to 32767int myInt = 2147483647; // 32-bit, -2^31 to 2^31-1long myLong = 9223372036854775807L; // 64-bit
// Floating-point typesfloat myFloat = 3.14f; // 32-bit, ~7 decimal digitsdouble myDouble = 3.141592653589793; // 64-bit, ~15 decimal digits
// Other typeschar myChar = 'A'; // 16-bit Unicode characterboolean myBoolean = true; // true or falseVariables and Constants
Section titled “Variables and Constants”// Variable declaration and initializationint score = 100;String name = "Alice";double average = 95.5;
// Constants (final keyword)final int MAX_SCORE = 100;final String GRADE_SCALE = "A-F";Operators
Section titled “Operators”// Arithmetic operatorsint sum = 10 + 5; // 15int difference = 10 - 5; // 5int product = 10 * 5; // 50int quotient = 10 / 3; // 3 (integer division)int remainder = 10 % 3; // 1
// Relational operatorsboolean isEqual = (10 == 10); // trueboolean isGreater = (10 > 5); // trueboolean isLess = (10 < 5); // false
// Logical operatorsboolean and = true && false; // falseboolean or = true || false; // trueboolean not = !true; // false
// Increment/Decrementint x = 5;x++; // x is now 6x--; // x is now 5Control Flow
Section titled “Control Flow”If-Else Statements
Section titled “If-Else Statements”int grade = 85;
if (grade >= 90) { System.out.println("A");} else if (grade >= 80) { System.out.println("B");} else if (grade >= 70) { System.out.println("C");} else if (grade >= 60) { System.out.println("D");} else { System.out.println("F");}Switch Statements
Section titled “Switch Statements”String day = "Monday";
switch (day) { case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday": System.out.println("Weekday"); break; case "Saturday": case "Sunday": System.out.println("Weekend"); break; default: System.out.println("Invalid day");}For Loops
Section titled “For Loops”// Basic for loopfor (int i = 0; i < 10; i++) { System.out.println(i);}
// Enhanced for loop (for-each)int[] numbers = {1, 2, 3, 4, 5};for (int num : numbers) { System.out.println(num);}While and Do-While Loops
Section titled “While and Do-While Loops”// While loopint count = 0;while (count < 10) { System.out.println(count); count++;}
// Do-while loop (executes at least once)int count = 0;do { System.out.println(count); count++;} while (count < 10);Arrays
Section titled “Arrays”Declaration and Initialization
Section titled “Declaration and Initialization”// Declare and allocateint[] numbers = new int[10];
// Declare and initializeint[] numbers = {1, 2, 3, 4, 5};
// Access elementsint first = numbers[0]; // 1int last = numbers[4]; // 5
// Modify elementsnumbers[0] = 10;
// Array lengthint length = numbers.length; // 5Array Traversal
Section titled “Array Traversal”int[] numbers = {1, 2, 3, 4, 5};
// Using for loopfor (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]);}
// Using enhanced for loopfor (int num : numbers) { System.out.println(num);}Common Array Operations
Section titled “Common Array Operations”// Find maximumint max = numbers[0];for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; }}
// Reverse arrayfor (int i = 0; i < numbers.length / 2; i++) { int temp = numbers[i]; numbers[i] = numbers[numbers.length - 1 - i]; numbers[numbers.length - 1 - i] = temp;}Object-Oriented Programming
Section titled “Object-Oriented Programming”Classes and Objects
Section titled “Classes and Objects”public class Student { // Instance variables private String name; private int age; private double gpa;
// Constructor public Student(String name, int age, double gpa) { this.name = name; this.age = age; this.gpa = gpa; }
// Methods public String getName() { return name; }
public void setGpa(double gpa) { this.gpa = gpa; }
public boolean isHonorRoll() { return gpa >= 3.5; }}
// Creating objectsStudent alice = new Student("Alice", 16, 3.8);Student bob = new Student("Bob", 17, 3.2);Inheritance
Section titled “Inheritance”public class Animal { protected String name;
public Animal(String name) { this.name = name; }
public void speak() { System.out.println(name + " makes a sound"); }}
public class Dog extends Animal { public Dog(String name) { super(name); }
@Override public void speak() { System.out.println(name + " barks"); }}Polymorphism
Section titled “Polymorphism”Animal animal = new Dog("Rex");animal.speak(); // Outputs: Rex barks (runtime polymorphism)
// Method overloadingpublic class Calculator { public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }}Data Structures
Section titled “Data Structures”ArrayList
Section titled “ArrayList”import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
// Add elementsnames.add("Alice");names.add("Bob");names.add("Charlie");
// Access elementsString first = names.get(0); // "Alice"
// Remove elementsnames.remove(1); // Removes "Bob"
// Sizeint size = names.size(); // 2
// Check if containsboolean hasAlice = names.contains("Alice"); // trueHashMap
Section titled “HashMap”import java.util.HashMap;
HashMap<String, Integer> ages = new HashMap<>();
// Add entriesages.put("Alice", 16);ages.put("Bob", 17);
// Get valuesint aliceAge = ages.get("Alice"); // 16
// Check if key existsboolean hasAlice = ages.containsKey("Alice"); // true
// Remove entryages.remove("Bob");
// Sizeint size = ages.size(); // 1Algorithms
Section titled “Algorithms”Searching
Section titled “Searching”// Linear Searchpublic static int linearSearch(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) { return i; } } return -1;}
// Binary Search (array must be sorted)public static int binarySearch(int[] arr, int target) { int low = 0; int high = arr.length - 1;
while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return -1;}Sorting
Section titled “Sorting”// Selection Sortpublic static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // Swap int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; }}
// Insertion Sortpublic static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1;
while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; }}Recursion
Section titled “Recursion”// Factorialpublic static int factorial(int n) { if (n <= 1) { return 1; } return n * factorial(n - 1);}
// Fibonaccipublic static int fibonacci(int n) { if (n <= 1) { return n; } return fibonacci(n - 1) + fibonacci(n - 2);}Exam Preparation
Section titled “Exam Preparation”Practice Questions
Section titled “Practice Questions”Question 1: What is the output of the following code?
int x = 5;int y = x++;System.out.println(y);A) 4 B) 5 C) 6 D) Error
Answer: B) 5 (post-increment returns original value)
Question 2: Which data structure uses FIFO ordering? A) Stack B) Queue C) ArrayList D) HashMap
Answer: B) Queue (First In, First Out)
Question 3: What is the time complexity of binary search? A) O(n) B) O(log n) C) O(n²) D) O(1)
Answer: B) O(log n)
Tips for Success
Section titled “Tips for Success”- Practice coding daily - Build muscle memory
- Understand concepts, not just syntax - Why things work
- Do past exams - Familiarize with question format
- Time management - Don’t spend too long on one question
- Debug systematically - Use print statements to trace
Common Mistakes
Section titled “Common Mistakes”- Off-by-one errors - Array indices start at 0
- Integer division - 10/3 = 3, not 3.33
- Null pointer exceptions - Check for null before using objects
- Forgetting return statements - Methods with return type must return
- Confusing == with .equals() - Use .equals() for object comparison