General Test Information
Java Quick Reference Sheet
Format of the 2024 AP CSA Exam
Multiple Choice
-
40 Questions 1 Hour 30 Minutes 50% of Exam Score - The MCQ section will mostly be individual questions, with 1-2 sets of two questions each.
Free Response
-
4 Questions 1 Hour 30 Minutes 50% of Exam Score -
Question #1: Methods and Control Structures
-
Question #2: Classes
-
Question #3: Array/ Array List
- Question #4: 2D Array
Review time 😁
Unit 1 - Primative Types
U1 Key learning targets:
- Data Types: Understand the different primitive data types in Java such as int, double, char, boolean.
- Variables and Constants: Learn how to declare variables and constants, and understand their scope.
- Literals: Know how to use literals to represent values directly in code.
- Operators: Familiarize yourself with arithmetic, relational, logical, and assignment operators.
-
Type Casting: Understand implicit and explicit type casting in Java.
- Primitive Types:
- Basic data types like
int
,double
,char
,boolean
. - Each type has a specific size and range of values.
- Example:
int
for integers,double
for floating-point numbers.
- Basic data types like
- Variables and Constants:
- Variables: Named memory locations to store data temporarily.
- Declared with a type and name, can change value during execution.
- Constants: Variables with values that cannot be changed.
- Declared with
final
keyword, conventionally named with uppercase letters.
- Declared with
- Variables: Named memory locations to store data temporarily.
- Literals:
- Literal representations of values in Java code.
- Types include integer (e.g.,
10
), floating-point (e.g.,3.14
), character (e.g.,'A'
), string (e.g.,"Hello"
), and boolean (e.g.,true
orfalse
) literals. - Example:
42
as an integer literal,'X'
as a character literal.
- Operators:
- Arithmetic:
+
,-
,*
,/
,%
. - Assignment:
=
,+=
,-=
,*=
,/=
,%=
. - Comparison:
==
,!=
,<
,<=
,>
,>=
. - Logical:
&&
,||
,!
. - Bitwise:
&
,|
,^
,~
,<<
,>>
,>>>
. - Example:
x + y
for addition,x > y
for comparison.
- Arithmetic:
- Type Casting:
- Converting a value from one data type to another.
- Implicit casting: Automatically done by the compiler (e.g.,
int
todouble
). - Explicit casting: Manually performed, may result in data loss.
- Example:
(double) x
to castx
to adouble
.
// Declaration and initialization of variables
int age = 25;
double height = 5.9;
char gender = 'M';
boolean isStudent = true;
// Output variables
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Gender: " + gender);
System.out.println("Is student: " + isStudent);
Age: 25
Height: 5.9
Gender: M
Is student: true
Unit 2: Using Objects
U2 Key learning targets:
- Classes and Objects: Understand how classes are used to create objects, and how to access their properties and methods.
- Constructors: Learn about constructors and their role in object initialization.
- Instance Methods: Understand the concept of instance methods and how they operate on object data.
- Encapsulation: Learn about encapsulation and how it helps in data hiding and abstraction.
- String Class: Explore the methods provided by the String class for manipulating strings.
// Class definition
class Person {
// Instance variables
String name;
int age;
// Constructor
public Person(String n, int a) {
name = n;
age = a;
}
// Instance method
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Creating objects of Person class
Person person1 = new Person("John", 21);
Person person2 = new Person("Aniket", 17);
// Calling instance method
person1.displayInfo();
person2.displayInfo();
Name: John
Age: 21
Name: Aniket
Age: 17
Unit 3: Boolean Expressions and If Statements
U3 Key learning targets:
- Boolean Expressions: Understand boolean expressions and their role in decision making.
- If Statements: Learn about conditional statements and how they are used to control the flow of a program.
- Nested If Statements: Understand nested if statements and their use in complex decision-making scenarios.
-
Boolean Operators: Learn about logical operators (&&, , !) and their use in combining boolean expressions.
int x = 10;
// If statement
if (x > 0) {
System.out.println("x is positive");
} else if (x < 0) {
System.out.println("x is negative");
} else {
System.out.println("x is zero");
}
x is positive
Unit 4: Iteration
U4 Key learning targets:
- While Loops: Understand the syntax and usage of while loops for repetitive tasks.
- For Loops: Learn how to use for loops as a concise way to iterate over a range of values.
- Nested Loops: Understand nested loops and their applications in handling multidimensional data structures.
- Loop Control: Learn about loop control statements like break and continue.
// Printing numbers from 1 to 5 using for loop
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
1
2
3
4
5
Unit 5: Writing Classes
U5 Key learning targets:
- Defining Classes: Understand the process of defining custom classes to model real-world entities.
- Instance Variables: Learn about instance variables and their role in storing object state.
- Methods: Understand how to define methods within a class to perform operations on object data.
- Constructors: Review constructors and their importance in initializing object state.
- Access Modifiers: Learn about access modifiers (public, private, protected) and their role in controlling access to class members.
// Class definition
public class Student {
// Instance variables
private String name;
private int age;
private String grade;
// Constructor with parameters
public Student(String name, int age, String grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
// Method to display student information
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);
}
// Getter and Setter methods for name
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Getter and Setter methods for age
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// Getter and Setter methods for grade
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
Unit 6: Array Lists
U6 Key learning targets:
- Array Lists: Understand the ArrayList class and its usage for dynamic arrays.
- Adding and Removing Elements: Learn how to add and remove elements from an ArrayList.
- Traversing Arrays: Understand various techniques for traversing and accessing elements in an ArrayList.
- Array List Methods: Familiarize yourself with common methods provided by the ArrayList class for manipulation.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Creating an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<>();
// Adding elements to the ArrayList
numbers.add(10);
numbers.add(20);
numbers.add(30);
// Printing elements of the ArrayList
System.out.println("Elements of the ArrayList:");
for (int num : numbers) {
System.out.println(num);
}
// Adding elements at specific index
numbers.add(1, 15); // Adds 15 at index 1
// Removing an element
numbers.remove(2); // Removes element at index 2
// Accessing elements using index
System.out.println("First element: " + numbers.get(0));
System.out.println("Second element: " + numbers.get(1));
// Checking if ArrayList is empty
System.out.println("Is ArrayList empty? " + numbers.isEmpty());
// Finding the size of the ArrayList
System.out.println("Size of ArrayList: " + numbers.size());
// Clearing the ArrayList
numbers.clear();
System.out.println("ArrayList after clearing: " + numbers);
}
}
ArrayListExample.main(null);
Elements of the ArrayList:
10
20
30
First element: 10
Second element: 15
Is ArrayList empty? false
Size of ArrayList: 3
ArrayList after clearing: []
Unit 7: 2D Arrays
U7 Key learning targets:
- 2D Arrays: Understand the concept of 2D arrays for representing tabular data.
- Array Initialization: Learn how to initialize and populate 2D arrays.
- Traversing 2D Arrays: Understand techniques for traversing rows and columns of a 2D array.
- Manipulating 2D Arrays: Learn about common operations performed on 2D arrays such as transposition and matrix operations.
public class TwoDArrayExample {
public static void main(String[] args) {
// Initializing and populating a 2D array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Traversing and printing the 2D array
System.out.println("Printing 2D array:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Accessing elements of the 2D array
System.out.println("Element at (0, 0): " + matrix[0][0]);
System.out.println("Element at (1, 2): " + matrix[1][2]);
// Transposing the 2D array
int[][] transposedMatrix = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
transposedMatrix[j][i] = matrix[i][j];
}
}
// Printing the transposed 2D array
System.out.println("Transposed 2D array:");
for (int i = 0; i < transposedMatrix.length; i++) {
for (int j = 0; j < transposedMatrix[i].length; j++) {
System.out.print(transposedMatrix[i][j] + " ");
}
System.out.println();
}
}
}
TwoDArrayExample.main(null);
Printing 2D array:
1 2 3
4 5 6
7 8 9
Element at (0, 0): 1
Element at (1, 2): 6
Transposed 2D array:
1 4 7
2 5 8
3 6 9
Unit 8: Inheritance
U8 Key learning targets:
- Inheritance: Understand the concept of inheritance and how it allows classes to inherit properties and behaviors from other classes.
- Superclass and Subclass: Learn about superclass-subclass relationships and how to extend classes.
- Overriding Methods: Understand method overriding and how it allows subclasses to provide their own implementation of inherited methods.
- Polymorphism: Learn about polymorphism and how it allows objects of different classes to be treated uniformly.
// Parent class (superclass)
class Animal {
// Method to make sound
void makeSound() {
System.out.println("Animal makes a sound");
}
}
// Child class (subclass) inheriting from Animal
class Dog extends Animal {
// Overriding the makeSound() method
void makeSound() {
System.out.println("Dog barks");
}
}
// Child class (subclass) inheriting from Animal
class Cat extends Animal {
// Overriding the makeSound() method
void makeSound() {
System.out.println("Cat meows");
}
}
public class InheritanceExample {
public static void main(String[] args) {
// Creating objects of Dog and Cat classes
Dog dog = new Dog();
Cat cat = new Cat();
// Calling makeSound() method of Dog and Cat objects
dog.makeSound(); // Output: Dog barks
cat.makeSound(); // Output: Cat meows
// Using polymorphism
Animal animal1 = new Dog(); // Animal reference, Dog object
Animal animal2 = new Cat(); // Animal reference, Cat object
// Calling makeSound() method using Animal references
animal1.makeSound(); // Output: Dog barks
animal2.makeSound(); // Output: Cat meows
}
}
InheritanceExample.main(null);
Dog barks
Cat meows
Dog barks
Cat meows
Unit 9: Recursion
U9 Key learning targets:
- Recursion Basics: Understand the concept of recursion and how it involves a method calling itself.
- Base Case: Learn about the importance of defining a base case in recursive methods to avoid infinite recursion.
- Recursive Thinking: Practice breaking down problems into smaller subproblems and solving them recursively.
- Recursive vs. Iterative Solutions: Compare and contrast recursive and iterative solutions for various problems.
public class RecursionExample {
// Recursive method to calculate factorial
static int factorial(int n) {
// Base case: If n is 0 or 1, return 1
if (n == 0 || n == 1)
return 1;
// Recursive case: Calculate factorial using recursion
else
return n * factorial(n - 1);
}
// Recursive method to calculate Fibonacci series
static int fibonacci(int n) {
// Base case: If n is 0 or 1, return n
if (n <= 1)
return n;
// Recursive case: Calculate Fibonacci series using recursion
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String[] args) {
// Calculating factorial of 5 using recursion
int factorialResult = factorial(5);
System.out.println("Factorial of 5: " + factorialResult);
// Calculating Fibonacci series up to 10 terms using recursion
System.out.println("Fibonacci series up to 10 terms:");
for (int i = 0; i < 10; i++) {
System.out.print(fibonacci(i) + " ");
}
}
}
RecursionExample.main(null);
Factorial of 5: 120
Fibonacci series up to 10 terms:
0 1 1 2 3 5 8 13 21 34
Unit 10: Searching and Sorting Algorithms
U10 Key learning targets:
- Linear Search: Understand the linear search algorithm and its implementation.
- Binary Search: Learn about the binary search algorithm and its efficiency for sorted arrays.
- Selection Sort: Understand the selection sort algorithm and its implementation.
- Merge Sort: Learn about the merge sort algorithm and its efficiency for sorting large datasets.
public class SearchAndSortExample {
// Linear Search algorithm
static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Return index if target found
}
}
return -1; // Return -1 if target not found
}
// Binary Search algorithm (recursive)
static int binarySearch(int[] arr, int target, int low, int high) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid; // Return index if target found
} else if (arr[mid] < target) {
return binarySearch(arr, target, mid + 1, high); // Search in the right half
} else {
return binarySearch(arr, target, low, mid - 1); // Search in the left half
}
}
return -1; // Return -1 if target not found
}
// Selection Sort algorithm
static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] with arr[minIndex]
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Merge Sort algorithm
static void mergeSort(int[] arr, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid); // Sort left half
mergeSort(arr, mid + 1, high); // Sort right half
merge(arr, low, mid, high); // Merge sorted halves
}
}
// Helper method to merge two sorted subarrays
static void merge(int[] arr, int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;
int[] left = new int[n1];
int[] right = new int[n2];
for (int i = 0; i < n1; i++) {
left[i] = arr[low + i];
}
for (int j = 0; j < n2; j++) {
right[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < n1) {
arr[k++] = left[i++];
}
while (j < n2) {
arr[k++] = right[j++];
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
// Linear Search
int linearResult = linearSearch(arr, 25);
System.out.println("Linear Search: Index of 25 is " + linearResult);
// Binary Search (requires sorted array)
mergeSort(arr, 0, arr.length - 1); // Sorting array before performing binary search
int binaryResult = binarySearch(arr, 25, 0, arr.length - 1);
System.out.println("Binary Search: Index of 25 is " + binaryResult);
// Selection Sort
selectionSort(arr);
System.out.println("Selection Sort: Sorted array is " + Arrays.toString(arr));
// Merge Sort (using the sorted array from selection sort)
mergeSort(arr, 0, arr.length - 1);
System.out.println("Merge Sort: Sorted array is " + Arrays.toString(arr));
}
}
SearchAndSortExample.main(null);
Linear Search: Index of 25 is 2
Binary Search: Index of 25 is 3
Selection Sort: Sorted array is [11, 12, 22, 25, 34, 64, 90]
Merge Sort: Sorted array is [11, 12, 22, 25, 34, 64, 90]