Вы находитесь на странице: 1из 21

Operators

Question 1
class EBH019 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 1;
int i3 = i1 >> 1, i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}}

What is the result of attempting to compile and run the program?

a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,7fffffff
c. Prints: ffffffff,7fffffff,ffffffff
d. Prints: ffffffff,7ffffffe,7ffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,7fffffff
g. Prints: fffffffe,7fffffff,ffffffff
h. Prints: fffffffe,7fffffff,7fffffff
i. Run-time error
j. Compile-time error
k. None of the above
ANSWER
1 f Prints: If the left-hand operand of a shift operator, <<, >>
fffffffe,ffffffff,7fffffff and >>>, is of type int, then the shift distance is
always within the range of 0 to 31, inclusive; and is
specified by the least significant 5 bits of the right hand
operand. Similarly, if the left-hand operand of a shift
operator is of type long, then the shift distance is
always within the range of 0 to 63, inclusive; and is
specified by the least significant 6 bits of the right hand
operand. The left shift operator, <<, shifts each bit of the
left operand to the left a distance specified by the shift
distance. A number of bits that is equal to the shift
distance are shifted out of the left-most bit position and
are lost. A number of bits that is equal to the shift
distance are shifted in at the right. The signed right shift
operator, >>, shifts each bit of the left operand to the
right a distance specified by the shift distance. A number
of bits that is equal to the shift distance are shifted out of
the right-most bit position and are lost. A number of bits
that is equal to the shift distance are shifted in at the left.
The value of each bit that is shifted in at the left is equal
to the value of the sign bit. The signed right shift
operator maintains the sign of the left operand. The
unsigned right shift operator, >>>, is similar to the
signed right shift operator except for the fact that each bit
shifted in at the left is zero.

Question 2
class EBH201 {
public static void main (String[] args) {
int a = 1 || 2 ^ 3 && 5;
int b = ((1 || 2) ^ 3) && 5;
int c = 1 || (2 ^ (3 && 5));
System.out.print(a + "," + b + "," + c);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0
b. Prints: 0,0,3
c. Prints: 0,3,0
d. Prints: 0,3,3
e. Prints: 3,0,0
f. Prints: 3,0,3
g. Prints: 3,3,0
h. Prints: 3,3,3
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
Compile-time Both operands of the conditional and operator and the conditional
2 j
error or operator must be of type boolean.

Question 3
class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The conditional and expression is not evaluated, because the left
hand operand of the conditional or expression is true. The
original expression is as follows: x = (a = true) || (b
= true) && (c = true). The left hand operand of the
conditional or expression is the result of the assignment
Prints:
3 e expression, (a = true). Since the left hand operand of the
true,false,false
conditional or expression is true, the right hand operand will
not be evaluated. In this case, the right hand operand is the
conditional and expression. Consequently, neither operand of
the conditional and expression is evaluated and the variables, b
and c, maintain their default values of false.

Question 4
class EBH203 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = a || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The right hand operand of the conditional or operator is
evaluated only if the left hand operand is false. The right hand
operand of the conditional and operator is only evaluated if the
Prints: left hand operand is true. In this case, the left hand operand of
4 d
false,true,true the conditional or operator is false, so the right hand operand
must also be evaluated. The left hand operand of the conditional
and operator is the result of the conditional or expression, true,
so the right hand operand is evaluated.

Question 5
class EBH011 {
public static void main (String[] args) {
float a = Float.POSITIVE_INFINITY;
double b = Double.POSITIVE_INFINITY;
double c = Double.NaN;
System.out.print((a == b)+","+(c == c)+","+(c != c));
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The positive infinity of type float is promoted to the positive
Prints:
5 f infinity of type double. NaN is not equal to anything
true,false,true
including itself.
Question 6
class EBH012 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Run-time error
f. Compile-time error
g. None of the above

ANSWER
Prints: The sign of an integral numeric type is changed by inverting all of
6 d
true,true the bits and by adding one.

Question 7
class EBH013 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((~x == -x - 1)+","+(~y == -y - 1));
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Run-time error
f. Compile-time error
g. None of the above

ANSWER
The bitwise complement operator produces the same result as
Prints: changing the sign and subtracting one. Please note that the operand
7 d
true,true must be an integral type. The bitwise complement operator can not be
applied to a floating-point value.
Question 8
class EBH014 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((y % x) + ",");
System.out.print(y == ((y/x)*x + (y%x)));
}}

What is the result of attempting to compile and run the program?

a. Prints: 1,true
b. Prints: 2,true
c. Prints: 1,false
d. Prints: 2,false
e. Run-time error
f. Compile-time error
g. None of the above

ANSWER
Suppose the left operand were divided by the right operand. The
Prints: remainder operator returns the remainder of the division operation. For
8 b
2,true integral types, the identity, (y == ((y/x)*x+(y%x))), is always
true.

Question 9
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color color1 = new Red(); Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b2 = color1 instanceof Blue;
boolean b3 = color2 instanceof Blue;
System.out.print(b1+","+b2+","+b3);
}}

