Kanchi CPSC 101 COMPUTING AND ALGORITHMS I TEST I Your Name: KEY -------------------------------- 1. (15 points) State True or False. Give reasons for your choice. A. The Java compiler works more efficiently if the looping statements are properly indented. Answer: False. The amount of indentation is irrelevant to java compiler. Java is free format. B. A memory is said to be a ROM if every address is a random number. Answer: False. ROM refers to the fact that the memmory cannot be written onto. It is a read only memory. C. If a is a char type whose value is '9' and b is an int type whose value is 9, then the contents in the memory of a and b will be the same. Answer: False. The unicode value of '9' is stored for char type. However, for int type the value is 9 is stored. Moreover the number of bits reserved may also vary for int and char types. D. Given that x is declared as a boolean, the assignment statement x = false || true; will store the value 'true' in x. Answer: True, x will be 'true' since the right side evaluates to 'true' value. E. Assuming that ReadUtility is available, the code, int x; x = (int) 14.5 + 3.5; will cause errors. Answer: True. 14.5 is converted to int first which makes it 14 and then 3.5 is added which makes it double value of 17.5 and this cannot be assigned to an int x. 2. (20 points) Find errors (compile time, run time and logical), if any, in following codes and explain the errors. Skip the error prone statements and continue with the rest of the code. Show the output of the error free lines. (Note: Do not look for missing statements, simply examine the given code for errors.) A. int x = 5; if (x == x+1) { System.out.println("yes"); } else System.out.println("No"); Answer: No Errors. The printout is No B. int x = 65; \\Line 1 float f = 65.5f; \\Line 2 char c = 'B'; \\Line 3 f = x; \\Line 4 x = f; \\Line 5 c = (char) x; \\Line 6 x = c; \\Line 7 c = (char) f; \\Line 8 f = (char) c; \\Line 9 System.out.println(c); \\Line 10 System.out.println(f); \\Line 11 System.out.println(x); \\Line 12 Answer: Error in Line 5, since x is int and f is float, it is a windening convention. Printout: A 66.0 66 3.(20 points) For each of the expressions below, find the value and state any changes to memory. Identify any invalid expressions and explain why it is invalid. a) int x = 4, y = 3; boolean d; d = x==3 % y || !false Answer d = x == 3 % y || true d = x == 0 || true d = false || true d = false || true d = true true Note: Side effect d becomes true. b) float q=9.0f, r= 4.3f; q = q+1 * 3 % 2 -r Answer: q = q+ 3 % 2 -r q = q+1 -r q = 10.0 -r q = 10.0 - 4.3 q = 5.7 5.7 Note: Side Effect is that q changes to 5.7. 4. (15 points) Use a if statement to read a character and if it is 'W' or 'w' -- print WINDOW if it is 'A' or 'a' -- print AISLE else -- print NOT VALID Answer: char c; c = ReadUtility.readChar(); if (c == 'w' || c == "W') System.out.println("WINDOW"); else if (c == 'A' || c == 'a') System.out.println("AISLE"); else System.out.println("NOT VALID"); 5. (30 points) Write a program that reads weight and height and prints if person is overweight. A valid weight must be between 50 pounds and 450 pounds and valid height must be between 4 feet and 7 feet. If an invalid entry is read, the program exits. The program computes if the person is overweight using the following rule: If a person's weight is more than height times 50, then that person is considered overweight. Make sure weight and height are 'float's not 'int's! Use constants wherever appropriate. Sample Run1: Enter weight: 340.0 Enter height: 5.0 YOU ARE OVERWEIGHT.. YOU ARE OVER 250 pounds!! Sample Run2: Enter weight: 140.0 Enter height: 5.0 YOU ARE NOT OVERWEIGHT.. YOU ARE UNDER 250 pounds!! Sample Run3: Enter weight: 740.0 Enter height: 6.0 YOUR WEIGHT OR HEIGHT IS INVALID Answer: import ReadUtility; class MainClass{ public static void main(String[] args){ float LOW_WEIGHT_LIMIT = 50.0f; float HIGH_WEIGHT_LIMIT = 450.0f; float LOW_HEIGHT_LIMIT = 4.0f; float HIGH_HEIGHT_LIMIT = 7.0f; float FACTOR = 50.0f; int NUM_TIMES = 8; int choice; int height; int weight; System.out.print("Enter weight:" ); weight = ReadUtility.readFloat(); System.out.print("Enter height:"); height = ReadUtility.readFloat(); if (weight < LOW_WEIGHT_LIMIT || weight > HIGH_WEIGHT_LIMIT || height HIGH_HEIGHT_LIMIT) System.out.println("INVALID WEIGHT OR HEIGHT"); } else if (weight > height *FACTOR) System.out.println("YOU ARE OVERWRIGHT ... YOU ARE OVER" + height*FACTOR +"POUNDS"); else System.out.println("YOU ARE NOT OVERWEIGHT.. YOU ARE UNDER" + height*FACTOR + "POUNDS"); } //end main }//end MainClass