Sunteți pe pagina 1din 12

Operator & Assignment

Question 1
class GFC404 {
private static int x=1;
static void m1(int x, int y) {x++; y++;}
public static void main (String[] args) {
int y=3; m1(x, y);
System.out.println(x + "," + y);
}}
What is the result of attempting to compile and run the program?
a. Prints: 1,3 e. Run-time error
b. Prints: 2,3 f. Compile-time error
c. Prints: 1,4 g. None of the above
d. Prints: 2,4
Answer:
Variables of primitive type are passed to methods by value: Only a copy of the
value of the variable is passed to the method. While the method works with a
Prints: local copy of the variable, the original variable remains unchanged by any
a actions performed on the method parameter. For that reason, method m1 does
1,3
not change the contents of the variable y in the main method or the class
variable x.

Question 2
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 e. Prints: true,false,false i. Run-time error
b. Prints: false,false,true f. Prints: true,false,true j. Compile-time error
c. Prints: false,true,false g. Prints: true,true,false k. None of the above
d. Prints: false,true,true h. Prints: true,true,true
Answer:
The positive infinity of type float is promoted to the positive
Prints:
f infinity of type double. NaN is not equal to anything including
true,false,true
itself.

Question 3
class A {
public static void main(String[] args) {
char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
System.out.print(a + b + "" + a + b);
}}What is the result of attempting to compile and run the program?
a. Prints: 390 d. Prints: ab195 g. Compile-time error
b. Prints: 195195 e. Prints: abab h. None of the above
c. Prints: 195ab f. Run-time error

Answer:
Both operands of the first addition operator are promoted from type char to
int, and are evaluated as integral numeric values. The right hand operand of
Prints: the second addition operator is of type String, so the result of the first
c
195ab addition operator is converted to type String, and is concatenated with the
right hand operand. As evaluation of the expression continues from left to
right, the remaining operands are also converted to type String.

Question 4
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 d. Prints: true,true g. None of the above
b. Prints: false,true e. Run-time error
c. Prints: true,false f. Compile-time error
Answer:
Prints: The sign of an integral numeric type is changed by inverting all of the
d
true,true bits and by adding one.

Question 5
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 d. Prints: 5 g. Compile-time error
b. Prints: 0 e. Prints: 10 h. None of the above
c. Prints: 1 f. Run-time error
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
Prints: distance is always within the range of 0 to 31, inclusive; and is specified by the
e
10 least significant 5 bits of the right-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 6
class Sienna {
static double a; static float b; static int c; static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}}
What is the result of attempting to compile and run the program?
a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above
Answer:
The literal, 'a', is promoted to type int; and is then multiplied by the value
Prints: of the left operand, 4. If one of the two operands of a numeric expression is of
a
true type int, then the other operand will be promoted to type int if it is of type
short, char or byte.

Question 7
interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver {
public static void main(String []args) {
Base[] base = {new Base()};
Sub sub[] = new Sub[1]; // 1
Object obj = base; // 2
sub = (Sub[])obj; // 3
I1 []i1 = (I1[])obj; // 4
}}
What is the result of attempting to compile and run the program?
a. Compile-time error at line 1 f. Run-time error at line 3
b. Run-time error at line 1 g. Compile-time error at line 4
c. Compile-time error at line 2 h. Run-time error at line 4
d. Run-time error at line 2 i. None of the above
e. Compile-time error at line 3
Answer:
Base is the superclass of type Sub, so a reference to an actual instance of
type Base can not be cast to type Sub. Therefore, a reference to an array
instance of type Base[] can not be cast to type Sub[]. The type of the
Run-time
reference, obj, is Object. Type Sub[] is a subclass of Object. The
f error at line
compiler accepts the cast, because the actual instance referenced at run-
3
time might be of type Sub[]. In this case, the actual type of the instance
referenced by obj at run-time is found to be type Base[], so the result
is a run-time error.

Question 8
class White {
public static void main(String args[]) {
int[] i = {1,2,3,4,5}; // 1
long[] l1 = new long[5]; // 2
long []l2 = l1; // 3
long l3[] = (long[])i; // 4
long l4[] = new long[5]; // 5
l4[1] = i[1]; // 6
}}
A compile-time error is generated at which line?
a. 1 d. 4 g. None of the above
b. 2 e. 5
c. 3 f. 6
Answer:
d 4 An array of primitive type can not be cast to an array of a different primitive type.

Question 9
class GFC200 {}
class GFC201 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}
}
What is the result of attempting to compile and run the program?
a. Prints: Object
b. Prints: String
c. Prints: GFC200
d. Compile-time error
e. Run-time error
f. None of the above
Answer:
The type of the argument is null and could be converted to any of the
types Object, String or GFC200, by method invocation conversion.
All three methods are applicable, but none of the three is more specific than
both of the other two. The ambiguity results in a compile-time type error. If
type GFC200 were a subclass of type String; then any argument that
Compile- could be pass to m(GFC200 x) could also be passed to m(String x)
d
time error without causing a compile-time type error, and we could say that
m(GFC200 x) is more specific than m(String x). Since GFC200 is
not a subclass of type String, a method invocation conversion is not able
to widen an argument of type GFC200 to match a method parameter of
type String, so m(GFC200 x) is not more specific than m(String
x).

