APCS-A UNIT 1 STUDY GUIDE: PRIMITIVE TYPES Prepared for the May 2026 APCS-A Exam =============================================================================== OVERVIEW ======== Unit 1 covers Primitive Types, which forms the foundation for all Java programming. This unit typically accounts for 2.5-5% of the multiple-choice questions and appears in most Free Response Questions as fundamental building blocks. =============================================================================== TOPIC CHECKLIST ================ [ ] Four primitive data types (int, double, boolean, char) [ ] Variable declaration and initialization [ ] Arithmetic operators and precedence [ ] Assignment operators [ ] Type casting (implicit and explicit) [ ] Input/output operations [ ] Common pitfalls and error patterns =============================================================================== PRIMITIVE DATA TYPES ===================== 1. INT (INTEGER) - Range: -2,147,483,648 to 2,147,483,647 - Use for: Counting, indexing, whole number calculations - Examples: int age = 16; int score = 95; 2. DOUBLE (DOUBLE-PRECISION FLOATING-POINT) - Range: Approximately ±1.7 × 10^308 - Use for: Decimal calculations, measurements, averages - Examples: double gpa = 3.85; double temperature = 98.6; 3. BOOLEAN (BOOLEAN) - Values: true or false only - Use for: Flags, conditions, yes/no situations - Examples: boolean isRaining = true; boolean passed = false; 4. CHAR (CHARACTER) - Range: 0 to 65,535 (Unicode values) - Use for: Single characters - Examples: char grade = 'A'; char symbol = '$'; >>> COMMON MISTAKE: Using double quotes for char (char letter = "A"; WRONG) instead of single quotes (char letter = 'A'; CORRECT) =============================================================================== VARIABLE OPERATIONS =================== DECLARATION AND INITIALIZATION ------------------------------- // Declaration only int count; double price; // Declaration with initialization (PREFERRED) int students = 25; double tax = 0.08; boolean ready = false; char initial = 'K'; ASSIGNMENT OPERATORS -------------------- = : Assignment += : Add and assign (x += 5 same as x = x + 5) -= : Subtract and assign *= : Multiply and assign /= : Divide and assign %= : Modulus and assign =============================================================================== ARITHMETIC OPERATIONS ===================== BASIC OPERATORS (IN ORDER OF PRECEDENCE) ----------------------------------------- 1. Parentheses () 2. Unary operators ++, --, +, - 3. Multiplication, Division, Modulus *, /, % 4. Addition, Subtraction +, - *** CRITICAL PROBLEM AREA: INTEGER DIVISION *** ----------------------------------------------- int result1 = 7 / 2; // Result: 3 (NOT 3.5!) double result2 = 7 / 2; // Result: 3.0 (still integer division) double result3 = 7.0 / 2; // Result: 3.5 (floating-point division) double result4 = 7 / 2.0; // Result: 3.5 (floating-point division) MODULUS OPERATOR (%) -------------------- - Returns the remainder after division - 17 % 5 = 2 (17 ÷ 5 = 3 remainder 2) - 20 % 4 = 0 (20 ÷ 4 = 5 remainder 0) - With negatives: Result has same sign as the dividend * -7 % 3 = -1 * 7 % -3 = 1 INCREMENT/DECREMENT OPERATORS ------------------------------ int x = 5; // Pre-increment: increment first, then use value int a = ++x; // x becomes 6, a gets 6 // Post-increment: use value first, then increment int b = x++; // b gets 6, x becomes 7 =============================================================================== TYPE CASTING ============ IMPLICIT CASTING (AUTOMATIC - WIDENING) ---------------------------------------- Java automatically converts from smaller to larger data types: int i = 100; double d = i; // int → double (automatic) char c = 'A'; int ascii = c; // char → int (automatic) Widening Order: byte → short → int → long → float → double EXPLICIT CASTING (MANUAL - NARROWING) -------------------------------------- Must use cast operator when converting from larger to smaller: double d = 3.14159; int i = (int) d; // Result: 3 (decimal part truncated!) int large = 300; byte small = (byte) large; // Possible overflow! *** MAJOR PITFALL: CASTING IN EXPRESSIONS *** ---------------------------------------------- int a = 7, b = 2; double result1 = (double) a / b; // 7.0 / 2 = 3.5 CORRECT double result2 = (double) (a / b); // (double) 3 = 3.0 WRONG =============================================================================== OPERATOR PRECEDENCE PROBLEMS ============================= EXAMPLE 1: BASIC PRECEDENCE ---------------------------- int result = 2 + 3 * 4; // Result: 14 (not 20!) // Calculation: 2 + (3 * 4) = 2 + 12 = 14 EXAMPLE 2: LEFT-TO-RIGHT EVALUATION ------------------------------------ int result = 12 / 3 * 2; // Result: 8 // Calculation: (12 / 3) * 2 = 4 * 2 = 8 EXAMPLE 3: MIXED TYPES ---------------------- double result = 5 / 2 * 2.0; // Result: 4.0 // Calculation: (5 / 2) * 2.0 = 2 * 2.0 = 4.0 =============================================================================== COMMON EXAM PATTERNS ==================== PATTERN 1: TRACE CODE EXECUTION Given variables, trace through operations step by step. PATTERN 2: PREDICT OUTPUT Determine what will be printed given specific code. PATTERN 3: IDENTIFY ERRORS Find compilation or logical errors in code snippets. PATTERN 4: CHOOSE EQUIVALENT EXPRESSIONS Select expressions that produce the same result. =============================================================================== HIGH-FREQUENCY MISTAKE CATEGORIES ================================== 1. INTEGER DIVISION CONFUSION - Problem: int average = (80 + 90 + 70) / 3; gives 80, not 80.0 - Solution: Use double or cast: double average = (80 + 90 + 70) / 3.0; 2. OPERATOR PRECEDENCE ERRORS - Problem: 2 + 3 * 4 evaluated as (2 + 3) * 4 - Remember: Multiplication/division before addition/subtraction 3. TYPE CASTING DATA LOSS - Problem: (int) 3.9 gives 3, not 4 - Remember: Casting truncates, doesn't round 4. ASSIGNMENT VS. EQUALITY - Problem: Using = instead of == in conditions - Remember: = assigns, == compares 5. UNINITIALIZED VARIABLES - Problem: Using variables before assigning values - Solution: Always initialize variables when declared =============================================================================== PRACTICE PROBLEMS ================= PROBLEM SET A: BASIC OPERATIONS -------------------------------- 1. int x = 15; int y = 4; int z = x / y * 2; What is z? 2. double a = 7; double b = 3; double c = a / b; What is c? 3. int p = 17; int q = 5; int r = p % q; What is r? PROBLEM SET B: TYPE CASTING ---------------------------- 1. double d = 5.8; int i = (int) d; What is i? 2. char c = 'B'; int ascii = c; What is ascii? 3. int x = 7; int y = 2; double z = (double) x / y; What is z? PROBLEM SET C: COMPLEX EXPRESSIONS ----------------------------------- 1. int result = 3 + 4 * 2 - 1; What is result? 2. double val = 10 / 3 * 1.0; What is val? 3. int a = 5; int b = ++a * 2; What are a and b? SOLUTIONS --------- Set A: 1) 6, 2) 2.333..., 3) 2 Set B: 1) 5, 2) 66, 3) 3.5 Set C: 1) 10, 2) 3.0, 3) a=6, b=12 =============================================================================== EXAM STRATEGY TIPS ================== FOR MULTIPLE CHOICE: --------------------- 1. Trace carefully: Work through each step methodically 2. Watch for traps: Look for integer division and precedence issues 3. Check data types: Pay attention to int vs double 4. Test edge cases: Consider negative numbers, zero, and overflow FOR FREE RESPONSE: ------------------ 1. Initialize variables: Always give variables initial values 2. Use meaningful names: Make your code readable 3. Comment tricky parts: Explain complex calculations 4. Test your logic: Mentally trace through your code TIME MANAGEMENT: ---------------- - Multiple Choice: ~1 minute per question - Don't get stuck: Mark difficult questions and return later - Double-check: Verify operator precedence and type casting =============================================================================== ADDITIONAL RESOURCES ==================== COLLEGEBOARD RESOURCES: ------------------------ - AP Computer Science A Course Description - AP Classroom practice questions - Previous FRQ solutions PRACTICE WEBSITES: ------------------ - CodingBat (Java exercises) - AP Central (official practice exams) - Barron's/Princeton Review prep books =============================================================================== PRE-TEST CHECKLIST ================== The day before your test, ensure you can: [ ] Declare and initialize all four primitive types [ ] Perform calculations with proper operator precedence [ ] Cast between types without data loss confusion [ ] Identify integer vs. floating-point division [ ] Trace through complex expressions step-by-step [ ] Spot common errors in code snippets Good luck on your Unit 1 test! Remember: practice makes perfect! ===============================================================================