Preview

06 - Compound Assignment Operators

 1. Java uses (==) to test if the value on the left is equal to the value on the right and (!=) to test if two items are not equal.

  TRUE

  FALSE

 2. ____________ assignment operators (+=, -=, *=, /=, %=) can be used in place of the assignment operator

  Distributed

  Double

  Compound

  Extreme

 3. Consider this statement: a = a+6; In Java this can also be written as:

  a += 6;

  a + a = 6 +

  aa6+

  a = 6 + .

 4. The increment operator (++) and decrement operator (--) are used to___ 1 or _____1 from the stored value of a variable or an array element.

  divide by / multiply by

  delete / consume

  add / subtract

  extract / exponent

 5. Compound operators, also called combined assignment operators, are a __________ way to update the value of a variable.

  inaccurate

  shorthand

  mathematical

  extended

 6. What does the following compound assignment operator do? -=

  Decrements then assigns

  Binary Right Shift and assigns

  Shift right zero fill and assigns

  Modulus then assigns

 7. What does the following compound assignment operator do? %=

  Binary AND assigns

  Modulus then assigns

  Binary Right Shift and assigns

  Decrements then assigns

 8. What does the following compound assignment operator do? &=

  Binary exclusive OR and assigns

  Binary Right Shift and assigns

  Binary inclusive OR and assigns

  Binary AND assigns

 9. What does the following compound assignment operator do? >>=

  Modulus then assigns

  Decrements then assigns

  Binary Right Shift and assigns

  Binary AND assigns

 10. What does the following compound assignment operator do? |=

  Binary exclusive OR and assigns

  Binary inclusive OR and assigns

  Binary AND assigns

  Binary Right Shift and assigns

 11. What is the final value of i?
int i=2;       //initializing i to 2
i*=2;

  2

  12

  8

  4

 12. What is the final value of a?
int a = 2;
int b = 3;
a *= b + 1;

 13. A binary operator will always have exactly two operands. However, sometimes one or both operands of a binary operator is a subexpression.

  False

  True

 14. Name the constant that is being used in this program.
public class CalculateTax
{
  public static void main ( String[] arg )
  {
    final double DURABLE = 0.045;
    final double NONDURABLE = 0.038;

    . . . . . .
  }
}

 15. Which of the following is an advantage of using a constant (as has been done in the above question) in your program?

  All these statements are valid advantages of the use of constants

  Constants can allow for quick updating and changing (you don' t have to change each occurrence)

  Constants make your program easier to check for correctness and accuracy

  Constants make your program easier to read

 16. Which of the following has the highest operator precedence?

  unary minus -

  remainder

  multiplication *

  addition

 17. Which of the following has the highest operator precedence?

  subtraction

  multiplication *

  short addition +

  addition

 18. The Modulus operator is often used, as is the case in this example, to ________________________________.
(num % 2 == 1 is odd and num % 2 == 0 is even). 

  to create odd numbers with even variables

  to divide even numbers with odd numbers

  to divide even numbers with either 1 or 0

  check for odd and even numbers

 19. MOD can also be used to _______________________________.
num1 % num2 == 0

  check if a number can be divided by zero

  check if any number is oddly divisible by another even number

  check if any number is evenly divisible by another

  check to see if any even number is actually odd

 20. x++ is the same as:

  x = x + 1

  x = x+11

  x = x * 1

  x+1+1

 21. This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
int x = 3;
int y = 5;
int z = 2;
x = z * 2;
y = y / 2;
z++;

  FALSE

  TRUE

 22. This code subtracts one from x, adds one to y, and then sets z to to the value in x - y minus the current value of y.
int x = 0;
int y = 1;
int z = 2;
x--; // x followed bythe double minus sign
y++;
z+=y;

  FALSE

  TRUE

 23. x += y is the same as:

  x = x + y

  x = xy+1

  x = y+y

  x = xy

 24. x *= y is the same as x = x * y

  TRUE

  FALSE

 25. x = / y is the same as x = x / y.

  FALSE

  TRUE