Chapter 3: Operators and Assignments Java Operators
Chapter 3: Operators and Assignments Java Operators
Java Operators
A. 32/ 4;
B. (8 >> 2) << 4;
C. 2^ 5;
D. 128 >>> 2;
E. (2 << 1) * (32 >> 3);
F. 2 >> 5;
ANSWER: B and D
B and D both evaluate to 32. B is shifting bits right then left using the signed bit shifters >> and
<<. D is shifting bits using the unsigned operator >>>, but since the beginning number is positive
the sign is maintained.
A evaluates to 8, C looks like 2 to the 5th power, but ^ is the Exclusive OR operator so C evaluates
to 7. E evaluates to 16, and F evaluates to 0 (2 >> 5 is not 2 to the 5th).
1. import java.awt.*;
2. class Ticker extends Component {
3. public static void main (String [] args) {
4. Ticker t = new Ticker();
5.
6. }
7. }
Which two of the following statements, inserted independently, could legally be inserted into
line 5 of this code? (Choose two.)
ANSWER: B and D
B is correct because class type Ticker is part of the class hierarchy of t; therefore, it is a legal
use of the instanceof operator. D is also correct because Component is part of the hierarchy of
t, because Ticker extends Component in line 2.
class Equals {
public static void main(String[] args) {
int x = 100;
double y = 100.1;
boolean b = (x = y);
System.out.println(b);
}
}
A. true
B. false
C. Compilation fails
D. An exception is thrown at runtime
ANSWER: C
The code will not compile because in line 5, the line will work only if we use (x == y) in the line.
The == operator compares values to produce a boolean, whereas the = operator assigns a value
to variables.
A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this
code, the output would be false.
1. import java.awt.Button;
2. class CompareReference {
3. public static void main(String [] args) {
4. float f = 42.0f;
5. float [] f1 = new float[2];
6. float [] f2 = new float[2];
7. float [] f3 = f1;
8. long x = 42;
9. f1[0] = 42.0f;
10. }
11. }
ANSWER: B, D, and E
B is correct because the reference variables f1 and f3 refer to the same array object. D is correct
because it is legal to compare integer and floating-point types. E is correct because it is legal to
compare a variable with an array element.
class BitShift {
public static void main(String[] args) {
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}
A. -2147483648 and 1
B. 0x80000000 and 0x00000001
C. -2147483648 and -1
D. 1 and -2147483648
E. None of the above
ANSWER: A
The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation
looks like this:
C is incorrect because the >>> operator zero fills the left bits, which in this case changes the
sign of x, as shown. B is incorrect because the output method print() always displays integers
in base 10. D is incorrect because this is the reverse order of the two output numbers. E is
incorrect because there was a correct answer.
class Bitwise {
public static void main(String[] args) {
int x = 11 & 9;
int y = x ^ 3;
System.out.println(y | 12);
}
}
A. 0
B. 7
C. 8
D. 14
E. 15
ANSWER: D
The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^
operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The |
operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.
7. Which of the following are legal lines of code? (Choose all that apply.)
A. int w = (int)888.8;
B. byte x = (byte)1000L;
C. long y = (byte)100;
D. byte z = (byte)100L;
ANSWER: A, B, C, and D
A is correct because when a floating-point number (a double in this case) is cast to an int, it
simply loses the digits after the decimal. B and D are correct because a long can be cast into a
byte. If the long is over 127, it loses its most significant (leftmost) bits. C actually works, even
though a cast is not necessary, because a long can store a byte.
class Test {
public static void main(String[] args) {
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++) {
if ((++x > 2) || (++y > 2)) {
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 5 3
B. 8 2
C. 8 3
D. 8 5
E. 10 3
F. 10 5
ANSWER: B
The first two iterations of the for loop both x and y are incremented. On the third iteration x is
incremented, and for the first time becomes greater than 2. The short circuit or operator ||
keeps y from ever being incremented again and x is incremented twice on each of the last three
iterations.
class Test {
public static void main(String[] args) {
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++) {
if ((++x > 2) && (++y > 2)) {
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 5 2
B. 5 3
C. 6 3
D. 6 4
E. 7 5
F. 8 5
ANSWER: C
In the first two iterations x is incremented once and y is not because of the short circuit &&
operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration
x is doubly incremented and y is incremented.
class SSBool {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if (b1 & b2 | b2 & b3 | b2)
System.out.print("ok ");
if (b1 & b2 | b2 & b3 | b2 | b1)
System.out.println("dokey");
}
}
A. ok
B. dokey
C. ok dokey
D. No output is produced
E. Compilation error
F. An exception is thrown at runtime
ANSWER: B
The & operator has a higher precedence than the | operator so that on line 6 b1 and b2 are
evaluated together as are b2 & b3. The final b1 in line 8 is what causes that if test to be true.
class Test {
public static void main(String[] args) {
int x = 20;
String sup = (x < 15) ? "small" : (x < 22) ? "tiny" : "huge";
System.out.println(sup);
}
}
A. small
B. tiny
C. huge
D. Compilation fails
ANSWER: B
This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the
“tiny” value is assigned to sup.
class BoolArray {
boolean[] b = new boolean[3];
int count = 0;
void test() {
if (b[0] && b[1] | b[2])
count++;
if (b[1] && b[(++count - 2)])
count += 7;
System.out.println("count = " + count);
}
}
A. count = 0
B. count = 2
C. count = 3
D. count = 4
E. count = 10
F. count = 11
ANSWER: C
The reference variables b and x both refer to the same boolean array. Count is incremented for
each call to the set() method, and once again when the first if test is true. Because of the &&
short circuit operator, count is not incremented during the second if test.
class Test {
static int s;
void start() {
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x) {
x = x * 2;
s = x;
}
}
A. 77
B. 7 14
C. 14 0
D. 14 14
E. Compilation fails
F. An exception is thrown at runtime
The int x in the twice() method is not the same int x as in the start() method. Start()’s x is not
affected by the twice() method. The instance variable s is updated by twice()’s x, which is 14.
class Test {
public static void main(String[] args) {
Test p = new Test();
p.start();
}
void start() {
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
return b1;
}
}
A. true true
B. false true
C. true false
D. false false
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: B
The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The
b1 in the start() method is not updated by the fix() method.
class PassS {
public static void main(String[] args) {
PassS p = new PassS();
p.start();
}
void start() {
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
A. slip stream
B. slipstream stream
C. stream slip stream
D. slipstream slip stream
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: D
When the fix() method is first entered, start()’s s1 and fix()’s s1 reference variables both refer
to the same String object (with a value of “slip”). Fix()’s s1 is reassigned to a new object that is
created when the concatenation occurs (this second String object has a value of “slipstream”).
When the program returns to start(), another String object is created, referred to by s2 and with
a value of “stream”.
class SC2 {
public static void main(String[] args) {
SC2 s = new SC2();
s.start();
}
void start() {
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo() {
return "foo";
}
}
A. 9 7 7 foo 7 7foo
B. 72 34 34 foo34 34foo
C. 9 7 7 foo34 34foo
D. 72 7 34 foo34 7foo
E. 9 34 34 foo34 34foo
ANSWER: D
Because all of these expressions use the + operator, there is no precedence to worry about and
all of the expressions will be evaluated from left to right. If either operand being evaluated is a
String, the + operator will concatenate the two operands; if both operands are numeric, the +
operator will add the two operands.
class PassA {
public static void main(String[] args) {
PassA p = new PassA();
p.start();
}
void start() {
long[] a1 = { 3, 4, 5 };
long[] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
A. 12 15
B. 15 15
C. 345375
D. 375375
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: B
The reference variables a1 and a3 refer to the same long array object. When the [1] element is
updated in the fix() method, it is updating the array referred to by a1. The reference variable
a2 refers to the same array object.
class Two {
byte x;
}
class PassO {
public static void main(String[] args) {
PassO p = new PassO();
p.start();
}
void start() {
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
A. null null 42
B. 00 42
C. 0 42 42
D. 000
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: C
In the fix() method, the reference variable tt refers to the same object (class Two) as the t
reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same
object). Remember also that the instance variable x in the Two class is initialized to 0.