Fall 2013 Name: Instructor s Name: Page Points Score 1 13 2 7 3 12 4 9 5 4 6 6 7 10 8 12 9 7 10 8 11 3 12 9 Total: 100 Instructions The exam is closed book. You may not use a calculator, cell phone, etc. You may bring to the exam your own cheat sheet : one 8.5 x 11 sheet of paper with notes that you have written. You may use both sides of the sheet. If a question asks you to answer briefly, it means just that. A sentence or two at most should be sufficient. Unless otherwise specified, you may use functions from the standard Java library. Good luck. i
Some Reminders about Syntax Miscellaneous static boolean Arrays.equals(int[ ] a1, int[ ] a2); static void Arrays.sort(int[ ] a); static int Arrays.binarySearch(int[ ] a, int key); static String Arrays.toString(int[ ] a); static double Math.max(double d1, double d2); Object class boolean equals(object obj); // indicates if some other object is "equal to" this one String tostring( ); // returns a string representation of the object String class int indexof(string s); // returns position of s, -1 if not there int indexof(string s, int k); //returns position of s at or past position k char charat(int k); // returns character at position k String substring(int k); //substring starting at k up to end String substring(int k, int h); //substring starting at k ending at h excluded int compareto(string anotherstring); String touppercase( ); boolean equalsignorecase(string s); Files Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(new File(filename)); boolean sc.hasnext( ); boolean sc.hasnextline( ); String sc.next( ); String sc.nextline( ); int sc.nextint( ); PrintWriter pw = new PrintWriter(new File(filenameString)); void close( ); // works for Scanner, PrintWriters
Short Answer Questions 1. Please provide a brief answer to each of the following: (1 point) (a) How many bytes are in a gigabyte? How many bits? (1 point) (b) In what component of a desktop computer is most of the computation done? (1 point) (c) About how fast can the component you mentioned in question (b) compute? (1 point) (d) What s the primary job of the Java compiler? (Hint: it s not to find the errors in my syntax.) (1 point) (e) Write the single line of code which declares an int x so that x is only directly accessible within its own class. (1 point) (f) What does it mean to override a method? (1 point) (g) What does it mean to say that a method is abstract? (2 points) 2. If it takes me 3 seconds to sort 1000 data points using a bubble sort (which is an O(n 2 ) algorithm), roughly how long should it take me to sort 3000 data points using the same bubble sort on the same machine? (4 points) 3. For the array below, circle each of the cells that is checked when a program executes a binary search to find the value 27. -29-12 -10-1 2 12 23 24 36 54 What is the return value from the binary search? 3. out of a possible 13 1 of 12 exam continues...
Trace Me Questions (3 points) 4. What are the values of variables a, b, c, and d at Reference Point 1? 1 public class BooleanExpressions 2 3 public static void main(string [] args) 4 5 boolean P = true; 6 boolean Q = false; 7 boolean R = P!Q; 8 int X = 17; 9 10 boolean a = P && Q; 11 boolean b = (P && Q) (X > 10) 12 boolean c = P && (Q!R) 13 boolean d =!!Q; 14 // Reference Point 1 15 16 (4 points) 5. Answer the question below about this method definition: 1 public static void display(double x, char c) 2 3 int y = x * x; 4 5 if (y > x) 6 System.out.println(c); 7 else if (y < x) 8 9 if(c== A ) 10 System.out.println("found an A "); 11 System.out.println("" + y + " is less than " + x); 12 13 For each method call below, indicate what the method will display on the screen. Method Call display(2.0, A ); Displays on the screen display(0.5, 8 ); display(0.1, A ); display(0.0, D ); out of a possible 7 2 of 12 exam continues...
(3 points) 6. What s printed when we execute the following: 1 public class ForTest 2 public static void main(string [] args) 3 int x = 1; 4 for(; x < 0; x++) 5 System.out.println("x = " + x); 6 7 (3 points) 7. What s printed when we execute the following: 1 public class DoWhileTest 2 public static void main(string [] args) 3 int x = 1; 4 do 5 6 System.out.println("x = " + x); 7 x++; 8 while (x < 0); 9 10 (3 points) 8. What s printed when we execute the following: 1 public class WhatsPrinted00 2 public static void func(int x,int y,int z) 3 4 x *= 2; 5 y += x; 6 z = (x + y) % 2; 7 System.out.println(x + " " + 8 y + " " + z); 9 10 11 12 13 public static void main(string args[]) 14 15 int x = 10, y = 20, z = 30; 16 func(z, x, y); 17 18 (3 points) 9. What s printed when we execute the following: 1 public class WhatsPrinted01 2 public static void func(int x,int y,int z) 3 4 x *= 2; 5 y += x; 6 z = (x + y) % 2; 7 8 9 public static void main(string args[]) 10 11 int x = 10, y = 20, z = 30; 12 func(z, x, y); 13 System.out.println(x + " " + y + " " + z); 14 15 16 out of a possible 12 3 of 12 exam continues...
(3 points) 10. What s printed when we execute the following: 1 public class WhatsPrinted02 2 public static void func(int A[]) 3 for (int i=1; i<a.length; i++) 4 A[i]+=A[i-1]; 5 6 7 public static void main(string args[]) 8 int A[] = 10,20,30; 9 func(a); 10 System.out.println(A[2]); 11 12 (3 points) 11. What s printed when we execute the following: 1 public class WhatsPrinted03 2 3 public static int func(int A[], int B[]) 4 5 A = B; 6 return A[1]; 7 8 9 public static void main(string args[]) 10 int A[] = 10,20,30; 11 int B[] = 40,50,60; 12 13 int x = func(a, B); 14 System.out.println(x + " " + A[1]); 15 16 (3 points) 12. What s printed when we execute the following: 1 class Stuff 2 int x; 3 4 public Stuff(int x) 5 this.x = x; 6 7 public void move() 8 x++; 9 10 11 12 13 14 15 public class WhatsPrinted05 16 public static void func(stuff s) 17 s.move(); 18 19 20 public static void main(string args[]) 21 Stuff a = new Stuff(10); 22 Stuff b = a; 23 24 func(a); 25 b.move(); 26 System.out.println(a.x); 27 28 out of a possible 9 4 of 12 exam continues...
(4 points) 13. The following two classes together have 9 different compile-time (syntax) errors. Circle the parts of the code where 4 of them occur (do not circle more than 4, you do not need to find them all). For each error that you circled, write a brief explanation of what the error is. 1 public class WillNotCompile 2 3 private int myfield = 3; 4 5 public static WillNotCompile(double initval) 24 public static void main(string [] args) 6 7 myfield = this.initval; 8 9 10 public void getfield() 11 12 return myfield; 13 14 15 public static int setfield(int newval) 16 17 this.myfield = newval; 18 19 20 21 22 public class AlsoWillNotCompile 23 25 26 WillNotCompile obj = 27 new WillNotCompile(7); 28 29 obj.setfield(10); 30 31 obj.myfield++; 32 33 int final = WillNotCompile.getField() + 5; 34 35 System.out.println(final); 36 37 out of a possible 4 5 of 12 exam continues...
(6 points) 14. Given the following class definitions: 1 class Lack extends Liatorp 2 public void b() 3 System.out.println("Lack b"); 4 5 6 class Liatorp 7 public void a() 8 System.out.println("Liatorp a"); 9 10 public void b() 11 System.out.println("Liatorp b"); 12 13 public String tostring() 14 return "Liatorp"; 15 16 17 class Ektorp extends Kvissle 18 public void a() 19 System.out.println("Ektorp a"); 20 21 22 class Kvissle extends Lack 23 public void a() 24 System.out.println("Kvissle a"); 25 26 public String tostring() 27 return "Kvissle"; 28 29 IfwehaveLiatorp[ ] theflurgs = new Ektorp(), new Kvissle(), new Liatorp(), new Lack(), what s printed when we execute: (a) System.out.println(theFlurgs[0]); (b) System.out.println(theFlurgs[1]); (c) System.out.println(theFlurgs[2]); (d) System.out.println(theFlurgs[3]); (e) theflurgs[0].a(); (f) theflurgs[1].a(); (g) theflurgs[2].a(); (h) theflurgs[3].a(); (i) theflurgs[0].b(); (j) theflurgs[1].b(); (k) theflurgs[2].b(); (l) theflurgs[3].b(); (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) (k) (l) out of a possible 6 6 of 12 exam continues...
Coding Questions (8 points) 15. (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters...). For example, if s is "Too bad Spongebob s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too bad..." If s is shorter than max characters, return the original String. If max is less than 3 and less than the length of s, return the empty String. (2 points) (b) Write a main method that calls the abbreviate method twice, and prints the result to the screen each time. The first call should abbreviate the String "How now, brown cow?" down to 10 characters. The second call should abbreviate the result of the first abbreviation down to 5 characters. out of a possible 10 7 of 12 exam continues...
Secret Message (12 points) 16. Below is a partially filled-out CipherDocument class that contains a String [] lines as a field. Each element of lines contains one line of text in the document. But there is a secret message encoded in the CipherDocument! Write an instance method called getsecretmessage() that returns a String containing the second character of each word in the document, or a space if the word is only one letter long. Hint: Recall that you can construct a Scanner object on a String. That way you can find all of the words inside of the String by repeatedly calling the next() method. After this code runs, it should display "easy problem" on the screen. public class CipherDocument private String [] lines; public CipherDocument(String [] doclines) this.lines = doclines; public static void main(string [] args) String [] doc = "He sat us by a spinning drum.", "You obstinate old eel, Emma!"; CipherDocument cd = new CipherDocument(doc); String secretmessage = cd.getsecretmessage(); System.out.println(secretMessage); // your definition of the getsecretmessage() method goes here out of a possible 12 8 of 12 exam continues...
GradeBook 17. Below is a partially filled-out GradeBook class, which stores the names and grades of students in a course. Names are stored in an array of Strings. Grades are stored in a two-dimensional array of doubles, one row for each student, and one column for each test. public class GradeBook private String [] names; private double [][] grades; public GradeBook(String [] names, double [][] grades) this.names = names; this.grades = grades; // additional methods that you will write (see below) will go here (2 points) (a) Write a getter (or accessor) method for the GradeBook class that takes an int i as argument, and returns the name of the i th student. (5 points) (b) Implement an instance method which is passed an int row as an argument. The method should return the average of the grades on that row. out of a possible 7 9 of 12 question 17 continues...
(5 points) (c) Implement an instance method that determines the row of grades with the highest average. The method should return the row number. You may call the method from part b in your answer to this question. (3 points) (d) Write a class called Teacher that has just a main method. The main method should construct a GradeBook for a class with two students, "Jack" and "Jill". Jack s grades on the three tests for the class are 33, 55, and 77. Jill s grades are 99, 66, and 33. After constructing the GradeBook object, main should use the instance methods of the GradeBook class to determine the name of the student with the highest average grades. Print the name of this student on the screen. out of a possible 8 10 of 12 exam continues...
18. Consider the CellPhone class below. public class CellPhone private String phonenumber; // stores e.g. "215-555-1234" private String carrier; // stores e.g. "Verizon" private boolean incall = false; // stores whether the phone is currently in a call private String connectedto = null; // the number of the other phone being called public CellPhone(String number, String carrier) this.phonenumber = number; this.carrier = carrier; public void initiatecall(string otherphone) incall = true; connectedto = otherphone; public void stopcall() incall = false; connectedto = null; (3 points) (a) Write a tostring method for the CellPhone class. It should return something like "Verizon, 215-555-1234". out of a possible 3 11 of 12 question 18 continues...
(5 points) (b) Write a subclass of the CellPhone class called SmartPhone that will allow the client class PhoneUser below to execute. SmartPhone should have two new fields: one that stores the name of the type of phone (either "ios" or "Android"), and one that stores an array of names for Apps that are stored on the phone. The SmartPhone class should also have a constructor that does everything that the superclass s constructor does, plus it should initialize the type of phone and the array of Apps. public class PhoneUser public static void main(string [] args) String [] apps = "SnapChat", "Doodle Jump"; SmartPhone phone = new SmartPhone("267-222-9876", "AT&T", "ios", apps); phone.initiatecall("610-999-0001"); phone.stopcall(); System.out.println(phone); (1 point) (c) Do you need to add a stopcall or initiatecall method to the SmartPhone class in order to make the PhoneUser class run properly? Why or why not? (1 point) (d) What does the PhoneUser class display on the screen when it is executed? (2 points) (e) What would we need to add (if anything) to the SmartPhone class in order for the command System.out.println(phone); to display everything that the CellPhone class s tostring method returns, plus the type of the SmartPhone object ("ios" or "Android")? out of a possible 9 12 of 12 end of exam