Operator & Assignment 1

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Operator & Assignment 1 as PDF for free.

More details

  • Words: 3,000
  • Pages: 11
Operator & Assignment Question 1 class GFC401 { static int m1(int x) {return ++x;} public static void main (String[] args) { int x = 1; int y = m1(x); System.out.println(x + "," + y); }} What is the result of attempting to compile and run the program?

a. Prints: 1,1 d. Prints: 2,2 g. None of the above b. Prints: 1,2 e. Run-time error c. Prints: 2,1 f. Compile-time error Answer: Prints: Primitive arguments are passed by value. The method m1 increments the parameter x, b 1,2 and the result is returned. The local variable x of main remains unchanged.

Question 2 class GRC10 { public static void main (String[] s) { System.out.print(s[1] + s[2] + s[3]); }} java GRC10 A B C D E F

What is the result of attempting to compile and run the program using the specified command line? a. b. c. d. e. f. g.

Prints: ABC Prints: BCD Prints: CDE Prints: A B C Prints: B C D Prints: C D E Compile-time error h. Run-time error i. None of the above

Answer: Prints: b BCD

The index for the first element of an array is zero so the first argument printed by this program is the second argument on the command line following the name of the class.

Question 3 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 Answer: Compile-time j error

e. f. g. h.

Prints: 3,0,0 Prints: 3,0,3 Prints: 3,3,0 Prints: 3,3,3

Both operands of the conditional and operator and the conditional or operator must be of type boolean.

