PRACTICE EXAM 1 SAMPLE ANSWERS 1. (a) 39 Be sure to evaluate the * / % operators first (b) 1 The remainder of 16 / 5 (c) 1 The integer fractions equate to 0 (d) "123456" Concatenation (e) Syntax error: Cannot assign a double to an int without typecasting 2. (a) 21 There are 21 characters in the string (b) w Remember that indices start at 0 (c) NEWS Note: The length of the substring is 7-3 and all the letters become captials (d) No good news. Don't forget to include the period! (e) No sew is good sews. Case matters and replace all occurrences 3. (a) double value = Math.random()*100.0; Math.random() returns a a double in the range [0.0, 1.0). If we multiply any of the values returned by Math.random by 100.0 we get values in the range [0.0, 100.0) (b) int number = (int) (Math.random()*6) * 6 + 12; Math.random()*6 returns a double in the range [0.0, 6.0) (int) (Math.random()*6) returns an int in set {0, 1, 2,..., 5} (int) (Math.random()*6)*6 returns int in {0, 6, 12,..., 30} The complete expression returns int in {12, 18, 24,..., 42} (c) public class LotteryNumberGenerator { public static void main(String[] args) { // This simple program prints out two 3-digit random // numbers for a lottery System.out.println("Play these lottery numbers:"); displayNumber(); displayNumber(); } public static void displayNumber() { // display three random digits from the numbers string String numbers = "0123456789"; System.out.print(numbers.charAt( (int)(Math.random()*10) )); System.out.print(numbers.charAt( (int)(Math.random()*10) )); System.out.println(numbers.charAt( (int)(Math.random()*10) )); } } Note: The print() method will print the three characters on a single line. The last line must use the println() method so that each set of three digits appear on separate lines. (int)(Math.random()*10) returns a random int in the set {0, 1, 2, ..., 9} 4. (a) SAD (!rain || snow) == (!true || false) == (false || false) == false (b) ------------- | rain = true | | snow = false | ------------- | | v / \ / \ true / \ false ------ /!rain || snow\------- | \ / | | \ / | | \ / | v \ / v --------- ------- | HAPPY | | SAD | --------- ------- | | | | ------------->|<------------- | | v (c) boolean rain = true; boolean snow = false; if ( rain && !snow ) System.out.println("SAD"); else System.out.println("HAPPY"); Note: Since we changed the order of the if-statement need to take the negative of the original condition !(!rain || snow) == (!!rain && !snow) by de Morgan's law == (rain && !snow) by double negative law (d) 11 (x % 2 == 1) is false so the body of the if is not executed. But beware: there is no {} so the second line after the condition is not part of the if statement and it is always executed 5. import java.util.*; public class ElectricBill { public static void main(String[] args) { // Read the user meter readings Scanner scan = new Scanner (System.in); System.out.print("Please input last month's meter reading [0-99999]: "); int lastReading = scan.nextInt(); System.out.print("Please input this month's meter reading [0-99999]: "); int thisReading = scan.nextInt(); // Compute kilowatt-hours if (thisReading < lastReading) thisReading += 100000; int numKWattHours = thisReading - lastReading; System.out.println("You used " + numKWattHours + " kilowatt-hour(s)."); // Compute cost int cost; if (numKWattHours < 1000) { cost = 7 * numKWattHours; } else if (numKWattHours <= 2500) { cost = 7000 + 5*(numKWattHours - 1000); } else { cost = 14500 + 3*(numKWattHours - 2500); } int dollars = cost / 100; int cents = cost % 100; System.out.print("Your bill is $" + dollars + "." ); if (cents < 10) System.out.print("0"); System.out.println(cents); } }