What is the result of attempting to compile and run the program?

a. false,false,false
b. false,false,true
c. false,true,false
d. false,true,true
e. true,false,false
f. true,false,true
g. true,true,false
h. true,true,true
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The type of the reference color2 is Red. Since Red is not a
subclass or a superclass of Blue, the expression color2
instanceof Blue is rejected at compile-time. Please note: The
expression, x instanceof T, produces a compile-time error
whenever the cast expression (T)x produces a compile-time error. If
the program had been able to compile and run, the expression
color1 instanceof Color would evaluate to true at run-
time. The reference color1 refers to an instance of type Red. Since
Compile-
9 j Red is a subclass of Color, the expression color1 instanceof
time error
Color would evaluate to true at run-time. The expression, color1
instanceof Blue would evaluate to false at run-time. The
reference, color1, is of type Color. Since Color is a superclass
of Blue, the expression, color1 instanceof Blue, is
accepted at compile-time. The type of the object instance referenced
by color1 is Red. Since Red is not Blue or a subclass of Blue,
the expression, color1 instanceof Blue, would be false at
run-time.

Question 10
class EBH020 {
public static void main (String[] args) {
int a = 1 | 2 ^ 3 & 5;
int b = ((1 | 2) ^ 3) & 5;
int c = 1 | (2 ^ (3 & 5));
System.out.print(a + "," + b + "," + c);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0
b. Prints: 0,0,3
c. Prints: 0,3,0
d. Prints: 0,3,3
e. Prints: 3,0,0
f. Prints: 3,0,3
g. Prints: 3,3,0
h. Prints: 3,3,3
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
Java evaluates operands from left to right while respecting operator
precedence. The order of operator precedence starting with the lowest is
Prints:
10 f as follows: |, ^, &. Although complete memorization of the operator
3,0,3
precedence chart is not necessary, it is a good idea to memorize the
three levels that appear in this question.

Question 11
class EBH025 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 33;
int i3 = i1 << (33 & 0x1f);
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3));
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Prints: 0,fffffffe
c. Prints: 0,ffffffff
d. Prints: ffffffff,ffffffff
e. Prints: ffffffff,fffffffe
f. Prints: fffffffe,ffffffff
g. Prints: fffffffe,fffffffe
h. Run-time error
i. Compile-time error
j. None of the above

ANSWER
11 g Prints: For each of the three shift operators, <<, >> and >>>, the
fffffffe,fffffffe shift distance is specified by the right hand operand. If the left
operand is of type int, then the shift distance is always within
the range 0 to 31, inclusive; and the following expression is
always true: (int1 << shift) == (int1 <<
(shift & 0x1f)). The hexadecimal representation of
decimal 31 is 0x1f and the binary representation is 0001 1111.
The hexadecimal representation of decimal 33 is 0x21 and the
binary representation is 0010 0001. The expression i1 <<
(33 & 0x1f) is equivalent to (0xffffffff << (0x21
& 0x1f)). Evaluation of the right hand operand of the shift
operator produces (0xffffffff << 1). The final result is
0xfffffffe. Similarly, if the left operand is of type long,
then the shift distance is always within the range 0 to 63,
inclusive; and the following expression is always true:
(long1 << shift) == (long1 << (shift &
0x3f)).

Question 12
class EBH001 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(1) - m(2) + m(3) * m(4));
}}

What is the result of attempting to compile and run the program?

a. Prints: 1, 2, 3, 4, 8,
b. Prints: 1, 2, 3, 4, 11,
c. Prints: 3, 4, 1, 2, 11,
d. Run-time error
e. Compile-time error
f. None of the above

