zuai-logo

Compound Assignment Operators

Emily Wilson

Emily Wilson

14 min read

Listen to this study note

Study Guide Overview

This AP Computer Science A study guide covers primitive types and operators, control flow (if, for, while loops, boolean expressions), the String class (methods, concatenation), classes and objects (constructors, methods, inheritance, polymorphism), arrays (basics, traversing), ArrayLists (methods, traversing), 2D arrays, and recursion. It includes practice questions and emphasizes code tracing, exam tips, and common mistakes to avoid.

AP Computer Science A: Ultimate Study Guide 🚀

Hey there, future AP Computer Science A rockstar! This guide is designed to be your best friend the night before the exam. We'll break down the key concepts, offer memory aids, and give you the confidence you need to ace this test. Let's do this! 💪

1. Primitive Types and Operators

1.1. Data Types

  • int: Whole numbers (e.g., -3, 0, 42). 💡
  • double: Decimal numbers (e.g., -3.14, 0.0, 2.718). 💡
  • boolean: true or false. 💡
Key Concept

Remember: int and double are numeric types, while boolean is for logical values. These are your building blocks!

1.2. Arithmetic Operators

  • + (addition), - (subtraction), * (multiplication), / (division), % (modulo - remainder). 💡
  • Integer division truncates (e.g., 5 / 2 is 2).
  • Modulo gives the remainder (e.g., 5 % 2 is 1).
Memory Aid

PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) is your best friend for order of operations. Remember it!

1.3. Compound Assignment Operators

  • +=, -=, *=, /=, %= combine an operation and assignment.
  • x += 5 is the same as x = x + 5. Super handy! 💡

1.4. Increment and Decrement Operators

  • ++ increments by 1, -- decrements by 1. * Post-increment (x++): Returns the original value of x, then increments.
  • Pre-increment (++x): Increments x, then returns the new value.
Exam Tip

For the AP exam, you'll mostly see the post-increment (x++) and post-decrement (x--) operators. Focus on how they change the value after the current statement.

1.5. Code Tracing

  • Follow code line by line, tracking variable changes. Use trace tables if it helps!

  • Pay close attention to how compound assignment and increment/decrement operators modify variables.

Practice Question
json
{
  "mcq": [
    {
      "question": "What is the value of `x` after the following code executes? `int x = 5; x += 3; x--;`",
      "options": ["6", "7", "8", "9"],
      "answer": "7"
    },
    {
      "question": "What is the value of `y` after the following code executes? `int y = 10; y /= 2; y %= 3;`",
      "options": ["0", "1", "2", "3"],
      "answer": "2"
    }
  ],
  "frq": {
    "question": "Consider the following code segment:\n```
java\nint a = 12;\nint b = 5;\nint c = 0;\na -= b;\nb *= 2;\nc = a % b;\na += c;\nb = a - b;\nc *= b;\n```
\n\n(a) What are the final values of a, b, and c? Show your work by tracing the code.",
    "answer": "(a) \n* a -= b; // a = 12 - 5 = 7\n* b *= 2; // b = 5 * 2 = 10\n* c = a % b; // c = 7 % 10 = 7\n* a += c; // a = 7 + 7 = 14\n* b = a - b; // b = 14 - 10 = 4\n* c *= b; // c = 7 * 4 = 28\n\nFinal values: a = 14, b = 4, c = 28\n",
    "scoring": {
      "a": "1 point for correct value of a, 1 point for showing work",
      "b": "1 point for correct value of b, 1 point for showing work",
      "c": "1 point for correct value of c, 1 point for showing work"
     }
   }
}

2. Control Flow

2.1. if Statements

  • Execute code blocks based on conditions.
  • if (condition) { /* code */ }
  • if (condition) { /* code */ } else { /* code */ }
  • if (condition1) { /* code */ } else if (condition2) { /* code */ } else { /* code */ }

2.2. for Loops

  • Repeat code a specific number of times.
  • for (initialization; condition; update) { /* code */ }
  • Example: for (int i = 0; i < 10; i++) { /* code */ }

2.3. while Loops

  • Repeat code while a condition is true.
  • while (condition) { /* code */ }
  • Be careful of infinite loops! ⚠️

2.4. Boolean Expressions

  • Combine conditions using && (AND), || (OR), ! (NOT).
  • true && true is true, true && false is false.
  • true || false is true, false || false is false.
  • !true is false, !false is true.
Memory Aid

Remember AND only returns true if both conditions are true. OR returns true if at least one condition is true. NOT reverses the boolean value.

Common Mistake

Be careful with the difference between = (assignment) and == (equality check) in conditions. Using = in an if statement will cause a compilation error.

2.5. Short-Circuit Evaluation

  • For &&, if the first condition is false, the second isn't evaluated.

  • For ||, if the first condition is true, the second isn't evaluated.

Practice Question
json
{
  "mcq": [
    {
      "question": "What will be printed by the following code? `int x = 5; if (x > 3) { System.out.print(\"A\"); } else { System.out.print(\"B\"); }`",
      "options": ["A", "B", "AB", "Nothing"],
      "answer": "A"
    },
    {
      "question": "How many times will the following loop execute? `for (int i = 0; i < 7; i += 2) { System.out.print(i); }`",
      "options": ["3", "4", "7", "8"],
      "answer": "4"
    }
   ],
  "frq": {
    "question": "Write a method `countEven` that takes an integer array `arr` and returns the number of even numbers in the array. For example, if `arr` is `{1, 2, 3, 4, 5, 6}`, the method should return `3`.",
    "answer": "```
java\npublic int countEven(int[] arr) {\n    int count = 0;\n    for (int num : arr) {\n        if (num % 2 == 0) {\n            count++;\n        }\n    }\n    return count;\n}\n```
",
    "scoring": {
      "initialization": "1 point for initializing the count variable",
      "loop": "1 point for correct `for` or `while` loop structure",
      "conditional": "1 point for correct conditional statement to check for even numbers",
      "increment": "1 point for incrementing the count correctly",
      "return": "1 point for returning the correct count"
 ...

Question 1 of 11

What is the value of x after the code int x = 10; x += 5; executes? ➕

5

10

15

20