CS 170 Exam 1 Section 001 Spring 2015 Name: Place your name tag here Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than the professor/proctor for ANY reason in ANY language in ANY manner. This exam is closed notes, closed books, and no calculator. Turn all mobile devices off and put them away now. You cannot have them on your desk. Write neatly and clearly indicate your answers. What I cannot read, I will assume to be incorrect. Stop writing when told to do so at the end of the exam. I will take 5 points off your exam if I have to tell you multiple times. Academic misconduct will not be tolerated. Suspected academic misconduct will be immediately referred to the Emory Honor Council. Penalties for misconduct will be a zero on this exam, an F grade in the course, and/or other disciplinary action that may be applied by the Emory Honor Council. Time: This exam has 8 questions on 13 pages including the title page. Please check to make sure all pages are included. You will have 75 minutes to complete this exam. I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Emory community. I have also read and understand the requirements and policies outlined above. Signature: Question: 1 2 3 4 5 6 7 8 Total Points: 10 10 11 15 5 5 9 10 75 Score: 1
1. (10 points) Definitions. Define 5 of the 6 terms below. You do not need to give a formal definition just a good description. You may include an example if it is helpful to your answer. Be brief, and clearly mark the term you do not want me to grade. Otherwise, I will discard the last term. (a) local variable Solution: A local variable is a variable that is only in scope (is visible or can be used) within the block of code that defined it. (for example, an if statement or method body). Once the function returns, the local variable is lost. (b) keyword Solution: A reserved word which is used by the compiler and has a special meaning in the language structure. Ex: if, int, class, etc (c) argument Solution: A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function (d) compiler Solution: A reserved word that is used by the compiler to parse the program; you cannot use keywords (such as if, class, and void) as variable or function names (identifiers). (e) ASCII code (or encoding) (f) string Solution: The internal mapping a computer makes between a symbol and a numerical value. Ex: A maps to 65. Solution: A series of characters or symbols. In Java, denoted by use of quotation marks, Ex: "Hello World!" 2
2. (10 points) Evaluate each expression. Then give the result of the evaluation and the data type of the result. If the expression cannot be evaluated or is not proper Java syntax, you may simply write error for the value. The first row has been done for you. String s1 = "Spring", s2 = "85"; char c1 = 5, c2 = q ; int i1 = 7, i2 = 4; double d1 = 1.0, d2 = 0.75; boolean b1 = true; Expression Value Type 4+1 5 int i1 % i2 3 int (i2+d1+2) / i1 1.0 double!b1 false boolean b1 && i2!= i1 true boolean "c1" + s1 c1spring String c2 + (int)d2 113 int s1.charat(4) + i2 + s2 11485 String (char)(i2 * 21) T char i1--!= ++i2 + 1 true boolean c1 < c2 i1 < i2 true boolean Notes: The parenthesis operator has the highest precedence of all operators. The logical not operator and the casting operator have higher precedence than arithmetic operators, relational/comparison operators, and the other logical operators. Arithmetic operators have higher precedence than relational/comparison or logical operators. Relational/comparison operators have higher precedence than logical operators. Assignment operators have the lowest precedence of all operators 3
3. (11 points) Consider the program below. What will be the output of the program when it is executed? public class Exam1 { public static int method1(int a, double b) { System.out.println("A " + a + " " + b); if (a > b) { System.out.println("B"); b += 1.5; return (int)b; if (b > a) { System.out.println("C"); a -= 2; return a; b += 12.5; return (int)(a+b); public static void method2(int a) { if (a >= 20 && a <= 30) { a += 30; System.out.println("D " + a); return; else { System.out.println("E " + a); System.out.println("F"); public static String method3(string s) { System.out.println("Hello, " + s); if (s.length() > 5) { return s.substring(5); else { return s.substring(2); public static void main(string[] args) { int a = 10; int b = 20; String s1 = method3("hel" + a); method2(b); System.out.println("G " + s1); System.out.println("H " + a + " " + b); a = method1(b, a); System.out.println("I " + a + " " + b); 4
Solution: Hello, Hel10 D 50 G l10 H 10 20 A 20 10.0 B I 11 20 5
4. Short Answer. (a) (2 points) In the following method, how many local variables are there? How many parameter variables are there? public static double mystery(int a, String b, char c) { a = a + c; double d = a; boolean e = a <= c; b += c; return a; Solution: 2 local variables (d and e) 3 parameter variables (a, b, and c) (b) (3 points) Consider the following function: public static int mysteryfunction(int a,int b) { if (a < b) { return a; return b; i. What does this function return if called with mysteryfunction(5,10)? Solution: 5 ii. Write an appropriate comment for this method. Solution: It returns the smaller of two input parameter numbers. In other words, it finds the minimum. It was not enough to restate the code! (c) (2 points) Give the output of the following code fragment: int a = 3; int b = 10; double pi = 3.14159; if (b / a > pi) System.out.println("a"); else if ((double)b / a > pi) System.out.println("b"); else System.out.println("c"); Solution: b 6
(d) (2 points) What does the following method print if it is invoked with the call foo(-1, 8, -3)? public static void foo(int a, int b, int c) { if (b > c) { if (c > 0) { System.out.println("A"); else if (b > 0) { System.out.println("B"); b=c-b; else if (a < 0) { System.out.println("C"); else { System.out.println("D"); if (b > c) { System.out.println("E"); else { System.out.println("F"); else System.out.println("G"); Solution: I ended up giving everyone full credit on this problem due to a significant typo. However, it s been corrected in the version above, and you should understand how the above code gives the answer: B F (e) (4 points) Give the output of the following code fragment: int a = 2; int b = 10; int c = 20; int d = 7; a += c; b %= d; System.out.println("a: " + a); System.out.println("b: " + b); c = ++a + b + d--; System.out.println("c: " + c); System.out.println("d: " + d); 7
Solution: a: 22 b: 3 c: 33 d: 6 8
(f) (2 points) Write a single Java statement which would cause the output: She said, "Hello" to me. to appear in the terminal window. Solution: System.out.println("She said\n\"hello\" to me"); 9
5. (5 points) Prof. Summet has written the following code to convert numerical scores to letter grade for CS170. However, she has 5 syntax errors. Identify the errors by circling them and rewrite the line of code to be syntactically correct. Hint for 1 tricky error: what will the program do if the user enters -5? 1 import java.util.scanner; 2 3 public class Exam1{ 4 public static String grade(int score) { 5 if (score >= 90) 6 return "A"; 7 if (score >= 80) 8 return "B"; 9 if (score >= 70) 10 return "C"; 11 if (score >= 60) 12 return "D"; 13 if (score => 0) 14 return "F"; 15 16 17 public static void main (String[] args){ 18 Scanner in = new Scanner(System.in); 19 System.out.print("Enter a numerical grade: "); 20 int score = in.nextint() 21 String g = Grade(score); 22 System.out.println(score + is a + g); 23 24 Solution: line 19: missing ; line 20: function name is grade not Grade line 21: missing " around String line 13: operator written incorrectly; should be >= lines 5-14: missing a return statement. It s not enough just to add an else since the else will only pair with the last if statement. You need to either 1) add just a return statement like return "Error"; as the last statement of the method or rewrite the entire method to use else if and the last condition simply use an else. Note that the space between main and the opening ( isn t an error since Java is a format-free language. 10
6. (5 points) Write a method named isnegative that takes in an integer parameter and returns a boolean value. If the parameter is negative, the function should return the boolean value true. If the parameter is zero or positive, it should return the boolean value false. Examples: isnegative(-8) returns true isnegative(5) returns false Solution: public static boolean isnegative(int num) { if (num >= 0) return false; else return true; Scoring: 1 point for a correct header 2 points for testing if the number is positive/negative correctly 1 point for returning false if the number is positive or zero 1 point for returning True if the number is negative 11
7. (9 points) Write a method named uniqueproduct which takes 3 integers as input. The function should return the product of the 3 inputs. However, if one of the input values is the same as another of the values, it should return the unique (not duplicated) number. If all three numbers are the same, the function should return -1. Examples: uniqueproduct(2, 3, 4) returns 24 uniqueproduct(2, 2, 3) returns 3 uniqueproduct(1, 1, 1) returns -1 Solution: public static int uniqueproduct(int a, int b, int c) { if (a == b && b == c) { return -1; else { if (a == b) return c; else if (b == c) return a; else if (a == c) return b; else return a * b * c; Scoring: +2 header +2 returns -1 if all are same +4 handles cases where only 2 numbers are the same +1 handles case where all are different 12
8. (10 points) Write a method named strangecat which takes two Strings as input. This function should perform a strange concatenation as described below: If the strings are different lengths, omit initial characters from the longer string so it is the same length as the shorter string. So Hello and Hi yield lohi (both of length 2, omits the initial 3 characters from Hello ). If the strings are the same length, return a String made by repeating the first String. Examples: strangecat("hello", "Hi") returns "lohi" strangecat("java", "Hello") returns "Javaello" strangecat("ho", "Hi") returns "HoHo" Solution: public static String strangecat(string a, String b) { if (a.length() > b.length()) { return a.substring(a.length()-b.length()) + b; else if (a.length() < b.length()){ return a + b.substring(b.length()-a.length()); else { return a+a; Scoring: +2 header +2 handles if b longer than a +2 handles if a longer than b +2 handles if strings are same length +2 omits initial characters correctly (uses substring correctly) 13