ANSWER
The expression can be simplified as follows: j = 1 - 2 + 3 *
4 = 11. The original expression is as follows: m(m(1) - m(2)
Prints: 1, + m(3) * m(4)). Simplification step one. Evaluate each operand
12 b 2, 3, 4, from left to right: m(1 - 2 + 3 * 4). Step two. Add parentheses
11, to indicate operator precedence: m(1 - 2 + (3 * 4)). Step
three. Evaluate the inner-most parentheses: m(1 - 2 + 12). Step
four: Work through the expression from left to right. j = 11.

Question 13
class EBH002 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(1) + m(2) % m(3) * m(4));
}}
What is the result of attempting to compile and run the program?

a. Prints: 1, 2, 3, 4, 0,
b. Prints: 1, 2, 3, 4, 3,
c. Prints: 1, 2, 3, 4, 9,
d. Prints: 1, 2, 3, 4, 12,
e. Prints: 2, 3, 4, 1, 9,
f. Prints: 2, 3, 4, 1, 3,
g. Run-time error
h. Compile-time error
i. None of the above

ANSWER
The expression can be simplified as follows: 1 + 2 % 3 * 4 =
9. The original expression is as follows: m(m(1) + m(2) % m(3)
* m(4)). Simplification step one. Evaluate each operand from left to
Prints: 1,
right: m(1 + 2 % 3 * 4). Step two. Add parentheses to indicate
13 c 2, 3, 4,
operator precedence and associativity: m(1 + ((2 % 3) * 4).
9,
Step three. Evaluate the inner-most parentheses: m(1 + (2 * 4)).
Step four. Evaluate the inner-most parentheses: m(1 + 8). The
result is 9.

Question 14
class EBH005 {
public static void main (String[] s) {
byte b = 127; b <<= 2;System.out.println(b);
}}

What is the result of attempting to compile and run the program?

a. Prints: -4
b. Prints: -3
c. Prints: -2
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 508
h. Run-time error
i. Compile-time error
j. None of the above
ANSWER
14 a Prints: If the left-hand operand of the shift operator is of type byte, short,
or char then the left operand is promoted to a 32 bit int and all four
bytes are shifted. When a variable of type int with a value of 127 is
shifted two bits to the left, the result is 508. The compound assignment
operator includes an implicit cast to the type of the left-hand operand.
The expression, E1 op= E2, is equivalent to E1=(T)((E1) op
-4
(E2)), where T is the type of the left hand operand. Therefore, when
508 is cast to an eight bit byte, the three most significant bytes (24 bits)
are discarded leaving only the least significant byte (8 bits). The result is
the binary value, 11111100, which is the two's complement
representation of negative four.

Question 15
class EBH007{
public static void main (String[] s) {
byte b = 5; System.out.println(b<<33);
}}

What is the result of attempting to compile and run the program?

a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Prints: 10
f. Run-time error
g. Compile-time error
h. None of the above

ANSWER
If the left-hand operand of the shift operator is of type byte, short,
or char then the left operand is promoted to a 32 bit int and all four
bytes are shifted. If the promoted type of the left-hand operand is of
type int, then the shift distance is always within the range of 0 to 31,
Prints:
15 e inclusive; and is specified by the least significant 5 bits of the right-
10
hand operand. In this case, the shift distance is 33, and the five least
significant bits are 00001; so the shift distance is one bit. Note: If the
type of the left hand operand is long, then the least significant six bits of
the right hand operand are used.

Question 16
class EBH015 {
public static void main (String[] args) {
System.out.print((new Object() instanceof Object)+",");
System.out.print((new Object() instanceof String)+",");
System.out.print((new String() instanceof Object));
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The left operand of the instanceof operator must be null
or a reference to an instance of an Object or a subclass of
Object. The right operand of the instanceof operator must
Prints: be a class type, interface type or array type. If the left operand is
16 f
true,false,true a reference to an instance of the type specified by the right
operand or if the left operand is a reference to an instance of a
subclass of the type specified by the right operand, then
instanceof returns true.

Question 17
class EBH101 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
int i = 1; m(m(++i) + m(i++) + m(-i) + m(i++));
}}

What is the result of attempting to compile and run the above program?

a. Prints: 1, 2, 3, 4, 10,
b. Prints: 1, 2, -3, 4, 4,
c. Prints: 2, 2, -3, -3, -2,
d. Prints: 2, 2, -3, 3, 4,
e. Prints: 2, 3, -3, -2, 0,
f. Prints: 2, 3, -3, 4, 6,
g. Prints: 2, 3, 4, 5, 14,
h. Run-time error
i. Compile-time error
j. None of the above

ANSWER
The expression can be simplified as follows: j = 2 + 2 + -3 +
Prints: 2, 3 = 4. The original expression is as follows: j = ++i + i++ +
17 d 2, -3, 3, -i + i++. Simplification step one. Evaluate the unary expressions
4, from left to right: j = 2 + 2 + -3 + 3. Step two. Complete the
evaluation of the simplified expression: j = 4.

Question 18
class EBH102 {
static int m(int i) {System.out.print(i + ","); return i;}
public static void main(String s[]) {
int i = 1, j = m(i++) + m(i++) * m(i++) + m(i++);
System.out.print(j % 5);
}}

What is the result of attempting to compile and run the above program?

a. Prints: 1,2,3,4,0
b. Prints: 1,2,3,4,1
c. Prints: 1,2,3,4,2
d. Prints: 1,2,3,4,3
e. Prints: 1,2,3,4,4
f. Prints: 1,2,3,4,5
g. Run-time error
h. Compile-time error
i. None of the above

ANSWER
18 b Prints: The expression can be simplified as follows: j = 1 + (2 * 3)
1,2,3,4,1 + 4 = 11. The original expression is as follows: j = m(i++)
+ m(i++) * m(i++) + m(i++). The method, m, prints and
then returns the value of the parameter, so the original expression is
equivalent to the following: j = i++ + i++ * i++ + i++.
Step one. Work through the expression from left to right to evaluate
the unary expressions: j = 1 + 2 * 3 + 4. Step two. Add
parentheses to indicate operator precedence: j = 1 + (2 * 3)
+ 4. Step three. Work through the simplified expression: j = 1 +
6 + 4 = 11. Step four. Evaluate the expression that is the
argument of the print method: j % 5 = 11 % 5 = 1.

Question 19
class EBH103 {
public static void main (String[] args) {
byte a = 1, b = 2, c = (byte)a++, d = (byte)++b, e = (byte)a + b;
System.out.print(c + d + e);
}}

What is the result of attempting to compile and run the above program?

a. Prints: 1 2 3
b. Prints: 6
c. Prints: 2 3 5
d. Prints: 10
e. Prints: 1 3 4
f. Prints: 8
g. Prints: 1 3 5
h. Prints: 9
i. Run-time error.
j. Compile-time error.
k. None of the above

ANSWER
The precedence of the cast operator is higher than the precedence of
the addition operator, so the cast applies only to variable a and not
to the result of the addition. Binary numeric promotion causes the
Compile-
19 j byte variables a and b to be promoted to type int before the
time error.
addition operation, and the result of the addition is also of type int.
The attempt to assign the int result to the byte variable e
generates a possible loss of precision error.

Question 20
class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}

