CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2010 What is your name?: There are three sections: I. True/False..............60 points; (30 questions, 2 points each) II. Multiple Choice........40 points; (10 questions, 4 points each) --------------- 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. 1. In a Java program a method that does not return anything can either have a return type of void or can leave off the return type altogether. 2. The section of code shown below would compile and run and give as output: 3 int sum = 0; for( int i=0; i<=3; i++) sum += i; System.out.println( sum); 3. 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"); 4. Any code that can be written with multiple if-else statements could also be written with a switch-case statement. 5. The following statements compile but then give a run-time error in Java: char[] letters = a, b, c ; if( letters.length() == 3) System.out.println( letters[ 3]); CS 102/107 - Intro. to Programming, Midterm Exam #2, page 1 of 9
6. The following code prints the words: abcdefg 7. The following is a valid declaration in Java: int Class = 3; char c='a'; switch (c) case 'a': System.out.print("a"); case 'b': System.out.print("b"); case 'c': System.out.print("c"); System.out.println("defg"); 8. The following statements compile and run in Java: String first = "last"; if( first.equals( "last")) System.out.println("first = last"); 9. When writing a class that is to be used by others, you should at a minimum implement instance variables, the get and set methods, default constructor, fully qualified constructor, copy constructor, equals method, and the tostring method. 10. Two methods in Java can have the same name and same number of parameters. 11. 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); 12. A chained constructor call must use this and must be the first action within the calling constructor. 13. 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"); 14. If tostring() is not implemented in a class, then printing a class object displays the object s class name and address in memory. 15. 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); CS 102/107 - Intro. to Programming, Midterm Exam #2, page 2 of 9
16. 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); 17. Given the final version of the Date class discussed in our lectures, the follow code will give the output: Nov/1/2010 Date d3 = new Date( "Nov",1,2010); Date d4= new Date(d3); d4.setday(2); System.out.println( d3); 18. Private instance variables can only be accessed directly through code in the class where they are declared. 19. 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. 20. The following Java code would compile, run, and display a value for x: int[] values = 3,4,5,6; int x = values[ values[1] ]; System.out.println( x); 21. Every program can be written with all the looping code implemented using recursion, without any for, while, or do loops. 22. Recursive methods typically have a base case which is the code that stops the recursion. 23. 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. 24. 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. 25. An integer array called values could be sorted into ascending order using: Arrays.sort( values); 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. CS 102/107 - Intro. to Programming, Midterm Exam #2, page 3 of 9
27. Assuming method swap( ) is correctly implemented, the code shown below would sort values in an integer array into ascending order. public void bubblesort( int[] thearray) int pass, current; int size = thearray.length; for ( pass=0; pass < (size-1); pass++) for ( current=0; current < (size-1); current++) if ( thearray[ current] > thearray[ current+1]) swap( thearray, current, current+1); 28. If the cost of a swap( ) operation were high, then using Selection Sort would give better performance than using Bubble Sort. 29. In a Java program using arrays, the maximum size of an array must be known before we begin running the program. 30. An ArrayList can be used in Java to dynamically adjust the size of an array as the program runs. II. Multiple Choice (4 pts. each) 31. Consider the code given below. If its output is: 12 16 20 15 20 25 18 24 30 21 28 35 what are the values for variables start, end, first and last? for( int i=start; i<=end; i++) for( int j=first; j<last; j++) System.out.printf("%5d", i*j); System.out.println(); a) int start=7, end=3, first=6, last=4; b) int start=4, end=7, first=3, last=6; c) int start=7, end=4, first=6, last=3; d) int start=3, end=7, first=4, last=6; CS 102/107 - Intro. to Programming, Midterm Exam #2, page 4 of 9
32. When using a binary search to search for a particular key within a sorted array of 65 values, the maximum of comparisons the program would need to make before finding the key (if it is there) is: a) 4 b) 5 c) 6 d) 7 33. 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=x; for (int i=0; i<y; i++) z += 1; return z; public int second(int x, int y) int z=0; for (int i=0; i<y; i++) z = first( z, x); return z; 34. What is the output of the following line of code: System.out.print( mystery1() ); where method mystery1( ) is shown below, and user input is: O y e a h! public char mystery1() char returnvalue = ' '; String input = keyboard.next(); char c = input.charat(0); if (c!= '!') mystery1(); returnvalue = c; return returnvalue; a) O b) y c) h d)! CS 102/107 - Intro. to Programming, Midterm Exam #2, page 5 of 9
35. What is the output of the following line of code: System.out.print( mystery2() ); where method mystery2( ) is shown below, and user input is: O y e a h! public char mystery2() String input = keyboard.next(); char c = input.charat(0); if (c!= '!') c = mystery2(); return c; a) O b) y c) h d)! 36. Consider the class given below, along with the driver class for it. class ClassA private int x; public ClassA() x = 1; public void addvalue(int val) x = x + val; //end ClassA class ClassADriver public void doit() int value = 7; 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: 6 b) value is: 7 c) value is: 8 d) doesn t compile CS 102/107 - Intro. to Programming, Midterm Exam #2, page 6 of 9
37. 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 a, int b) int temp = x; x = y; y = 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); //end method doit() a) Answer is: 3 b) Answer is: 4 c) Answer is: 5 d) Answer is: 7 CS 102/107 - Intro. to Programming, Midterm Exam #2, page 7 of 9
38. 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 39. What is the output of the code given below when method usearray() is called? public void usearray() char[] thearray= 'o','b','f','u','s','c','a','t','e'; processarray( thearray); for(int i=0; i<thearray.length; i++) System.out.print( thearray[i]); public void processarray(char[] w) int x = w.length; char c; for( int i=0; i<x; i++) c = w[i]; w[i] = w[x-i-1]; w[x-i-1] = c; a) The contents of the original array in reverse order b) The contents of the original array in the original order c) The contents of the original array with half of the characters reversed d) The original array with characters rearranged so they are neither in the original nor reversed orde CS 102/107 - Intro. to Programming, Midterm Exam #2, page 8 of 9
40. Consider the following Maze program similar to the one covered in class: class Maze private static int start = 11; private static int end = 57; private int[] maze = /* + 0 1 2 3 4 5 6 7 8 9 */ /* 0 */ 1,1,1,1,1,1,1,1,1,1, /*10 */ 1,0,0,0,0,1,1,1,1,1, /*20 */ 1,1,1,0,1,1,1,1,1,1, /*30 */ 1,1,1,0,0,0,1,1,1,1, /*40 */ 1,1,1,1,1,0,1,1,1,1, /*50 */ 1,1,1,1,0,0,0,0,1,1, /*60 */ 1,1,1,1,1,1,1,1,1,1; private int[] moves = 10,1,-1,-10; public Maze() makemove( start); public void makemove( int current) System.out.print(current + ", "); if ( current == end) System.out.println("Got to destination!"); return; for (int i=0; i<4; i++) int next = current + moves[i]; if ((maze[next]!= 1) ) makemove( next); //end makemove //end Maze What is the output from creating an instance of Maze and running this code? a) The solution path in order b) The solution path with an infinite loop at the end going back and forth c) A bunch of numbers, but not the solution path d) An infinite loop near the beginning of the search CS 102/107 - Intro. to Programming, Midterm Exam #2, page 9 of 9