Name Signature Tutor Student ID CSE 8B Midterm Fall 2015 Page 1 (XX points) Page 2 (XX points) Page 3 (XX points) Page 4 (XX points) Page 5 (XX points) Total (XX points)
1. What is the Big-O complexity of the following code segments? for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { if(i == j) { System.out.println( match ); for(i = 0; i < 10; i++) { for(j = 0; j < n; j++) { if(i == j) { System.out.println( match ); for(i = 0; i < n/2; i++) { for(j = 0; j < n/2; j++) { if(i == j) { System.out.println( match ); 2. Draw the order of methods on the stack up to the indicated point of program execution, method2. Note: the stack may not fill completely, also Stacks fill from the bottom to the top: public class Example { private static int size = 4; public static void main (String[] args) { Example ex1 = new Example(); ex1.method3(); ex1.method4(); public void method1() { Example ex2 = new Example(); ex2.method2(); public void method2() { // DRAW MEMORY HERE public void method3() { method1(); public void method4() { return;
3. Given the following code: import java.util.scanner; public class DogYears { final static int AGE_FACTOR = 7; Scanner scnr = new Scanner(System.in); String name = Scruffy ; int dogageyears = 0; double result = 0; System.out.print("Enter dog's age (in years): "); dogageyears = scnr.nextint(); result = dogageyears * AGE_FACTOR; System.out.println(result); In which part of memory are the contents of the following variables stored (static, stack, or heap) a) AGE_FACTOR b) scnr c) name d) dogageyears e) result 4. What is the output from the following code: public class Question4 { Chalk c1 = new Chalk( white ); Chalk c2 = new Chalk( green ); Chalk c3 = c1; c1.write( midterm ); c2.write( test ); c3.write( Q4 ); public class Chalk { private static int numpieces = 0; private String color; public Chalk(String color) { this.numpieces++; this.color = color; public static int getnumpieces() { return numpieces; public void write(string word) { System.out.println(numPieces + / + color + / + word);
5. The following Unix commands are typed into the Unix terminal in the order that they are shown. Answer the following questions below. Assume that all Unix commands in the box have been completed. Also assume that when a file is opened, the file is created, saved and closed. If you use emacs, imagine the vim command is just emacs and the same result occurs (file is created, saved, closed). vim Apple.java mkdir Dir1 cd Dir1 vim Pear.java vim Peach.java cd.. mkdir Dir2 cd Dir2 vim Orange.java What is the output if the user types ls? What single command will copy both Pear.java and Peach.java into the current directory? The user switches into Dir1 and runs a mystery command. The output of the command is: /home/linux/ieng6/cs8b/cs8bxx/dir1 What is the mystery command? vim Grape.java vim Cherry.java 6. What gets printed in the following code: public void question6() { try { System.out.println(1); statement2; //causes Exception System.out.println(3); catch(exception e) { System.out.println(4); System.out.println(5); public void question6() throws Exception { System.out.println(1); statement2; //causes Exception System.out.println(3); System.out.println(4); System.out.println(5);
7. Explain when you would use each of the following classes: a. PrintWriter b. Scanner c. FileInputStream d. FileStream e. System (e.g. System.in or System.out) f. BufferedInputStream g. BufferedStream 8. Draw the UML diagram for the following class: public class Chalk { private int numpieces = 0; public String color; public Chalk(String color) { this.numpieces++; this.color = color; public int getnumpieces() { return numpieces; private void write(string word) { System.out.println(numPieces + / + color + / + word); 9. What is the output from the following program: public class Example { private int num; private char ch; public Example() { this(1); ch = A ; System.out.println( C1 ); public Example(int num) { this.num = num; ch = B ; System.out.println( C2 ); public void print() { System.out.println(ch); System.out.println(num); Example ex1 = new Example(2); Example ex2 = new Example(); ex1.print(); ex2.print();
10. What is the output from the following code: public class Question10 { Animal a1 = new Animal( Zebra ); Animal a2 = new Animal( Monkey ); Animal a3 = a1; setage(10, a1); setage(20, a2); setage(30, a3); a1.print(); a2.print(); a3.print(); public static void setage(int age, Animal a) { a.setage(age); public class Animal { private String type; private int age; public Animal(String type) { this.type = type; this.age = 0; public void setage(int age) { this.age = age; public void print() { System.out.println(type + age);