What is the result of attempting to compile and run the program?


a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Run-time error
f. Compile-time error
g. None of the above

ANSWER
The right operand of the conditional or operator is evaluated only if
the left hand operand is false. In this case, the left operand of the
Prints: first conditional or operator is false so the right hand operand is
20 c
BFCTAT evaluated. No further evaluation of the expression is necessary so
the right hand operand of the second conditional or operator is not
evaluated.

Question 21
class EBH104 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
int i = 1; m(m(++i) - m(i++) + m(-i) * m(~i));
}}

What is the result of attempting to compile and run the above program?

a. Prints: 2, 2, -3, -4, 8,


b. Prints: 2, 2, -3, -4, 12,
c. Prints: 2, 3, -3, -4, 7,
d. Prints: 1, 1, 1, 1, 0,
e. Prints: 2, 2, -2, -2, 4,
f. Prints: 2, 3, -2, -2, 3,
g. Prints: -1, -2, 2, 2, 0,
h. Run-time error
i. Compile-time error
j. None of the above

ANSWER
21 b Prints: 2, The original expression is as follows: m(m(++i) - m(i++) +
2, -3, -4, m(-i) * m(~i)). The method, m, prints and then returns the
12, value of the parameter, so the original expression is equivalent to the
following: ++i - i++ + -i * ~i. We can use a simplification
process that evaluates the expression as follows. Step one. Work
through the expression from left to right to evaluate the unary
expressions: 2 - 2 + -3 * -4. Step two. Add the parentheses to
indicate operator precedence: 2 - 2 + (-3 * -4). Step three.
Evaluate the inner-most parentheses: 2 - 2 + 12. Step four.
Evalute the simplified expression to produce the result, 12. The
bitwise complement operator, ~, inverts each bit of the operand. To
avoid working in binary the same result can be obtained by changing
the sign of the operand and then subtracting 1. The following identity
is always true ~x == -x - 1.

