Você está na página 1de 19

Operators & Assignments

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 Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}}

Compile-time errors are generated at which lines?


a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8

ANSWER
This question demonstrates a variety of assignment conversions.
The compiler will implicitly do a narrowing conversion for an
assignment statement if the right hand operand is a compile time
constant of type byte, short, char, or int and the value
falls within the range of the variable on the left and if the variable
c 3
2 on the left is of type byte, short, or char. In this case,
d 4
variables s1 and c1 are not compile time constants so the
compiler will not do an implicit narrowing conversion. However,
variables s2 and c2 are compile time constants that fall within
the range of the left hand operand. For more information, please
see JLS section 5.2.

Question 3

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 changing the sign and subtracting one. Please
Prints:
3d note that the operand must be an integral type. The
true,true
bitwise complement operator can not be applied to a
floating-point value.

Question 4

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.
Prints: The remainder operator returns the remainder of the division
4b
2,true operation. For integral types, the identity, (y ==
((y/x)*x+(y%x))), is always true.

Question 5

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
5 j Compile- The type of the reference color2 is Red. Since Red is
time error 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 Red is a subclass of Color,
the expression color1 instanceof 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 6

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

Question 7

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
7 g Prints: For each of the three shift operators, <<, >> and
fffffffe,fffffffe >>>, the 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 8

class GFC205 {} class GFC206 extends GFC205 {}


class GFC207 extends GFC206 {
static void m(GFC205 x, GFC205 y)
{System.out.print("GFC205,GFC205");}
static void m(GFC205 x, GFC206 y)
{System.out.print("GFC205,GFC206");}
static void m(GFC206 x, GFC205 y)
{System.out.print("GFC206,GFC205");}
static void m(GFC206 x, GFC206 y)
{System.out.print("GFC206,GFC206");}
public static void main(String[] args) {
GFC207 gfc207 = new GFC207(); m(gfc207,
gfc207);
}}

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

a. Prints: GFC205,GFC205
b. Prints: GFC205,GFC206
c. Prints: GFC206,GFC205
d. Prints: GFC206,GFC206
e. Compile-time error
f. Run-time error
g. None of the above
ANSWER
Type GFC207 is a subclass of types GFC206
and GFC205, so any of the four methods are
applicable to the method invocation expression,
m(gfc207, gfc207). The most specific of
the four, m(GFC206 x, GFC206 y), is
Prints: chosen. Type GFC206 is a subclass of type
8d
GFC206,GFC206 GFC205, and method m(GFC206 x,
GFC206 y) is more specific than the other
three, because any invocation of m(GFC206
x, GFC206 y) could also be handled by any
of the other three without causing a compile-
time type error.

Question 9

class GFC211 {} class GFC212 extends GFC211 {}


class GFC213 extends GFC212 {
static void m(GFC211 x, GFC211 y)
{System.out.print("GFC211,GFC211");}
static void m(GFC211 x, GFC212 y)
{System.out.print("GFC211,GFC212");}
static void m(GFC212 x, GFC211 y)
{System.out.print("GFC212,GFC211");}
static void m(GFC212 x, GFC212 y)
{System.out.print("GFC212,GFC212");}
static void m(GFC211 x, GFC213 y)
{System.out.print("GFC211,GFC213");}
public static void main(String[] args) {
GFC213 gfc213 = new GFC213(); m(gfc213,
gfc213);
}}

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

a. Prints: GFC211,GFC211
b. Prints: GFC211,GFC212
c. Prints: GFC212,GFC211
d. Prints: GFC212,GFC212
e. Prints: GFC211,GFC213
f. Compile-time error
g. Run-time error
h. None of the above

ANSWER
The method invocation expression, m(gfc213,
gfc213), is ambiguous; because, no applicable method
is more specific than all of the others. Method
m(GFC212 x, GFC212 y) is more specific than
m(GFC212 x, GFC211 y), because any invocation
of m(GFC212 x, GFC212 y) could also be handled
Compile- by m(GFC212 x, GFC211 y) without causing a
9f
time error compile-time type error. However, some invocations of
m(GFC212 x, GFC212 y) can not be handled by
m(GFC211 x, GFC213 y), so m(GFC212 x,
GFC212 y) is not more specific than m(GFC211 x,
GFC213 y). Furthermore, not all invocations of
m(GFC211 x, GFC213 y) could be handled by
m(GFC212 x, GFC212 y).

Question 10

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 be a
Prints: class type, interface type or array type. If the left
10 f
true,false,true operand is 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 11

