Review Questions

graphics/rq_icon.gif

3.20

What would be printed during execution of the following program?

public class MyClass {
    public static void main(String[] args) {
        test(1<<32, "1<<32");
        test(1<<31, "1<<31");
        test(1<<30, "1<<30");
        test(1,     "1"    );
        test(0,     "0"    );
        test(-1,    "-1"   );
    }

    public static void test(int i, String exp) {
        if ((i >> 1) != (i >>> 1)) System.out.println(exp);
    }
}

Select the two correct answers.

  1. "1<<32"

  2. "1<<31"

  3. "1<<30"

  4. "1"

  5. "0"

  6. "-1"

3.21

Which of the following are not operators in Java?

Select the two correct answers.

  1. %

  2. <<<

  3. &

  4. %=

  5. >>>

  6. <=

  7. &&=

3.22

Given a variable x of type int (which may contain a negative value), which are correct ways of doubling the value of x, barring any wrapping of out-of-range intermediate values?

Select the four correct answers.

  1. x << 1;

  2. x = x * 2;

  3. x *= 2;

  4. x += x;

  5. x <<= 1;

3.23

Which of the following operators can be used both as an integer bitwise operator and a boolean logical operator?

Select the three correct answers.

  1. ^

  2. !

  3. &

  4. |

  5. ~

3.24

Given these declarations, which of the following expressions are valid?

byte  b = 1;
char  c = 1;
short s = 1;
int   i = 1;

Select the three correct answers.

  1. s = b * 2;

  2. i = b << s;

  3. b <<= s;

  4. c = c + b;

  5. s += i;