Question 10
class UltraViolet {
public static void main (String[] args) {
char a = '\u002a', b = '\u0024'; // a = Asterisk *; b = Dollar Sign
$
System.out.print(a + b); // 1
System.out.print(" ABC" + a + b); // 2
}}
What is the result of attempting to compile and run the program?
a. Prints: 78 ABC*$
b. Prints: *$ ABC*$
c. Prints: 78 ABC78
d. Compile-time error
e. Run-time error
f. None of the above
Answer:
When char variables a and b are converted to String types they are
printed as *$. When not converted to String types they are promoted to
type int, and are printed as the numeric sum of 0x2a and 0x24. At line 1,
the expression, a + b, can be evaluated as follows: 0x2a + 0x24 = 42
Prints: 78 + 36 = 78. At line 2, the first operand of the expression, " ABC" + a
a
ABC*$ + b, is of type String. Since one operand of the first addition operator is
of type String the other operand must be converted to type String: ("
ABC" + "*") + b = " ABC*" + b. The process is repeated for the
second addition operation: " ABC*" + b = " ABC*" + "$" = "
ABC*$"

Question 11
class GFC202 {} class GFC203 extends GFC202 {}
class GFC204 {
static void m(GFC202 x) {System.out.print("GFC202");}
static void m(GFC203 x) {System.out.print("GFC203");}
public static void main(String[] args) {m(null);}
}
What is the result of attempting to compile and run the program?
a. Prints: GFC202
b. Prints: GFC203
c. Compile-time error
d. Run-time error
e. None of the above
Answer:
The type of the argument is null and could be converted to either type
GFC202 or GFC203 by method invocation conversion; so both methods are
applicable. The more specific of the two, m(GFC203 x), is chosen. Type
Prints:
b GFC203 is a subclass of type GFC202; so any argument that can be passed
GFC203
to m(GFC203 x) can also be passed to method m(GFC202 x) without
causing a compile-time type error; therefore, we can say that method
m(GFC203 x) is more specific than m(GFC202 x).

Question 12
class GFC305 {
static void m1(int[] i1, int[] i2) {
int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
What is the result of attempting to compile and run the program?
a. Prints: 1,1 d. Prints: 3,3 g. None of the above
b. Prints: 1,3 e. Run-time error
c. Prints: 3,1 f. Compile-time error
Answer:
Method m1 is not able to change the value of the local variables that are
Prints: declared in the main method and serve as the arguments in the method
c
3,1 invocation expression. However, method m1 is able to modify the contents of
the arrays that are referenced by the method parameters.

Question 13
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 d. Prints: 1,1 g. None of the above
b. Prints: 1,0 e. Run-time error
c. Prints: 0,1 f. Compile-time error
Answer:
b Prints: The expression, i = i++ + m(i), can be reduced to, i = 0 + 0. The left
1,0 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 method m. After printing the argument
value, method m returns the 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 14
class GFC306 {
static int[] i1 = {1}, i2 = {3};
static void m1(int[] i1) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}}
What is the result of attempting to compile and run the program?
a. Prints: 1,1 d. Prints: 3,3 g. None of the above
b. Prints: 1,3 e. Run-time error
c. Prints: 3,1 f. Compile-time error
Answer:
The method m1 is invoked by the method invocation expression m1(i1). The
argument i1 denotes the static member variable i1. Inside the declaration of
method m1, the method parameter i1 shadows the static member variable i1.
The assignment expression i1 = i2 assigns the value of the member variable
i2 to the method parameter i1, but the member variable i1 remains
unchanged. Inside of method m1, the member variable i2 is not shadowed; so
Prints:
a the assignment expression i2 = i3 assigns the reference value contained by
1,1
the method local variable i3 to the member variable i2. This question
demonstrates that argument values are passed to method parameters by value,
and the method parameter is only a copy of the argument value. A change made
to the method parameter does not change the value of any variable that is
shadowed by the parameter and does not change the value of the argument
appearing in the method invocation expression.

Question 15
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 d. Prints: 6 g. Compile-time error
b. Prints: 4 e. Prints: 7 h. None of the above
c. Prints: 5 f. Run-time error
Answer:
c Prints: The two statements, int a=1 followed by a += ++a + a++, can be
5 rewritten as the single statement, a=(int)((1)+(++a + a++)). Further
evaluation produces a=(int)((1)+(2 + 2)). 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 16
class Amber {
public static void main(String[] args) {
int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
Object[] obj = (Object[])a.clone(); // 2
for(int i = 0; i < obj.length; i++) { // 3
int[] ia = (int[])obj[i]; // 4
System.out.print(ia[i]); // 5
}}}
A compile-time error is generated at which line?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Answer:
The program compiles and runs without error and prints 112. It is necessary to
remember that arrays are Cloneable objects. Furthermore, a two
dimensional array is also a single dimensional array of single dimensional
None of arrays. At line 2, a two dimensional array of int primitives is converted to a
f the single dimensional array with components of type Object where each
above Object is a single dimensional array. The loop iterates through each element
of the Object array. Since each element is actually a reference to a single
dimensional array of integer primitives a conversion from type Object to an
int array is legal.

Question 17
class GFC307 {
static void m1(int[] i1, int[] i2) {
i1 = i2 = null;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
What is the result of attempting to compile and run the program?

a. Prints: 0,0 d. Prints: 3,1 g. Compile-time error


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

Answer:
Although the reference parameters i1 and i2 are reassigned inside of m1, the
Prints:
c change has no impact outside of m1. Array references are passed by value: the
1,3
invoked method gets a copy of the array reference.

Question 18
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 d. Prints: 1,2,3,4,5,3 g. Run-time error
b. Prints: 1,2,3,4,5,1 e. Prints: 1,2,3,4,5,4 h. Compile-time error
c. Prints: 1,2,3,4,5,2 f. Prints: 1,2,3,4,5,5 i. None of the above
Answer:
d Prints: The expression can be simplified as follows: j = 1 + ((2 * 3) %
1,2,3,4,5,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.

S-ar putea să vă placă și