COS 126 Midterm 1 Written Exam Fall 2012
COS 126 Midterm 1 Written Exam Fall 2012
COS 126 Midterm 1 Written Exam Fall 2012
! ! Precept:
Print your name, login ID, and precept number on this page (now), and write out and sign the
Honor Code pledge before turning in this paper. Note: It is a violation of the Honor Code to
discuss this midterm exam question with anyone until aer everyone in the class has taken the
exam. You have 50 minutes to complete the test.
“I pledge my honor that I have not violated the Honor Code during this examination.”
Signature
1 /7
2 /9
3 /12
4 /6
5 /8
6 /6
7 /12
8 /10
TOTAL /70
1. Types and Casts (7 points). Give the value and type of each of the following Java expressions.
For any expression that will not compile, write X for its value and Illegal for its type.
“2445”
Illegal
1 = 2 X [Cannot assign
a value to 1]
2.0
3 * 0.5 + 3 / 2 * 0.5 double
[1.5 + (1 * 0.5)]
A. Which of the following are true of Java arrays? Circle all that apply.
(i) Array entries are auto-initialized to 0.0 when creating a new array of double
values.
(iii) Given an array a[] that has been declared and initialized, accessing a[a.length]
results in a runtime error.
(v) Can pass an array to a function and have that function change the values stored in the
array entries.
B. Which of the following best defines a data type? Circle the single best answer.
(v) e type of the arguments, method name, and the type of the return value for a
function.
C. Why might a program be written to take input from standard input rather than to take
command-line arguments? Circle the single best answer.
(i) To allow the usage of inputs that consist of more than one line.
(iii) To allow the program to interpret inputs adaptively according to prior inputs.
Each line of text on the le-hand side below represents the standard input for a run of ArrayQ.
In this problem, you must determine the output for each of these runs. On the right-hand side,
labeled from A to F, the correct outputs are given, but the order of the lines on the le is not the
same as the order of the lines on the right. Indicate which output matches each input by writing
the corresponding letter in each box. Each letter should appear in exactly one box.
6 5 4 3 2 1 C ! ! A. 5 4 6 3 2 1
1 3 5 7 2 4 F !B. 2 3 4 5 6 1
3 6 4 5 1 2 D !C. 6 5 4 3 2 1
6 3 1 5 2 4 E !D. 6 4 5 3 2 1
5 3 4 6 1 2 A !E. 6 3 5 2 4 1
1 2 3 4 5 6 B !F. 3 5 7 2 4 1
4. Redirection and piping (6 points). Consider the following Java program:
public class EZ
{
public static void main(String[] args)
{
int x = StdIn.readInt();
int y = Integer.parseInt(args[0]);
StdOut.print(x + y + " ");
StdOut.print(x + " ");
StdOut.println(y);
}
}
6 5 4 3
A. Write the result of executing the following command. Circle your answer.
9 6 3
B. Write the result of executing the following command. Circle your answer.
11 9 2
5. Debugging (8 points). Consider the following program, which is intended to compute the
mean of N integers from standard input, rounded to the nearest integer, where N is a command-
line argument.
13 }
14 }
In the spaces provided below, identify (in ten words or less, each) three bugs and a performance
problem in this code. Use the line numbers to refer to the code, and circle your answers. You do
not have to write code to fix the bugs.
C. A bug that will cause the program to print the wrong answer on many inputs:
12: sum/N integer division truncates rather than rounding.
Example fix, casting to double to do division and then rounding to nearest integer:
Math.round((double) sum / N)
N time
1 just over 30 seconds
100 ?
A. In the blank at right, give an estimate of the running time for N = 100: _about 1 hour_
B. Which of the following best describes the order of growth of the running time of this
program as a function of the size of its input? Circle your answer.
a. Logarithmic
b. Linear
c. Linearithmic
d. Quadratic
e. Cubic
a. Logarithmic
b. Linear
c. Linearithmic
d. Quadratic
e. Cubic
7. Methods (12 points). Consider the following program.
See next page (blank page from exam) for memory schematic trace through the program.
is page intentionally le blank. e exam continues on the next page.
8. Recursion (10 points). Consider the following program, which includes a recursive method:
A. Give the output generated when this program is compiled and run. Hint: Draw the
tree showing the recursive calls in the space provided, then write the output in the
underlined spaces provided at the bottom.
(2,0)
(1,0) (1,2) (1,1)
(0,0) (0,2) (0,1) (0,6) (0,8) (0,7) (0,3) (0,5) (0,4)
Output: _ 0_ _ 2_ _ 1_ _ 6_ _ 8_ _ 7_ _ 3_ _ 5_ _ 4_
C. What is the last number printed for the call R(5, 2)? ___ 607____
e last number printed is printed in the leaf that is reached by traversing from the root down
to the leaf level, taking the 3rd -- R(n-1, 3*t+1) -- call each time:
R(5,2)
R(4, 7)
R(3, 22)
R(2, 67)
R(1, 202)
R(0, 607)