Compound Assignment Operators

Help Questions

AP Computer Science A › Compound Assignment Operators

Questions 1 - 8
1

In the game score code below, how does the use of *= affect the value of score?


public class GameScoreDemo {

    public static void main(String[] args) {

        int score = 10; // starting score

        /* Player collects a coin */

        score += 5; // add 5 points

        // Player hits a trap

        score -= 3; // lose 3 points

        // Player gets a double-score power-up

        score *= 2; // double the score

        // Player shares points across 3 rounds

        score /= 3; // integer division

        System.out.println("Final score: " + score);

    }

}

It adds 2 to score

It divides score by 2

It doubles score by multiplying by 2

It sets score equal to 2

Explanation

This question tests understanding of compound assignment operators in Java and their effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable score is updated using *= 2 which multiplies the current value of score by 2, effectively doubling it. Choice A is correct because it accurately describes that the *= operator doubles score by multiplying by 2, performing the equivalent of score = score * 2. Choice B is incorrect because it describes the behavior of += 2, which would add 2 to the score rather than multiply by 2. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.

2

In the shopping cart code below, how does the use of += affect the value of totalPrice?


public class ShoppingCartDemo {

    public static void main(String[] args) {

        double totalPrice = 20.00; // starting cart total

        /* Add an item price to the cart total */

        totalPrice += 15.50; // add $15.50

        // Apply a $5 coupon discount

        totalPrice -= 5.00; // subtract $5.00

        // Apply 8% tax by multiplying

        totalPrice *= 1.08; // increase by 8%

        // Reduce to 2 decimal places (in cents) using remainder

        totalPrice %= 100.00; // keep within $0-$99.99

        System.out.println("Final total: $" + totalPrice);

    }

}

It sets totalPrice to 15.50

It adds 15.50 to totalPrice

It multiplies totalPrice by 15.50

It subtracts 15.50 from totalPrice

Explanation

This question tests understanding of compound assignment operators in Java and their effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable totalPrice is updated using += which adds 15.50 to the current value of totalPrice (20.00), resulting in 35.50. Choice A is correct because it accurately states that the += operator adds 15.50 to totalPrice, performing the equivalent of totalPrice = totalPrice + 15.50. Choice B is incorrect because it suggests the operator replaces the value entirely rather than adding to it, which would be the behavior of a simple assignment (=) operator. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.

3

What is the final value of totalPrice after execution of the shopping cart code below?


public class ShoppingCartTotal {

    public static void main(String[] args) {

        double totalPrice = 50.00; // starting cart total

        /* Add two items */

        totalPrice += 12.00; // add first item

        totalPrice += 8.00;  // add second item

        // Apply a $10 discount

        totalPrice -= 10.00; // coupon

        // Apply 5% tax

        totalPrice *= 1.05; // tax

        // Split total across 2 payments

        totalPrice /= 2.0; // half now

        System.out.println("Final total: $" + totalPrice);

    }

}

26.25

30.00

31.50

63.00

Explanation

This question tests understanding of compound assignment operators in Java and their cumulative effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable totalPrice undergoes multiple operations: starting at 50.00, adding 12.00 and 8.00 (70.00), subtracting 10.00 (60.00), multiplying by 1.05 (63.00), and finally dividing by 2.0 (31.50). Choice A is correct because it accurately reflects the cumulative effect of all compound operations on totalPrice, resulting in 31.50. Choice B is incorrect because it represents the value before the final division operation, missing the last step in the calculation sequence. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.

4

What is the final value of score after execution of the game score code below?


public class GameScoreTotal {

    public static void main(String[] args) {

        int score = 7; // starting score

        /* Earn points for actions */

        score += 9; // bonus points

        score *= 3; // triple score

        // Penalty for mistake

        score -= 5; // subtract 5

        // Convert to points per level (2 levels)

        score /= 2; // integer division

        // Keep only remainder when split into 4 teams

        score %= 4; // remainder

        System.out.println("Final score: " + score);

    }

}

1

2

4

16

Explanation

This question tests understanding of compound assignment operators in Java and their cumulative effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable score undergoes multiple operations: starting at 7, adding 9 (16), multiplying by 3 (48), subtracting 5 (43), dividing by 2 with integer division (21), and finally taking the remainder when divided by 4 (1). Choice B is correct because it accurately reflects the cumulative effect of all compound operations on score, with 21 % 4 = 1. Choice A is incorrect because it misunderstands the modulo operation, possibly confusing it with division or another calculation. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.

5

Which line of code demonstrates the use of a compound assignment operator in the bank code below?


public class BankTransactionDemo {