Question 22
class EBH105 {
static int m(int i) {System.out.print(i + ","); return 0;}
public static void main (String[] args) {
int i = 0; i = i++ + m(i); System.out.print(i);
}}

What is the result of attempting to compile and run the above program?

a. Prints: 0,0
b. Prints: 1,0
c. Prints: 0,1
d. Prints: 1,1
e. Run-time error
f. Compile-time error
g. None of the above

ANSWER
The expression, i = i++ + m(i), can be reduced to, i = 0 + 0.
The left operand of the addition expression is found to be zero, and the
value of variable i is then incremented to 1. The right hand operand of
the addition operation is evaluated next. The value, 1, is passed to
Prints:
22 b method m. After printing the argument value, method m returns the
1,0
value zero. The two operands of the addition operation are zero as is the
result of the addition. The zero value serves as the right hand operand
of the assignment statement, so the value, zero, is assigned to variable
i.

Question 23
class EBH106 {
public static void main(String args[]) {
int a = 1; a += ++a + a++; System.out.print(a);
}}
What is the result of attempting to compile and run the above program?

a. Prints: 3
b. Prints: 4
c. Prints: 5
d. Prints: 6
e. Prints: 7
f. Run-time error
g. Compile-time error
h. None of the above

ANSWER
The two statements, int a=1 followed by a += ++a + a++, can
be rewritten as the single statement, a=(int)((1)+(++a + a+
Prints: +)). Further evaluation produces a=(int)((1)+(2 + 2)).
23 c
5 Generally speaking, a compound assignment expression of the form E1
op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the
type of E1.

Question 24
class EBH107 {
static int m(int i) {System.out.print(i + ","); return i;}
public static void main(String s[]) {
int i=0, j = m(++i) + m(++i) * m(++i) % m(++i) + m(++i);
System.out.print(j%5);
}}

What is the result of attempting to compile and run the above program?

a. Prints: 1,2,3,4,5,0
b. Prints: 1,2,3,4,5,1
c. Prints: 1,2,3,4,5,2
d. Prints: 1,2,3,4,5,3
e. Prints: 1,2,3,4,5,4
f. Prints: 1,2,3,4,5,5
g. Run-time error
h. Compile-time error
i. None of the above

ANSWER
24 d Prints: The expression can be simplified as follows: j = 1 + ((2 *
1,2,3,4,5,3 3) % 4) + 5 = 8. The original expression is as follows: j =
++i + ++i * ++i % ++i + ++i. Step one. Evaluate the
unary expressions from left to right: j = 1 + 2 * 3 % 4 +
5. Step two. Add parentheses to indicate operator precedence: j =
1 + ((2 * 3) % 4) + 5. Step three. Evaluate the inner
most parentheses: j = 1 + (6 % 4) + 5. Repeat step three:
j = 1 + 2 + 5. Repeat step three: j = 8. The argument of
the print expression is: j%5. The result is: 8 % 5 = 3.

Question 25
class EBH108 {
public static void main(String s[]) {
int i=0, j = ++i + ((++i * ++i) % ++i) + ++i;
System.out.print(j%5);
}}

What is the result of attempting to compile and run the above program?

a. Prints: 1
b. Prints: 2
c. Prints: 3
d. Prints: 4
e. Prints: 5
f. Run-time error
g. Compile-time error
h. None of the above

ANSWER
The expression can be simplified as follows: j = 1 + ((2 * 3) %
4) + 5 = 8. The original expression is as follows: j = ++i +
((++i * ++i) % ++i) + ++i;. Step one. Evaluate the unary
expressions from left to right: j = 1 + ((2 * 3) % 4) + 5.
Prints:
25 c Step two. Evaluate the inner-most parentheses: j = 1 + (6 % 4)
3
+ 5. Step three: Evaluate the inner-most parentheses. j = 1 + 2 +
5. Step four: Work through the expression from left to right. j = 8.
The argument of the print expression is: j%5. The result is: 8 % 5 =
3.

