Operator & Assignment Question 1 class GFC402 { static int x=1; void m1(int i) {x++; i++;} public static void main (String[] args) { int y=3; m1(y); System.out.println(x + "," + y); }} What is the result of attempting to compile and run the program?
a. Prints: 1,3 b. Prints: 2,3 c. Prints: 1,4 Answer:
d. Prints: 2,4 e. Run-time error f. Compile-time error
g. None of the above
Method m1 is an instance method, and must be invoked with reference to Compile-time f an instance of type GFC402. Method m1 can not be invoked from a static error context.
Question 2 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 g. Prints: true,true,false b. Prints: false,false,true h. Prints: true,true,true c. Prints: false,true,false i. Run-time error d. Prints: false,true,true j. Compile-time error e. Prints: true,false,false k. None of the above f. Prints: true,false,true 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 left hand operand is true. In this case, the left hand operand of the conditional or operator Prints: d false,true,true 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 3 class GFC102 { public static void main(String[] args) { byte b1 = -129; // 1 byte b2 = 127; // 2
short s1 = -32768; short s2 = 32768; char c1 = -1; char c2 = 65535;
// // // //
3 4 5 6
}} Compile-time errors are generated at which lines?
a. b. c. d. e. f.
1 2 3 4 5 6
Answer: a d 1 4 The minimum value of type byte is -128. The maximum value of type e 5 short is 32767. The minimum value of type char is 0.
Question 4 class GFC403 { private static int x=1; static void m1(int i) {x++; i++;} public static void main (String[] args) { int y=3; m1(y); System.out.println(x + "," + y); }} What is the result of attempting to compile and run the program?
a. Prints: 1,3 d. Prints: 2,4 g. None of the above b. Prints: 2,3 e. Run-time error c. Prints: 1,4 f. Compile-time error 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 local copy of the variable, the original variable remains unchanged by any Prints: actions performed on the method parameter. For that reason, method m1 does b 2,3 not change the value of the variable y in the main method. However, method m1 does have direct access to the class variable x and the content of the class variable is modified by method m1.
Question 5 class Maroon { public static void int a = 1; // short b = 1; // long c = 1; // a = c + a; // c = b + a; // }} A compile-time error
a. b. c. d. e. f.
1 2 3 4 5 None of the above
main (String[] args) { 1 2 3 4 5 is generated at which line?
Answer: The assignment expression, a = c + a, requires an explicit cast to type int. If one of the two operands of a numeric expression is of type long and if the other operand is of type int, short, char or byte; then it will be promoted to type d 4 long, and the result of the expression will be of type long. (Note: The rule does not apply to the shift operator.) The type long result can not be assigned to a variable of type int without an explicit cast.
Question 6 interface I1 {} interface I2 {} class Base implements I1 {} class Sub extends Base implements I2 {} class Yellow { public static void main(String args[]) { Base base = new Sub(); // 1 I1 i1 = base; // 2 Sub sub = (Sub)base; // 3 I2 i2 = (Sub)base; // 4 }} A compile-time error is generated at which line?
a. 1 b. 2 Answer:
c. 3 d. 4
e. None of the above
Although the reference named base is of type Base, the instance that it None of refers to is of type Sub, and the reference can be cast to type Sub. Since e the instances of type Sub implement both interfaces, I1 and I2, the Sub type above instances can be assigned to references of type I1 and I2 without an explicit cast.
Question 7 import java.io.Serializable; class Blue { public static void main (String args[]) { int[] i = {1,2,3}; // 1 Serializable s = i; // 2 i = (int [])s; // 3 }} What is the result of attempting to compile and run the program?
a. Compile-time error at line 1. e. Compile-time error at line 3. b. Run-time error at line 1. f. Run-time error at line 3. c. Compile-time error at line 2. g. None of the above d. Run-time error at line 2. Answer: None of the All array types implement the Serializable interface and may be g above assigned to a reference of type Serializable.
Question 8 class GFC217 { static String m(int i) {return "int";}
static String m(float i) {return "float";} public static void main (String[] args) { long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1));
}} What is the result of attempting to compile and run the program?
a. Prints: float,float e. Compile-time error b. Prints: float,double f. Run-time error c. Prints: double,float g. None of the above d. Prints: double,double Answer: The method invocation expression, m(b1), contains an argument of type double. A method invocation conversion will not implicitly narrow the Compile- argument to match the parameter type of the method, m(float i). The e time error method invocation expression, m(a1), contains an argument of type long. A method invocation conversion will widen the argument to match the parameter type of the the method, m(float i).
Question 9 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 f. Prints: 127 b. Prints: -3 g. Prints: 508 c. Prints: -2 h. Run-time error d. Prints: 0 i. Compile-time error e. Prints: 1 j. 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. 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 Prints: the type of the left-hand operand. The expression, E1 op= E2, is equivalent to a -4 E1=(T)((E1) op (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 10 class Teal { public static void main (String[] args) { byte b = 1; // 1 long l = 1000; // 2 b += l; // 3 }} A compile-time error is generated at which line?
a. 1 b. 2
c. 3 d. None of the above
Answer: The compound assignment operators include an implicit cast to the type of None of the left hand operand. The expression at line 3, b += l, does not require an d the above explicit cast to convert the right hand operand from type long to type byte.
Question 11 interface I1 {} interface I2 {} class Base implements I1 {} class Sub extends Base implements I2 {} class Gray { public static void main(String []args) { Base[] base = {new Base()}; // 1 Sub sub[] = {new Sub()}; // 2 Object obj = sub; // 3 base = obj; // 4 }} A compile-time error is generated at which line?
a. b. c. d. e.
1 2 3 4 None of the above
Answer: The Object referenced by obj is of type Sub[], and the reference, base, is of d 4 type Base[]. The assignment expression, base = obj requires an explicit cast to type Base[] as follows: base = (Base[])obj.
Question 12 class Black { public static void main(String args[]) { int[] i = {1,2,3,4,5}; // 1 long[] l = new long[5]; // 2 for (int j = 0; j < l.length(); j++) { // 3 l[j] = i[j]; // 4 }}} A compile-time error is generated at which line?
a. b. c. d. e.
1 2 3 4 None of the above
Answer: The length member of the array type is an attribute. A compile-time error is c 3 generated as a result of the attempt to access length as though it were a method.
Question 13 class GFC218 { static void m(Object x) {System.out.print("Object");} static void m(String x) {System.out.print("String");} public static void main(String[] args) {m(null);} }
What is the result of attempting to compile and run the program? a. b. c. d. e.
Prints: Object Prints: String Compile-time error Run-time error None of the above
Answer:
b
Prints: String
A method invocation conversion can widen an argument of type String to match a method parameter of type Object, so any argument that can be passed to m(String x) without generating a compile-time type error can also be passed to m(Object x). For that reason, we can say that m(String x) is more specific than m(Object x). The argument of the method invocation expression, m(null), is of type null and can be converted to either type String or Object by method invocation conversion, so both methods, m(String x) and m(Object x), are applicable. The more specific of the two, m(String x), is chosen over the less specific, m(Object x).
Question 14 class GFC303 { private String name; public GFC303(String name) {this.name = name;} public void setName(String name) {this.name = name;} public String getName() {return name;} public static void m1(GFC303 pet1, GFC303 pet2) { pet1 = new GFC303("Fish"); pet2 = null; } public static void main (String[] args) { GFC303 pet1 = new GFC303("Dog"); GFC303 pet2 = new GFC303("Cat"); m1(pet1,pet2); System.out.println(pet1.getName() + "," + pet2.getName()); }} What is the result of attempting to compile and run the program?
a. Prints: Dog,Cat d. Prints: Fish,Fish g. None of the above b. Prints: Dog,Fish e. Compile-time error c. Prints: Fish,Cat f. Run-time error Answer: The method m1 is invoked by the method invocation expression m1(pet1,pet2). A copy of the reference argument pet1 is assigned to the method parameter pet1. Inside the body of method m1, the assignment expression pet1 = new GFC303("Fish") assigns a reference to a new instance of GFC303 to the method parameter pet1; but the argument pet1 that appears in the method invocation expression m1(pet1,pet2) and the Prints: a local variable pet1 that is declared in the main method remain unchanged. Dog,Cat The method invocation expression m1(pet1,pet2) has a second argument pet2, and a copy of pet2 is assigned to the method parameter pet2. Inside of method m1, the assignment expression pet2 = null changes the value of the method parameter pet2; but the argument pet2 appearing in the method invocation expression remains unchanged in the main method.
Question 15
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 e. Prints: 1 3 4 i. Run-time error. b. Prints: 6 f. Prints: 8 j. Compile-time error. c. Prints: 2 3 5 g. Prints: 1 3 5 k. None of the above d. Prints: 10 h. Prints: 9 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 Compile- of the addition. Binary numeric promotion causes the byte variables a and j time error. b to be promoted to type int before the 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 16 class GFC304 { static void m1(int[] i1, int[] i2) { int[] i3 = i1; i1 = i2; i2 = i3; } 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: The method m1 is invoked by the method invocation expression m1(i1, i2). The argument i1 denotes a local variable of type int[] that is declared in the main method. The value of the argument is a reference to the array, and the argument value is used to initialize the method parameter i1 of method m1. Prints: Inside the body of m1, the expression i1 = i2 sets the value of parameter i1 b 1,3 to the value of parameter i2, but the change in the value of the parameter i1 does not change the original argument value or the local variable i1 of the main method that the argument denotes. Similarly, the assignment expression i2 = i3 in method m1 does not change the value of the local variable i1 declared in the main method.
Question 17 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 d. Prints: BTCTDFAT g. None of the above b. Prints: ATBFCTDF e. Run-time error c. Prints: BFCTAT f. Compile-time error 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 first Prints: c conditional or operator is false so the right hand operand is evaluated. BFCTAT No further evaluation of the expression is necessary so the right hand operand of the second conditional or operator is not evaluated.
Question 18 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, f. Prints: 2, 3, -2, -2, 3, b. Prints: 2, 2, -3, -4, 12, g. Prints: -1, -2, 2, 2, 0, c. Prints: 2, 3, -3, -4, 7, h. Run-time error d. Prints: 1, 1, 1, 1, 0, i. Compile-time error e. Prints: 2, 2, -2, -2, 4, j. None of the above Answer: The original expression is as follows: m(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: ++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 Prints: 2, right to evaluate the unary expressions: 2 - 2 + -3 * -4. Step two. b 2, -3, -4, Add the parentheses to indicate operator precedence: 2 - 2 + (-3 * 12, -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 19 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, h. Prints: -2, 0, 3, 2, 2, 5, b. Prints: -2, 3, 0, 2, 1, 4, i. Prints: -2, 0, 3, 3, 2, 6, c. Prints: -2, 3, 0, 2, 2, 5, j. Prints: -1, 0, 3, 3, 2, 7, d. Prints: -2, 3, 0, 3, 2, 6, k. Run-time error e. Prints: -1, 3, 0, 3, 2, 7, l. Compile-time error f. Prints: -2, 0, 3, 3, 0, 6, m. None of the above g. Prints: -1, 0, 3, 2, 1, 4, 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) Prints: -2, + m(1<<1)). Expr 1: ~1 = -1 - 1 = -2. Expr 2: 1|2 = 0001 | c 3, 0, 2, 2, 0010 = 0011 = 3. Expr 3: 1&2 = 0001 & 0010 = 0000. Expr 4: 5, 1^3 = 0001 ^ 0011 = 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.