CS 102/107, Midterm #2, Spring 2011 page 1
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. Any program can be written using only a sequence of instructions with if statements and looping
statements, without variables, assignment statements, or methods.
T F 2. Any program can be written using only a sequence of instructions with if statements, looping
statements and methods, without variables or assignment statements.
T F 3. The contents of two strings can be compared using either double equals (==) or the equals()
method.
T F 4. Any code that can be written with multiple if-else statements could also be written with a switch-
case statement.
T F 5. Java variable names must begin with an alphabetic character, $ or _
T F 6. Private instance variables can only be accessed directly through code in the class where they are
declared.
T F 7. 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.
T F 8. Under some circumstances two methods in Java can have the same name and same number of
parameters.
T F 9. 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 2
T F 12. The section of code shown below would compile and run and give as output: 6
T F 13. The section of code shown below would compile and run and give as output: Is 3 Done
T F 14. The following statements compile and run in Java:
T F 15. The following code prints the words: undecided
T F 16. The following statements compile and run in Java:
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.
T F 18. A chained constructor call must use this and must be the first action within the calling con-
structor.
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.
Date d1 = new Date(2,14,2000);
Date d2 = new Date( d1);
String last = "first";
if( last.equals( "first")) {
System.out.println("first = last");
}
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");
char[] letters = {'Y','e','s'};
if( letters.length() == 3) {
System.out.println( letters[ 3]);
}
int x = 4;
if( x = 3)
System.out.print("Is 3 ");
else
System.out.print("Is 4 ");
System.out.println("Done");
int sum = 0;
for( int i=0; i<3; i++)
sum += i;
System.out.println( sum);
CS 102/107, Midterm #2, Spring 2011 page 3
T F 20. Given the final version of the Date class discussed in our lectures, the follow code will give the
output: Are 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:
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
T F 23. The following Java code would compile, run, and display a value for 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.
int[] values = {8,3,2,6};
int x = values[ values[2] ];
System.out.println( x);
Date d1 = new Date( "Nov",1,2010);
Date d2 = d1;
d1.setDay(2);
System.out.println( d2);
Date d3 = new Date();
System.out.println("Date is: " + d3);
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");
CS 102/107, Midterm #2, Spring 2011 page 4
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
e) None of the above
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?
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
e) None of the above
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);
}
}
CS 102/107, Midterm #2, Spring 2011 page 5
33. Assume an instance of class Confusing is created, and that instance is used to call method doIt().
What is the output?
a) Answer is: 3 5
b) Answer is: 3 4
c) Answer is: 3 3
d) Answer is: 1 3
e) None of the above
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()
}
CS 102/107, Midterm #2, Spring 2011 page 6
34. What is the output from running the code in the following Maze program?
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
e) None of the above
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
CS 102/107, Midterm #2, Spring 2011 page 7
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) xy
e) None of the above
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
a) 3
b) 31415
c) 9
d) 14159
e) None of the above
int mystery1( int x)
{
if( x < 10) {
return x;
}
else return mystery1( x/10);
}
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;
}