Quiz #2 ----------------------------------------- 1. (4 points) What would the values of the following expressions be: a. 68 % 204 b. 1 + 2 * 4 % 2 ANSWER: a. 68 b. 1 2. (3 points) The following code won’t compile. Fix the error in it. Please use casting in your answer. double a = 4.532; int wholeNumber = a; ANSWER: int wholeNumber = (int) a; 3. (6 points) A class contains a method and the first line of the method is shown below. Assume that the rest of the method is correctly implemented and answer the questions below. public void garbleSpeech( String speech ) { a. What is the method name? ANSWER: garbleSpeech b. What arguments, if any, are required when garbleSpeech is called? ANSWER: speech is a required String argument. c. What, if anything, is returned? ANSWER: Nothing (void) is returned. 4. (10 points) What is the output of the program below? Be sure to include any assumptions you make about how the program runs to get partial credit. public class Buzz { private static int i = 5; private static String str = new String(“fizz”); public static void fizzed( int i ) { if ( (( i % 5) == 0) && ((i % 7) == 0) ) { System.out.print(“fizzbuzz”); } else { if ((i%5) == 0) System.out.print( str ); else { if ((i%7) == 0) System.out.print(“buzz”); else System.out.print(i); } } } public static void main( String[] args ) { System.out.println(“In the main method”); fizzed( 49 ); fizzed( i ); System.out.println(); System.out.println( str == “fizz” ); } } // Buzz ANSWER: You can cut/paste this into a program and run it. No answer given. 5. (4 points) Assume the following if statement exists in a class. What is wrong with it and how would you fix it? long inky = 500; if ( 1 < inky < 1000 ) { System.out.println( inky ); } ANSWER: The logical statement works in math, but not in programming where it would have to be split up into two statements like this: if (1 < inky && inky < 1000) 6. (4 points) What is the output for the following chunk of code?: long giggle = 3; int a = 0, b = 0; switch( giggle ) { case 1: a = 1; break; case 2: b = 2; case 3: a = 3; case 4: b = 4; case 5: a = 5; break; default: break; } System.out.println("a is " + a + " and b is " + b); ANSWER: Well, you actually can't switch on a long, but if you could the answer would be: a is 5 and b is 4 This is due to the lack of a break statement in case 3. 7. (6 points) What is wrong with the following piece of code and why? int i = 1; if ( i ) { System.out.println("I saw i!"); } ANSWER: if ( 1 ) - it's not a boolean statement. if ( true ) would be 8. (10 points) Write a series of conditional statements that will find the minimum of two integer numbers a and b. You can put the conditional statement inside the method construct we have written for you. You may not use the Math class in this question. public int minimum( int a, int b ) { int min = a; // put ANSWER code here if ( b < min ) min = b; // return our minimum to whomever called this method return min; } 9. (4 points) Write one statement that declares an array named lizards, creates the array, and fills it with the values “Binky”, “Zeller”, and “Stellar”. Answer: String[] lizards = {“Binky”, “Zeller”, “Stellar”}; 10. (6 points) Turn the following statement into an equivalent if set of statements. You may assume that count has previously been created and is an integer. switch ( count ) { case 1 : System.out.println( “1” ); break; case 2 : System.out.println( “2” ); default: System.out.println( “default” ); } Answer: if (count == 1) { System.out.println( “1” ); } else { if ( count == 2 ) { System.out.println( “2” ); } System.out.println( “default” ); } Anything other than count == 1 will end up printing the "default" since there is no break statement. 11. (8 points) What is the output of the following program? public class MysteryQuestion2 { public static void main(String[] args) { for (int outerCount=0; outerCount < 4; outerCount++) { for (int count=0; count<=outerCount; count++) { System.out.print("-"); } System.out.println(">?"); } } } Answer: Go ahead and copy/paste to get the answer. No answer provided