Final ----------------------------------------- 1. (4 points) Define what static and final mean in the context of creating a variable. Answer: Static refers to the creation of a class variable. There is only one copy of the variable and whenever an instance is created it refers to the single copy of the class variable. Final means that the variable is constant. 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: int num = 0; int i=0; while ( i >= 0 ) { num += i; i--; } 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: false true 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: Constructors do not make objects, they instead initialize variables for an object that is being created. In order to fix the constructor: public class Circle { private double radius = 3.5; public Circle( double r ) { this.radius = r; } ... the rest of the class ... } 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 ) ); Answer: false true true false false 6. (5 points) What would the value of the following expressions be: a. 22 / 7 = _____________________ 2 b. 22 % 7 = ____________________ 1 c. 6 * 4 / 4 + 2 = _______________ 8 d. 5 * (3 + 1) % 3 = ______________ 2 e. 8 / (5 % 3) / 2 = _______________ 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? makeThingsPrivate b. What arguments, if any, are required when makeThingsPrivate is called? none c. What, if anything, is returned? a double 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. public double returnMax(int a, int b, int c) { double returnValue = a; if (returnValue < b) returnValue = b; if (returnValue < c) returnValue = c; return returnValue; } 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 Legal identifiers are: b. and d. Note that variables may also start with an underscore or dollar sign. 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: This code may be run to figure out the 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); Invalid assignments: a. It's constant and can't be changed. c. The assignment statement is backwards. d. There should be a multiplication sign between the 3 and ( 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 Answer: You may copy and run this program to see the result. 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 Answer: You may copy and run this program to see the result. 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(); Answer: The correct assignments are b. and d. Both of the correct answers have the more general class (the super class) on the left side of the assignment and the more specific class (the subclass) on the right side. 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. Answer: a. true b. true c. false - you can create non-abstract methods in an abstract class. 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 Answer: legal calls are - b., c., and f. 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. public class Zergling extends Monster { private static int zerglingNumber = 0; public Zergling( String name ) { super(name, false, 0.0); zerglingNumber++; } public static int getZerglingNumber() { return zerglingNumber; } // Note: report assumes a String is returned and so a // println should not be done. public String report() { String returnValue; if ( getZerglingNumber() > 150 ) { returnValue = "Zergfest is imminently possible. Give up now.”; } else { returnValue = “Zergling " + getName() + "has been created and is coming to destroy you.”; } return returnValue; } }