Question 26
class EBH023 {
static String m1(boolean b){return b?"T":"F";}
public static void main(String [] args) {
boolean b1 = false?false:true?false:true?false:true;
boolean b2 = false?false:(true?false:(true?false:true));
boolean b3 = ((false?false:true)?false:true)?false:true;
System.out.println(m1(b1) + m1(b2) + m1(b3));
}}

What is the result of attempting to compile and run the program?

a. Prints: FFF
b. Prints: FFT
c. Prints: FTF
d. Prints: FTT
e. Prints: TFF
f. Prints: TFT
g. Prints: TTF
h. Prints: TTT
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The expression used to assign variable b1 is equivalent to the
Prints:
26 b expression used to assign variable b2. The results demonstrate that the
FFT
conditional operator (?:) groups from right-to-left.

Question 27
class EBH024 {
public static void main(String[] args) {
int i1 = 15;
String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
String b2 = (i1>30)?"Red":((i1>20)?"Green":
((i1>10)?"Blue":"Violet"));
System.out.println(b1 + "," + b2);

}}

What is the result of attempting to compile and run the program?

a. Prints: Red,Red
b. Prints: Green,Green
c. Prints: Blue,Blue
d. Prints: Violet,Violet
e. Prints: Blue,Violet
f. Prints: Violet,Blue
g. Prints: Blue,Green
h. Prints: Green,Blue
i. Run-time error
j. Compile-time error
k. None of the above

ANSWER
The expression used to assign variable b1 is equivalent to the
Prints:
27 c expression used to assign variable b2. The results demonstrate
Blue,Blue
that the conditional operator (?:) groups from right-to-left.

Question 28
class EBH003 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}

What is the result of attempting to compile and run the program?

a. Prints: -2, 3, 0, 3, 0, 6,
b. Prints: -2, 3, 0, 2, 1, 4,
c. Prints: -2, 3, 0, 2, 2, 5,
d. Prints: -2, 3, 0, 3, 2, 6,
e. Prints: -1, 3, 0, 3, 2, 7,
f. Prints: -2, 0, 3, 3, 0, 6,
g. Prints: -1, 0, 3, 2, 1, 4,
h. Prints: -2, 0, 3, 2, 2, 5,
i. Prints: -2, 0, 3, 3, 2, 6,
j. Prints: -1, 0, 3, 3, 2, 7,
k. Run-time error
l. Compile-time error
m. None of the above

ANSWER
The expression can be simplified as follows: -2+3+0+2+2=5. The
original expression is as follows: m(m(~1) + m(1|2) +
m(1&2) + m(1^3) + m(1<<1)). Expr 1: ~1 = -1 - 1 =
Prints: -2,
-2. Expr 2: 1|2 = 0001 | 0010 = 0011 = 3. Expr 3: 1&2
28 c 3, 0, 2, 2,
= 0001 & 0010 = 0000. Expr 4: 1^3 = 0001 ^ 0011 =
5,
0010 = 2. Expr 5: 1<<1 = 0001 << 1 = 0010 = 2. Note:
The bitwise expressions were demonstrated using only four bits of
the 32 bit int type values.
Question 29
class EBH021 {
public static void main(String[] args) {
System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Prints: -1,4
c. Prints: 0x1f,8
d. Prints: 31,16
e. Run-time error
f. Compile-time error
g. None of the above
ANSWER
Prints 31,0. The expression (-1 & 0x1f) is equal to
(0xffffffff & 0x1f), and both are equal to the hex value
0x1f or decimal 31. The expression (8 << -1) is equivalent to
(8 << 0xffffffff). If the left hand operand of a shift
expression is of type int, then the right hand operand is implicitly
None of
masked with the value 0x1f. In other words, the expression (8 <<
29 g the
-1) is equivalent to (8 << (-1 & 0x1f)). By replacing -1 with
above
the hexadecimal representation we have (8 << (0xffffffff &
0x1f)). By evaluating the right hand operand we have (8 <<
31). When 8 is shifted 31 bits to the left, the result is zero since the
only non-zero bit is lost as it is shifted beyond the most significant bit
of the int data type.

Вам также может понравиться