Beruflich Dokumente
Kultur Dokumente
Questions 1-4 are to be answered on paper. 1. Write assignment statements that perform the following operations with the variables a, b and c, which are already declared. a and b are doubles, and c is a char. a) b) c) d) e) Adds 2 to a and stores the result in b. Multiplies b times 4 and stores the result in a. Divides a by 3.14 and stores the result in b. Subtracts 8 from b and stores the result in a. Stores the character 'K' in c. b = a+2; a = b * 4; b = a / 3.14; a = b 8; c = 'K';
2. Evaluate the following expressions (do each calculation independently from the others). int a = 2, b = -3, c = 5, d = -7, e = 11; Expression d = a / b / c; a = 7 + c * d / e; d = 2 * a % - b + c + 1; a = 39 / - e - 29 % c; c = 7 + a % (3 + b);
3. Write the code for the function median that takes three integers, a, b and c as parameters and returns the middle value in terms of size (e.g., the call median(5, 2, 4) should return the value 4). public static int median(int a, int b, int c) { int max = Math.max(Math.max(a,b),c); int min = Math.min(Math.min(a,b), c); int med = (a+b+c)-(max+min); return med; } 4. What gets printed by the code below? public static void main(String [] args) { int a, b = 1, total = 0; for (a = 0; a + b < 8; ++a) { total += a * b; b += 2; } System.out.println("a = " + a + " } a = 3 b = 7 total = 13
b="+b+"