CIS1068 Spring 2010 MIDTERM 1 Closed books, closed notes. The points per question are indicated next to the question. The total points for the exam are 155 (5) 1. What are microseconds? kilohertz? terabytes? (5) 2. People say that a Java program is written once and can be run anywhere. What do they mean exactly? (5) 3. What does a compiler do (for instance the Java compiler javac)? (5) 4. Name the Java primitive data types. (10) 5. In Java a short is represented using 2 bytes. (a) How many values can be represented? (b) What value do I get if I add 1 to the largest positive integer? (5) 6. We write the number two hundred thirty four in positional decimal notation as 234. What does "positional decimal" mean? (10) 7. We have mentioned that (a) when using integers we can run into a kind of error, and (b) when using reals we can also run into another kind of error. What are the names and meaning of these run-time errors? (5) 8. What is a variable? (5) 9. Given an example of a formal parameter and of a local variable. (5) 10. f and g are two Strings. We are told that f.equals(g) is true. Will also f == g be true? Explain. (5) 11. Declare the integer constant RETIREMENT_AGE with value 66. (10) 12. What is the value of the expression (show intermediate results): 20 < 3 % 3 + 5 / 3 * 17 || 3 == 3 && 3 < 1 (10) 13. Which of the following assignments is legal (I just give names of primitive types, to avoid having to write declarations): int = short; int = char; double = int; boolean = char; float = double; (10)14. After the declaration of String s = "violets are blue"; What is s.length() s.substring(3) s.substring(2, 6) s.indexOf("e") s.indexOf("e", 5) s.charAt(3) (10) 15. What is printed out by the following code fragment: String s1 = "x"; String s2 = "y"; for (int k = 0; k<3; k++) { System.out.print(s1 + s2); s1 += s1; s2 += s2; } (10)16. What is printed by the following program and why public class Moo { public static void foo (int x) {x = x + 2;} public static void main(String[] args) { int a = 5; System.out.println(a); } } (10)17. From high school you remember that the distance between a point at coordinates x1,y1 and a point at coordinates x2,y2 is the square root of (x2-1)*(x2-x1) + (y2-y1)*(y2-y1) Write the function public static double distance(double x1, double y1, double x2, double y2) which returns the distance between x1,y1 and x2,y2. (15)18. Implement the method public static String unique(String s) which, given a string s, returns the string consisting of a single copy of each character in s. For example unique("abracadabra") is "abrcd". (15)19. Implement the method public static int countChar(String s, char c) that returns the number of occurrences of the character c in s.