Question 4 class GFC100 { public static void main(String[] args) { final short s1 = 1; // 1 final char c1 = 1; // 2 byte b1 = s1; // 3 byte b2 = c1; // 4 byte b3 = 1; // 5 byte b4 = 1L; // 6 byte b5 = 1.0; // 7 byte b6 = 1.0d; // 8 }} Compile-time errors are generated at which lines?

a. b. c. d. e. f. g. h.

1 2 3 4 5 6 7 8

i. Run-time error j. Compile-time error k. None of the above

Answer: The compiler will implicitly do a narrowing conversion for an assignment f 6 statement if the right hand operand is a compile time constant of type byte, g 7 short, char, or int and the value falls within the range of the variable on the h 8 left and if the variable is of type byte, short, or char.

Question 5 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 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 compiler will implicitly do a narrowing conversion for an assignment f 6 statement if the right hand operand is a compile time constant of type byte, g 7 short, char, or int and the value falls within the range of the variable on the h 8 left and if the variable is of type byte, short, or char.

Question 6 class GFC101 { public static void main(String[] args) { byte b1 = -128; // 1 byte b2 = 128; // 2 short s1 = -32769; // 3 short s2 = 32767; // 4 char c1 = 0; // 5 char c2 = 65536; // 6 }} Compile-time errors are generated at which lines?

a. b. c. d. e. f.

1 2 3 4 5 6

Answer: b c 2 3 The maximum value of type byte is 127. The minimum value of type short f 6 is -32768. The maximum value of type char is 65535.

Question 7 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. b. c. d. e. f.

Prints: 1, 2, 3, 4, 8, Prints: 1, 2, 3, 4, 11, Prints: 3, 4, 1, 2, 11, Run-time error Compile-time error 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) + m(3) * Prints: 1, m(4)). Simplification step one. Evaluate each operand from left to right: b 2, 3, 4, m(1 - 2 + 3 * 4). Step two. Add parentheses to indicate operator 11, 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 8 class Magenta { static byte a = (byte)127, b = (byte)256; public static void main(String System.out.print(a + " " + b }} What is the result of attempting

(byte)128, c = (byte)255, d = args[]) { + " " + c + " " + d); to compile and run the program?

a. Prints: 127 128 255 256 d. Prints: 127 -128 -1 0 g. None of the above b. Prints: 127 128 255 0 e. Run-time error c. Prints: 127 -1 -127 0 f. Compile-time error Answer: Bytes are stored as 8 bit two's complement signed integers. When an int primitive is cast to a byte, the three most significant bytes are discarded and Prints: only the least significant byte remains. The most significant bit of the d 127 -128 remaining byte becomes the new sign bit. byte a = (byte)127; // 01111111. -1 0 byte b = (byte)128; // 10000000. byte c = (byte)255; // 11111111. byte d = (byte)256; // 00000000.

Question 9 interface I1 {} interface I2 {} class Base implements I1 {} class Sub extends Base implements I2 {} class Red { public static void main(String args[]) { Sub s1 = new Sub(); I2 i2 = s1; // 1 I1 i1 = s1; // 2 Base base = s1; // 3 Sub s2 = (Sub)base; // 4 }}A compile-time error is generated at which line?

a. b. c. d. e.

1 2 3 4 None of the above

Answer: Line 4 does not generate a compile-time error. The reference named base None of the actually refers to an instance of type Sub, so the reference may be cast to e above type Sub.

Question 10 class Green { public static void main (String args[]) { int[] i = null; // 1 Cloneable c = i; // 2 i = (int [])c; // 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: The null literal is converted to an int array type with the value null. All None of array types implement the Cloneable interface, so any array reference can be assigned to a reference of type Cloneable. The int array object g the above referenced by the Cloneable reference, c, can be assigned to a reference of the int array type, int[].

Question 11 class GFC215 { static String static String public static int a1 = 1; }}

m(float i) {return "float";} m(double i) {return "double";} void main (String[] args) { long 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 d. Prints: double,double g. None of the above b. Prints: float,double e. Compile-time error c. Prints: double,float f. Run-time error Answer: a Prints: A method invocation conversion can widen an argument of type float to float,float match a method parameter of type double, so any argument that can be passed to m(float i) can also be passed to m(double i) without generating a compile-time type error. For that reason, we can say that m(float i) is more specific than m(double i). Since both methods are applicable, the more specific of the two, m(float i), is chosen over the less specific, m(double i). The arguments of the method invocation expressions, m(a1) and m(b1), are of types int and long respectively. A method invocation conversion can widen an argument of type int or

long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i).

Question 12 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, f. Prints: 2, 3, 4, 1, 3, b. Prints: 1, 2, 3, 4, 3, g. Run-time error c. Prints: 1, 2, 3, 4, 9, h. Compile-time error d. Prints: 1, 2, 3, 4, 12, i. None of the above e. Prints: 2, 3, 4, 1, 9, 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)). Prints: 1, Simplification step one. Evaluate each operand from left to right: m(1 + 2 c 2, 3, 4, % 3 * 4). Step two. Add parentheses to indicate operator precedence and 9, associativity: m(1 + ((2 % 3) * 4). Step three. Evaluate the innermost parentheses: m(1 + (2 * 4)). Step four. Evaluate the inner-most parentheses: m(1 + 8). The result is 9.

Question 13 class Primitives { static void printFloat(float f) {System.out.println(f);} static void printDouble(double d) {System.out.println(d);} public static void main(String[] args) { byte b = 1; // 1 short s = b; // 2 char c = s; // 3 int i = c; // 4 long l = i; // 5 float f = l; // 6 printFloat(i); // 7 printFloat(l); // 8 printDouble(l); // 9 }} A compile-time error is generated at which line?

a. 1 b. 2 c. 3 d. 4 Answer:

e. f. g. h.

5 6 7 8

i. 9 j. None of the above

c 3

Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa.

Question 14 interface I1 {} interface I2 {} class Base implements I1 {} class Sub extends Base implements I2 {} class Orange { public static void main(String args[]) { Base base = new Base(); I1 i1 = base; // 1 Sub sub = (Sub)base; // 2 }} What is the result of attempting to compile and run the program?

a. Compile-time error at line 1 d. Run-time error at line 2 b. Run-time error at line 1 e. None of the above c. Compile-time error at line 2 Answer: Run-time The compiler accepts the explicit cast at line 2, but an error is generated at d error at line run-time. Type Base is the super class of type Sub, so an instance of type 2 Base can not be converted to the type of the subclass, Sub.

Question 15 class Purple { public static void main (String []args) { int[] i = {1,2,3}; // 1 Object obj = i; // 2 i = obj; // 3 }} What is the result of attempting to compile and run the program?

a. b. c. d.

Compile-time error at line 1. Run-time error at line 1. Compile-time error at line 2. Run-time error at line 2.

Answer: Compile-time e error at line 3.

e. Compile-time error at line 3. f. Run-time error at line 3. g. None of the above

Although the referenced object is indeed an array of type int, an explicit cast is necessary to cast the obj reference to an int array.

Question 16 class GFC216 { static String m(float i) {return "float";} static String m(double i) {return "double";} public static void main (String[] args) { char a1 = 1; long 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 d. Prints: double,double g. None of the above b. Prints: float,double e. Compile-time error c. Prints: double,float f. Run-time error Answer: A method invocation conversion can widen an argument of type float to match a method parameter of type double, so any argument that can be passed to m(float i) without generating a compile-time type error can also be passed to m(double i). For that reason, we can say that m(float i) is more specific than m(double i). The arguments of the method invocation expressions, m(a1) and m(b1), are of types char Prints: a float,float and long respectively. A method invocation conversion can widen an argument of type char or long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i).

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, e. Prints: 2, 3, -3, -2, 0, i. Compile-time error b. Prints: 1, 2, -3, 4, 4, f. Prints: 2, 3, -3, 4, 6, j. None of the above c. Prints: 2, 2, -3, -3, -2, g. Prints: 2, 3, 4, 5, 14, d. Prints: 2, 2, -3, 3, 4, h. Run-time error Answer: The expression can be simplified as follows: j = 2 + 2 + -3 + 3 = Prints: 2, 4. The original expression is as follows: j = ++i + i++ + -i + i++. d 2, -3, 3, Simplification step one. Evaluate the unary expressions from left to right: j 4, = 2 + 2 + -3 + 3. Step two. Complete the evaluation of the simplified expression: j = 4.

Question 18 class GFC301 { private String name; public GFC301(String name) {this.name = name;} public void setName(String name) {this.name = name;} public String getName() {return name;} public static void m1(GFC301 r1, GFC301 r2) { r1.setName("Bird"); r2 = r1;

} public static void main (String[] args) { GFC301 pet1 = new GFC301("Dog"); GFC301 pet2 = new GFC301("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 e. Run-time error b. Prints: Dog,Bird f. Compile-time error c. Prints: Bird,Cat g. None of the above d. Prints: Bird,Bird Answer: The method m1 is invoked by the method invocation expression m1(pet1,pet2). The value of the reference variable denoted by the argument pet1 is used to initialize the method parameter r1. Inside of method m1, the method invocation expression r1.setName("Bird") uses the copy of the value of the argument pet1 to assign a new name to the instance of GFC301 that is referenced by the local variable pet1 in the main method. Generally speaking, a reference parameter can be used to Prints: c invoke methods on the referenced object and change the state of the object to Bird,Cat the extent provided by the object's methods. The method invocation expression m1(pet1,pet2) has a second argument pet2, and the value of pet2 is used to initialize the method parameter r2. Inside of method m1, the assignment expression r2 = r1 changes the value of the method parameter r2; but the local variable of the main method denoted by the argument pet2 appearing in the method invocation expression m1(pet1,pet2) remains unchanged.

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

Related Documents

Operator
May 2020 27
1. Operator Logika.docx
November 2019 13

More Documents from "refina"