Operators and Assignments
Operators and Assignments
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
.
Answer: Option B
Explanation:
Output: 15 15
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.
So Output: 3+7+5+" "3+7+5
Output: 15 15 Because Numeric values will be added
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
A
true true
.
B
false true
.
C
true false
.
D
false false
.
Answer: Option B
Explanation:
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.
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
.
Answer: Option D
Explanation:
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".
A
-2147483648 and 1
.
B
0x80000000 and 0x00000001
.
C
-2147483648 and -1
.
D 1 and -2147483648
.
Answer: Option A
Explanation:
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:
Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001
Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.
Option B is incorrect because the output method print() always displays integers in base 10.
Option D is incorrect because this is the reverse order of the two output numbers.
A
true
.
B
false
.
C
Compilation fails
.
D
An exception is thrown at runtime
.
Answer: Option C
Explanation:
The code will not compile because in line 7, 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.
Option 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.
A
small
.
B
tiny
.
C
huge
.
D
Compilation fails
.
Answer: Option B
Explanation:
This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.
A
52
.
B
53
.
C
63
.
D
64
.
Answer: Option C
Explanation:
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.
A
53
.
B
82
.
C
83
.
D
85
.
Answer: Option B
Explanation:
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.
A
0
.
B
7
.
C
8
.
D
14
.
Answer: Option D
Explanation:
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.
A
ok
.
B
dokey
.
C
ok dokey
.
D
No output is produced
.
E. Compilation error
Answer: Option B
Explanation:
The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line
10 is what causes that if test to be true. Hence it prints "dokey".
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
.
Answer: Option D
Explanation:
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.
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
.
Answer: Option B
Explanation:
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 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
0 0 42
.
C
0 42 42
.
D
000
.
Answer: Option C
Explanation:
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.
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
.
Answer: Option C
Explanation:
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.
A
2
.
B
4
.
C
8
.
D
16
.
Answer: Option B
Explanation:
Java only ever passes arguments to a method by value (i.e. a copy of the variable) and never by reference. Therefore the value of the
variable i remains unchanged in the main method.
If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you had 16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If
you had 128 right shifted by 2 bits then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting to binary to do the left and
right bit shifts.
1.
import java.awt.*;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/* Missing Statements ? */
}
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
1. boolean test = (Component instanceof t);
2. boolean test = (t instanceof Ticker);
3. boolean test = t.instanceof(Ticker);
4. boolean test = (t instanceof Component);
A
1 and 4
.
B
2 and 3
.
C
1 and 3
.
D
2 and 4
.
Answer: Option D
Explanation:
(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct
because Component is part of the hierarchy of t, because Ticker extends Component.
(1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is
incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.
2. Which of the following are legal lines of code?
1. int w = (int)888.8;
2. byte x = (byte)1000L;
3. long y = (byte)100;
4. byte z = (byte)100L;
A
1 and 2
.
B
2 and 3
.
C
3 and 4
.
D
All statements are correct.
.
Answer: Option D
Explanation:
Statements (1), (2), (3), and (4) are correct. (1) 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.
(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.
(3) actually works, even though a cast is not necessary, because a long can store a byte.
B
2 and 4
.
C
3 and 4
.
D
1 and 3
.
Answer: Option B
Explanation:
(2) is correct. 16 >> 2 = 4
(4) is correct. 16 >>> 2 = 4
(1) is wrong. 16 * 4 = 64
(3) is wrong. 16/2 ^ 2 = 10
B
2 and 3
.
C
3 and 4
.
D
1 and 4
.
Answer: Option C
Explanation:
(1) is wrong. 3/2 = 1 (integer arithmetic).
(2) is wrong. 3 < 2 = false.
(3) is correct. 3 * 4 = 12.
(4) is correct. 3 <<2= 12. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12 in binary (3*2*2).
5.
import java.awt.Button;
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
B
2, 4 and 5
.
C
3, 4 and 5
.
D
1, 4 and 5
.
Answer: Option B
Explanation:
(2) is correct because the reference variables f1 and f3 refer to the same array object.
(4) is correct because it is legal to compare integer and floating-point types.
(5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.
B
2 and 4
.
C
1 and 3
.
D
2 and 3
.
Answer: Option B
Explanation:
(2) and (4) are correct. (2) and (4) both evaluate to 32. (2) is shifting bits right then left using the signed bit shifters >> and <<. (4) is shifting bits
using the unsigned operator >>>, but since the beginning number is positive the sign is maintained.
(1) evaluates to 8, (3) looks like 2 to the 5th power, but ^ is the Exclusive OR operator so (3) evaluates to 7. (5) evaluates to 0 (2 >> 5 is not 2 to the
5th).