Sample
Prelim 1 Questions (FROM CS100A, Fall 1999)
Notes:
__ Read ALL directions. In particular, when we give you a checklist, make sure
you observe each requirement!
__ This is not a sample prelim: It is too long.
1. (Short Answer) Each line in the program segment below has something wrong
with it. Fix each line.
int 3 = x;
if (7 - x = 4)
System.Out.Println("four is " + 4);
int x = "4";
if (3 < 4, 4 < 5)
System.out.println(4 is between 3 and 5);
2. (Short Answer) Briefly answer each question below.
(a) What is the difference between "=" and "=="?
(b) What is the difference among the following three statements?
i) int x; ii) int x = 3; iii) x = 3;
(c) Is there any difference between "fred" and "Fred"?
(d) Assume the code below is correct. Indent it properly. Note that there is
more than one correct answer.
while (x<0) { counter *= 10; if (y<z) y++; else z = z*2; x = x+2; w =
w/7; }
3. (Tracing) Trace the code segment below -- fill in the table below using the
format of one column per assignment, one entry per column. The first three
assignments have already been done for you.
int n = 19, x = 0, y = 1;
while (n != 0)
if (n % 2 == 0)
n = n/2;
else {
n = n-1;
x = 10*x + 3;
y = y*10;
}
y = (y-1) / 3;
+----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
n | 19 | | | | | | | | | | | | | | | | | |
+----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
x | | 0 | | | | | | | | | | | | | | | | |
+----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
y | | | 1 | | | | | | | | | | | | | | | |
+----+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
4. (Iteration) Write a program segment to read a positive integer n from input
and print a hollow n-triangle. Examples for 1, 2, 3, 4, 5 are shown below.
__ Assume TokenReader in = new TokenReader(System.in); has already been
included.
__ Use good style
__ Clearly indicate in a comment how many "hollow" spaces you intend
to print on each line.
__ Do NOT use Format.print.
*
* **
* ** * *
* ** * * * *
* ** *** **** *****
5. (Iteration) Same as question 4, but orient the triangles as shown below, and
include the additional requirement below.
__ Clearly indicate in a comment how many "padding" spaces you intend
to on each line before the first star.
* ** *** **** *****
* ** * * * *
* ** * *
* **
*
6. (Processing Input) The Cornell psychology department is known in part for
its sleep lab. Write a program to help them out.
Write the program
__ using good style,
__ to read in a log of sleep sessions during one month,
__ and at the end of the month, print how many days the person
is asleep in 3 or more separate sessions during that day.
The input format is a sequence of integers in groups of 2 representing sleep
sessions in chronological order.
__ The first integer is the day the sleep session starts.
__ The second integer is the length of the session in minutes.
__ The sequence is terminated by 0 0.
__ For simplicity, the person is always still awake at midnight, so that no
sleep session starts on one day and ends on another day.