    public static void main(String[] args) {

        double accountBalance = 100.00; // initial balance

        /* Deposit paycheck */

        accountBalance += 250.00; // add funds

        // Withdraw cash

        accountBalance -= 40.00; // remove funds

        // Apply monthly interest

        accountBalance *= 1.02; // 2% interest

        System.out.println("Balance: $" + accountBalance);

    }

}

double accountBalance = 100.00;

System.out.println("Balance: $" + accountBalance);

public static void main(String[] args) {

accountBalance += 250.00;

Explanation

This question tests understanding of compound assignment operators in Java and their identification in code. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the line accountBalance += 250.00; demonstrates the use of a compound assignment operator, specifically the += operator which adds 250.00 to the current balance. Choice B is correct because it contains the += operator, which is a compound assignment operator that performs addition and assignment in one step. Choice A is incorrect because it shows a simple variable declaration and initialization, not a compound assignment operator. To help students master this concept, emphasize the importance of recognizing the syntax of compound operators (+=, -=, *=, /=, %=) versus simple assignment (=). Practice identifying these operators in real code examples to build pattern recognition skills.

6

What is the final value of accountBalance after execution of the bank code below?


public class BankBalanceTotal {

    public static void main(String[] args) {

        double accountBalance = 80.00; // initial balance

        /* Deposit and withdrawal operations */

        accountBalance += 20.00; // deposit

        accountBalance -= 15.00; // withdraw

        // Apply a 10% fee multiplier (penalty)

        accountBalance *= 0.90; // reduce by 10%

        // Split into 5 equal envelopes

        accountBalance /= 5.0; // divide evenly

        System.out.println("Balance: $" + accountBalance);

    }

}

13.00

15.30

17.00

85.00

Explanation

This question tests understanding of compound assignment operators in Java and their cumulative effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable accountBalance undergoes multiple operations: starting at 80.00, adding 20.00 (100.00), subtracting 15.00 (85.00), multiplying by 0.90 (76.50), and finally dividing by 5.0 (15.30). Choice A is correct because it accurately reflects the cumulative effect of all compound operations on accountBalance, resulting in 15.30. Choice B is incorrect because it represents an incorrect calculation, possibly missing one or more of the operations in the sequence. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.

7

In the data processing loop below, how does the use of %= affect the value of checksum?


public class DataProcessingDemo {

    public static void main(String[] args) {

        int checksum = 0; // initial checksum

        int[] values = {12, 7, 9}; // data values

        /* Aggregate values into checksum */

        for (int v : values) {

            checksum += v; // add each value

        }

        // Mix checksum by scaling

        checksum *= 3; // multiply by 3

        // Keep checksum in range 0-9

        checksum %= 10; // remainder after dividing by 10

        System.out.println("Checksum: " + checksum);

    }

}

It sets checksum equal to 10

It adds 10 to checksum

It keeps only checksum's remainder mod 10

It divides checksum by 10

Explanation

This question tests understanding of compound assignment operators in Java and their effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable checksum is updated using %= 10 which calculates the remainder when checksum is divided by 10, effectively keeping only the last digit. Choice A is correct because it accurately describes that the %= operator keeps only checksum's remainder mod 10, performing the equivalent of checksum = checksum % 10. Choice B is incorrect because it suggests the operator sets checksum equal to 10, which would be the behavior of a simple assignment (checksum = 10) rather than a modulo operation. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.

8

In the shopping cart code below, how does the use of /= affect the value of totalPrice?


public class ShoppingCartSplit {

    public static void main(String[] args) {

        double totalPrice = 30.00; // starting total

        /* Add item and apply discount */

        totalPrice += 18.00; // add item

        totalPrice -= 6.00;  // discount

        // Apply tax

        totalPrice *= 1.10; // 10% tax

        // Split bill between 2 people

        totalPrice /= 2.0; // divide total by 2

        System.out.println("Each pays: $" + totalPrice);

    }

}

It multiplies totalPrice by 2.0

It subtracts 2.0 from totalPrice

It divides totalPrice by 2.0

It sets totalPrice to 2.0

Explanation

This question tests understanding of compound assignment operators in Java and their effects on variable values. Compound assignment operators in Java, like += and *=, provide shorthand methods for updating variable values by performing an operation and assignment in one step. In the provided code snippet, the variable totalPrice is updated using /= 2.0 which divides the current value of totalPrice by 2.0, effectively splitting the bill in half. Choice A is correct because it accurately states that the /= operator divides totalPrice by 2.0, performing the equivalent of totalPrice = totalPrice / 2.0. Choice B is incorrect because it describes the behavior of *= 2.0, which would multiply rather than divide the value. To help students master this concept, emphasize the importance of understanding each operator's function and practice tracing variable changes step-by-step. Encourage frequent use of print statements to verify expected outcomes during coding.