Final ----------------------------------------- 1. (4 points) Define what static and final mean in the context of creating a variable. Answer: 2. (6 points) Turn the following statement into an equivalent while loop. int num = 0; for (int i =5; i >= 0; i--) { num += i; } Answer: 3. (4 points) What is the output of the following code snippet? String testMe = new String(“testMe”); String testMeAgain = new String(“testMe”); System.out.println( testMe == testMeAgain ); testMeAgain = testMe; System.out.println( testMeAgain == testMe ); Answer: 4. (4 points) Assume the following constructor is in a Circle class. What is wrong with the constructor and how would you fix it? public class Circle { private double radius = 3.5; public Circle( double r ) { Circle c = new Circle( 5.5 ); } ... the rest of the class ... } Answer: 5. (5 points) What is the output of the following instructions? Assume that the code is correctly implemented in a class and will work. String icky = new String("icky"); String blicky = new String("icky"); long count = 1; long total = 500; System.out.println( count == 50 || count >= total && count > 0 ); System.out.println( total - count > count - total ); System.out.println( total == (count + total – count ) ); System.out.println(icky==blicky&&count!=total&&count<500&&total==500); System.out.println( !(total != 700) && (total % 2 == 0 ) ); 6. (5 points) What would the value of the following expressions be: a. 22 / 7 = _____________________ b. 22 % 7 = ____________________ c. 6 * 4 / 4 + 2 = _______________ d. 5 * (3 + 1) % 3 = ______________ e. 8 / (5 % 3) / 2 = _______________ 7. (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 double makeThingsPrivate( ) { a. What is the method name? b. What arguments, if any, are required when makeThingsPrivate is called? c. What, if anything, is returned? 8. (8 points) Write a method that returns the maximum of three given arguments. The numbers will all be doubles and if the maximum is equal to more than one of the numbers, it does not matter which one is returned. // The method you write must be called max and must // take 3 double parameters and return a double. You may // not use the Math class in this method. 9. (5 points) Which of the following are legal identifiers (variable names that won't cause compiler errors) in Java? a. fast;one b. cat6 c. school book d. thisGuy e. 2gone 10. (8 points) What is the output of the following program? public class MysteryQuestion6 { public static void main(String[] args) { for (int count=0; count<4; count++) { System.out.println("-> " + count); switch ( count ) { case 1 : System.out.println( "1" ); break; case 2 : System.out.println( "2" ); default: System.out.println( "default" ); } } } } Answer: 11. (4 points) Given the following Java declarations: final int index = 5; int counter = 400; double average = 4.5; Which of the following assignments are invalid? For each invalid assignment, state why the assignment is invalid. a. index = (int) average; b. average = index + counter; c. 40.5 = average; d. average = 3(counter + 10); 12. (8 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 13. (8 points) What is the output of the following program? Remember to give some indication of how you think the program runs, so that partial credit may be given. public class Mystery { private static int x = -1; private static String strX = new String("Chemical X"); public static void doSomething( String p ) { x = 5; System.out.println( x ); } // doSomething public static void main( String[] args ) { System.out.println( strX ); if (strX.equals("puff")) { System.out.println("Chemical X is not puff."); } else { int x = 8; System.out.println( strX ); System.out.println( x ); } System.out.println( x ); } // main } // Mystery 14. (4 points) Given the following class inheritance information, circle all correct statements below: class H extends A class L extends I class K extends H class G extends L class M extends A a. K myK = new A(); b. A myA = new K(); c. M myM = new H(); d. I myI = new G(); 15. (3 points) Indicate if each statement below is true or false: a. Any class with an abstract method is automatically abstract and must be declared as such. b. An abstract class may not be instantiated. c. All methods in an abstract class must be abstract. 16. (6 points) Given the following three partial class definitions: class Penguin { private void penguin1( ) { … } protected void penguin2( ) { … } public void penguin3( ) { … } } class CatBird extends Penguin { public void tryOut( ) { … } } class BirdApp { private Penguin p; public void tryout( ) { … } } Circle all of the following calls that would be legal. a. penguin1( ), from inside CatBird.tryOut b. penguin2( ), from inside CatBird.tryOut c. penguin3( ), from inside CatBird.tryOut d. p.penguin1( ), from inside BirdApp.tryOut e. p.penguin2( ), from inside BirdApp.tryOut f. p.penguin3( ), from inside BirdApp.tryOut 17. (9 points) Given the following abstract class: public abstract class Monster { private String name="Fluffy"; private boolean deadly=false; private double killCount=0; public Monster(String name,boolean deadly, double killCount){ this.name = name; this.deadly = deadly; this.killCount = killCount; } public String getName( ) { return name; } public boolean isDeadly() { return deadly; } public double getKills( ) { return killCount; } public abstract String report(); } Write a complete class Zergling. You only need to do what is listed below. 1. Zergling must inherit from the Monster class. Zergling will not be an abstract class. 2. Write a Zergling constructor that accepts 1 argument: name. The deadly variable in the superclass will be set to false and the killCount variable in the superclass will be set to 0.0 3. There will be a class variable (of type int) inside the Zergling class that will keep track of the number of zergling objects that have been created. The class variable should be initialized to 0 and incremented for each zergling that is created. 4. Implement a method for getting the number zerglings that currently exist. This method will be called getZerglingNumber() and will return an int. 5. Implement the method report(). The results of this method depend on the number of existing zerglings. If there are greater than 150 zerglings, the report method should return “Zergfest is imminently possible. Give up now.”. Otherwise, return the string, “Zergling has been created and is coming to destroy you.” where is the name of the zergling this method is called on.