CS151 Practice Exam 1 Solutions Fall 1999 Part One The parameters specify what information a method requires as input; the arguments are the values provided when the method is invoked. The right-hand side of an assignment statement can be any kind of expression, but the left-hand side must be a variable. An operand can be a simple value, or a variable, or even a (expression, method invocation). Part Two public static void narf (int fizz); { System.out.println (fizz + fizz); } Illegal semi-colon after the parameter list. int x = 5; String y = "5"; int z = x + y; In the last line, the right-hand side has type String; the left hand side is an int. int x = 2; if (x%2) { System.out.println ("yes!"); } else { System.out.println ("no!"); The final squiggly-bracket has been omitted. Part Three Write the first line of a method named zool that takes three parameters: an int and two Strings. public static void zool (int bing, String bang, String boom) { Write a line of code that invokes zool, passing as arguments the value 11, the name of your first pet, and the name of the street you grew up on (feel free to make something up---I won't check). zool (11, "Henry", "Edgewood"); Part Four: public class Buzz { public static void baffle (String blimp) { System.out.println (blimp); zippo ("ping", -5); } public static void zippo (String quince, int flag) { if (flag < 0) { System.out.println (quince + " zoop"); } else { System.out.println ("ik"); baffle (quince); System.out.println ("boo-wa-ha-ha"); } } public static void main (String[] args) { zippo ("rattle", 13); } } What is the value of the parameter blimp when baffle gets invoked? "rattle" What is the output of this program? ik rattle ping zoop boo-wa-ha-ha Part Five Please write a method named distance} that takes four doubles as parameters (x1, y1, x2 and y2) and that prints the distance between the points. public static void distance (double x1, double y1, double x2, double y2) { return Math.sqrt (sumSquares (x2-x1, y2-y1)); } Part Six For any expression, the compiler can determine the type without being told. Explain how, and convince me that this is possible for any expression. For simple values (1, 1.5, "string"), it can tell by looking at things like quotation marks and decimal points. For variables it can tell by looking at the declaration. For method invocations it can tell by looking at the method definition. For expressions it can tell by looking at the types of the operands and the known behavior of the operators. Suppose I told you that the * operator can be used with String}s, as long as the second operand is an integer. It might be defined to mean ``repeat the String $n$ times,'' so the expression "ha" * 3 would yield the value "hahaha". Is * a good choice to represent this operation? Why or why not? PRO: it might be a useful operation, and * is probably an easy symbol to remember, since the operation is reminiscent of multiplication in some ways. CON: this operation is clearly not the same thing as multiplication, which I can demonstrate two ways: (1) multiplication is commutative, and this thing is not (2) corresponding operands do not produce corresponding results 3 * 3 = 9 3.0 * 3.0 = 9.0 "3" * 3 = "333" The first two operations correspond. The third does not.