CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011 What is your name?: This test has the following sections: I. True/False... 60 points; (30 questions, 2 points each) II. Multiple Choice... 36 points; (6 questions, 6 points each) --------------- 96 + 4 points for free = 100 points total This test is worth 15% of your final grade. You must put your answers on the bubble form. All code is in Java unless stated otherwise. This test is open book and open notes. For the multiple choice problems, select the best answer for each one and select the appropriate letter on your answer sheet. Be careful - more than one answer may seem to be correct. Some questions are tricky. You have 50 minutes. I. True/False: (2 points each) On your bubble form fill out a for true and b for false. T F 1. T F 2. T F 3. T F 4. T F 5. T F 6. T F 7. T F 8. T F 9. Any program can be written using only a sequence of instructions with if statements and looping statements, without variables, assignment statements, or methods. Any program can be written using only a sequence of instructions with if statements, looping statements and methods, without variables or assignment statements. The contents of two strings can be compared using either double equals (==) or the equals() method. Any code that can be written with multiple if-else statements could also be written with a switchcase statement. Java variable names must begin with an alphabetic character, $ or _ Private instance variables can only be accessed directly through code in the class where they are declared. Private instance variables can be accessed indirectly through code in another class, as long as the method in the other class using these instance variables is also marked as private. Under some circumstances two methods in Java can have the same name and same number of parameters. Two methods in Java can have the same name, same number of parameters with the same types as long as the return type is different. T F 10. The following is a valid declaration in Java: String Int = "Char"; T F 11. If tostring() is not implemented in a class, then printing a class object displays the object s class name and address in memory. CS 102/107, Midterm #2, Spring 2011 page 1
T F 12. The section of code shown below would compile and run and give as output: 6 int sum = 0; for( int i=0; i<3; i++) sum += i; System.out.println( sum); T F 13. The section of code shown below would compile and run and give as output: Is 3 Done int x = 4; if( x = 3) System.out.print("Is 3 "); else System.out.print("Is 4 "); System.out.println("Done"); T F 14. The following statements compile and run in Java: char[] letters = 'Y','e','s'; if( letters.length() == 3) System.out.println( letters[ 3]); T F 15. The following code prints the words: undecided char c='a'; switch (c) case 'a': System.out.print("u"); case 'b': System.out.print("n"); case 'c': System.out.print("d"); case 'd': System.out.print("e"); System.out.println("cided"); T F 16. The following statements compile and run in Java: String last = "first"; if( last.equals( "first")) System.out.println("first = last"); T F 17. In order for the code shown below to compile and run properly, a copy constructor for the Date class must have been created by the user. Date d1 = new Date(2,14,2000); Date d2 = new Date( d1); T F 18. A chained constructor call must use this and must be the first action within the calling constructor. T F 19. When writing a class that includes instances of a class someone else has written, you will not be able to write a copy constructor unless that other class already implemented a copy constructor. CS 102/107, Midterm #2, Spring 2011 page 2
T F 20. Given the final version of the Date class discussed in our lectures, the follow code will give the output: Are equal Date d1 = new Date( 1,1,2010); Date d2 = new Date( d1); if( d1==d2) System.out.println("Are equal"); else System.out.println("NOT equal"); T F 21. Assuming the final version of the Date class as developed in our lectures, the tostring method gets called automatically in the following code: Date d3 = new Date(); System.out.println("Date is: " + d3); T F 22. Given the final version of the Date class discussed in our lectures, the follow code will give the output: Nov/2/2010 Date d1 = new Date( "Nov",1,2010); Date d2 = d1; d1.setday(2); System.out.println( d2); T F 23. The following Java code would compile, run, and display a value for x: int[] values = 8,3,2,6; int x = values[ values[2] ]; System.out.println( x); T F 24. When a method sends parameters to a second method which changes those parameter values, those changed values are not reflected back to the calling method. T F 25. When a method sends an array to a second method which changes the values in the array, the original array values are restored once control is returned back to the calling method. T F 26. Typically an n-dimensional array in Java will use n sets of square braces [] every time we want to read from or write to an array element. T F 27. In a Java program using arrays, the maximum size of an array must be known before we begin running the program. T F 28. If an array is explicitly initialized, the size doesn't need to be given since Java computes it automatically. T F 29. The size of an array could itself be a variable, where the value of the variable isn't known until the program runs. T F 30. Once an array is created in Java the grow( n) command can be used to add n elements to the array size. CS 102/107, Midterm #2, Spring 2011 page 3
II. Multiple Choice (6 pts. each) 31. Consider the class given below, along with the driver class for it. class ClassA int x; public ClassA() x = 4; addvalue( x); public void addvalue(int val) x = x + val; //end ClassA class ClassADriver public void doit() int value = 2; ClassA instance1 = new ClassA(); instance1.addvalue( value); System.out.println("value is: " + instance1.x); //end ClassADriver When running method doit() in the ClassADriver class, the output will be: a) value is: 4 b) value is: 6 c) value is: 8 d) doesn t compile 32. Assume that you create class Employee that includes an instance of class Date to store the startdate for each employee. Assume that you have written some test code in class EmployeeDriver shown below, where you change the startdate for e1. To your surprise when you run this code the startdate for e2 has changed as well. What is the most likely explanation for this? class EmployeeDriver public static void main(string[] args) Employee e1 = new Employee(); Employee e2 = new Employee( e1); e1.changedate( 11, 1, 2010); System.out.println(e1); System.out.println(e2); a) The Date class fields are declared as static b) The Date class copy constructor does not chain to the fully qualified constructor c) The new value happens to be the same as the default value d) The Employee copy constructor does not create a new Date CS 102/107, Midterm #2, Spring 2011 page 4
33. Assume an instance of class Confusing is created, and that instance is used to call method doit(). What is the output? class Confusing int x; int y; public Confusing() x = 3; y = 5; void first( int x, int b) int temp = x; y = b; b = temp; //end method first void second( int x, int y) first( x, y); x = y + 1; //end method second public void doit() int x = 1; int y = 3; first( x,y); second( y,x); System.out.println("Answer is: "+ this.x + " " + this.y); //end method doit() a) Answer is: 3 5 b) Answer is: 3 4 c) Answer is: 3 3 d) Answer is: 1 3 CS 102/107, Midterm #2, Spring 2011 page 5
34. What is the output from running the code in the following Maze program? public class Maze1 private static int startrow = 1; // starting row private static int startcol = 1; // starting column private int endrow = 8; // ending row private int endcol = 8; // ending column // Maze is a 2-D array where 1 is a wall, 0 is an open pathway. private int[][] maze = /* 0 1 2 3 4 5 6 7 8 9 */ /* 0 */ 1,1,1,1,1,1,1,1,1,1, /* 1 */ 1,0,0,0,1,1,1,1,1,1, /* 2 */ 1,1,0,1,1,1,0,0,1,1, /* 3 */ 1,1,0,1,1,1,0,1,1,1, /* 4 */ 1,1,0,0,0,0,0,1,1,1, /* 5 */ 1,1,1,1,1,0,1,1,1,1, /* 6 */ 1,0,1,1,1,0,1,0,0,1, /* 7 */ 1,1,1,1,1,0,1,0,1,1, /* 8 */ 1,1,0,0,0,0,0,0,0,1, /* 9 */ 1,1,1,1,1,1,1,1,1,1 ; public static void main(string[] args) Maze1 instanceofmaze1 = new Maze1(); instanceofmaze1.makemove( startrow, startcol); public void makemove( int row, int col) while( true) System.out.print("[" + row + "," + col + "], "); if (( row == endrow) && ( col == endcol)) System.out.println("\n Got to destination! "); break; if ( maze[ row+1][ col]!= 1) row++; continue; if (maze[ row][ col+1]!= 1) col++; continue; if (maze[ row-1][ col]!= 1) row--; continue; if (maze[ row][ col-1]!= 1) col--; continue; //end makemove //end class Maze1 a) The solution path in order b) The solution path with some additional output at the end c) Output that terminates, but not the solution path d) Output that is in an infinite loop CS 102/107, Midterm #2, Spring 2011 page 6
35. Consider method second shown at right, which itself uses method first. For positive numbers, how would you best describe its return value? a) x + y b) x * x c) x * y d) x y public int first( int x, int y) int z = 0; for( int i=0; i<x; i++) z += y; return z; public int second( int x, int y) int z=1; for( int i=0; i<y; i++) z = first( z, x); return z; 36. Recursion is when a method calls itself. It behaves the same as if there were unlimited copies of the same method, where each has its own copy of parameters. What is the output of the following line of code: System.out.print( mystery1( 314159) ); where method mystery1( ) is shown below, and user input is: 314159 int mystery1( int x) if( x < 10) return x; else return mystery1( x/10); a) 3 b) 31415 c) 9 d) 14159 CS 102/107, Midterm #2, Spring 2011 page 7