class GFC214 {
static void m1(boolean b1)
{System.out.print("boolean ");}
static void m1(byte b1) {System.out.print("byte
");}
static void m1(short s1) {System.out.print("short
");}
static void m1(char c1) {System.out.print("char
");}
static void m1(int i1) {System.out.print("int
");}
public static void main(String[] args) {
byte b1; m1(b1 = 1); m1(b1); m1(b1 == 1);
}}

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


a. Prints: byte byte byte
b. Prints: byte byte boolean
c. Prints: int int int
d. Compile-time error
e. Run-time error
f. None of the above

ANSWER
Variable b1 was initialized by the first method
invocation statement, so the second method invocation
statement does not result in a compile-time error. The
assignment expression, b1 = 1, initializes variable b1
with the value 1, and the same value is passed as an
Prints: byte argument to method m1(byte b1). The method
11 b byte invocation expression, m1(b1), invokes the same
boolean
method, m1(byte b1). The argument of the third
method invocation expression, m1(b1 == 1), is the
result of the equality expression, b1 == 1. The type
of the result and the argument is boolean, so the
invoked method is m1(boolean b1).

Question 12

class GFC308 {
int[] i1 = {1}, i2 = {3};
void m1() {
m2(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}
void m2(int[] i1, int[] i2) {
int[] i3 = i1;
this.i1 = i2;
this.i2 = i3;
}
public static void main (String[] args) {
new GFC308().m1();
}}

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


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

ANSWER
Inside of method m2, the local variables i1 and i2
Prints:
12 d remain unchanged while the shadowed instance variables
3,1
are changed.

Question 13

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
13 c Prints: The expression can be simplified as follows: j = 1 +
3 ((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. Step two.
Evaluate the inner-most parentheses: j = 1 + (6 % 4)
+ 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 14

class A {} class B extends A {}


class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
A a1; B b1; m(null,null); m(a1=null,b1=null);
m(b1, a1);
}}

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

a. Prints: BBABAB
b. Prints: BBABBA
c. Prints: BBBBAB
d. Prints: BBBBBA
e. Prints: BBBBBB
f. Compile-time error
g. Run-time error
h. None of the above

ANSWER
14 b Prints: Type B is a subclass of type A, and method m(B x, B
BBABBA y) is more specific than the other three; because any
invocation of it could be handled by any of the other
three without causing a compile-time type error. All
four methods are applicable to the first method
invocation expression, m(null,null). The most
specific method, m(B x, B y), is chosen; and both
arguments are converted to type B. In the second
method invocation expression,
m(a1=null,b1=null), simple assignment
expressions initialize the local variables a1 and b1
with null references of types A and B respectively.
The invoked method is m(A x, B y). In the third
method invocation expression, the positions of the
arguments are reversed relative to the previous
invocation: The type of the first argument is now B and
the second is A. The invoked method is m(B x, A
y).

Question 15

class A {} class B extends A {}


class C extends B {
static void m1(A x) {System.out.print("m1A");}
static void m2(B x) {System.out.print("m2B");
m1(x);}
static void m2(A x) {System.out.print("m2A");
m1(x);}
static void m3(C x) {System.out.print("m3C");
m2(x);}
static void m3(B x) {System.out.print("m3B");
m2(x);}
static void m3(A x) {System.out.print("m3A");
m2(x);}
public static void main(String[] args) {m3(new
C());}
}

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

a. Prints: m3Am2Am1A
b. Prints: m3Bm2Bm1A
c. Prints: m3Cm2Bm1A
d. Prints: m3Cm2Am1A
e. Compile-time error
f. Run-time error
g. None of the above

ANSWER
The method invocation expression, m3(new
C()), invokes method m3(C x), because the
argument type matches the parameter type of the
method declaration exactly. Method m3 uses the
parameter as the argument of the next invocation
Prints: expression, m2(x). Of the two overloaded
15 c
m3Cm2Bm1A versions of m2, the most specific is invoked,
m2(B x). Type B is a subclass of A, so any
invocation of m2(B x) could be handled by
m2(A x) without causing a compile-time type
error. For that reason, m2(B x) is more specific
than m2(A x).

Question 16

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
Prints: the expression used to assign variable b2. The results
16 b
FFT demonstrate that the conditional operator (?:) groups from
right-to-left.

Question 17

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
Prints: equivalent to the expression used to assign variable
17 c
Blue,Blue b2. The results demonstrate that the conditional
operator (?:) groups from right-to-left.

Question 18

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
18 g None of Prints 31,0. The expression (-1 & 0x1f) is equal to
the (0xffffffff & 0x1f), and both are equal to the hex
above 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 masked with the value 0x1f.
In other words, the expression (8 << -1) is equivalent
to (8 << (-1 & 0x1f)). By replacing -1 with 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.

Você também pode gostar