SAMPLE
PRELIM 1 – CS100J – FALL 2002
Question 1 Trace the
sequence of assignments to variables made by the program segment given below
when it is run with input data 5.
Repeated assignments of the same value to a variable should be repeated
in the table. You may not need all of
the table columns that have been provided.
Assume that variable in has been declared such that expression in.readInt()
inputs one integer value.
int p =
in.readInt();
int d = 0;
int n = 0;
while ( p
> 1 ){
d
= 2;
while ( (p % d) != 0 ) d = d + 1;
if ( d == p ) n = n + 1;
p
= p - 1;
}
System.out.println(n);
time à
|
p |
? |
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d |
? |
|
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n |
? |
|
|
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
–––––––––––––––––––––––––––––––––––––––––––-––––––––-––––––––––––––
Question 2 Complete the program segment according to the following input/output specification.
Input. The input of your program consists of two or more positive integers terminated by a -1. The positive integers specify one or more runs,
where a run is an sequence that begins and ends with the same value. In particular, the first integer of the
first run (if there is at least one run) is given by the first integer in the
input; the remaining values of the first run are given by subsequent integers
up until the first integer of the run is repeated. The first integer of the
second run (if there is a second run) is given by the next integer, and so on.
Here is some sample input consisting of three runs of length 5, 4, and 2,
respectively.
10 50 50 20 10
50 60 75 50
50 50
-1 -1
Notice that runs are as short as possible, e.g., we do NOT say that the above input consists of two runs of lengths 5 and 6, respectively. Assume that the input is well formed, i.e., no run is left open (e.g., 10 -1).
Output. Output the number of runs and
the average length run (rounded to the nearest whole number). For the sample
input given above, the output would be:
Number of runs: 3
Average run length: 4
-------------------------------------------------------------------------------------------------------------------------------------------
TokenReader in = new TokenReader(System.in);
int first = in.readInt(); // first integer of the current run.
int next = in.readInt(); // next integer of the current run.
int length = 2; // length of the
current run so far.
int sum = 0; // total
length of completed runs so far.
int nRuns = 0; // number of
completed runs so far.
while (
________________________________________ ) {
if (_______________________________________________) {
}
else
{
}
next
= in.readInt();
}
System.out.println(
_______________________________________________________ );
System.out.println(
_______________________________________________________ );
Question 3 Complete the program segment according to the following input/output specification.
Input. The input consists of a non-negative integer n followed by n additional non-negative integers. For example,
4 5 3 0 2
Output. Output n lines of stars (*'s), where the number of *'s in a line is given by the corresponding integer in the input. For example, for the input given, the output would be:
*****
***
<a blank line>
**
-----------------------------------------------------------------------------------------------------------------------------------------------
TokenReader in = new
TokenReader(System.in);
int n = in.readInt(); // number of lines of output requested.
// other declarations
// statements
Question 4
a) Draw a picture of the "world,” i.e., all objects, variables and methods at the moment the following program reaches the line indicated. Use the diagramming technique illustrated in lecture:
· · Draw objects as rectangles with dashed borders containing the object's instance variables.
· · Each object should contain two compartments separated by a dashed line.
· · The upper compartment should contain the object's instance variables; the lower compartment should show the object's instance methods.
· · Each variable should be drawn as a solid rectangle. The name of the variable should appear to the left of the rectangle, and the contents of the variable should be written in the rectangle.
· · Draw object references as arrows pointing to the objects to which they refer.
b) What is the output of method "main" below?
public class Q1 {
private static int a = 1;
private int b;
private Q1 c = null;
public Q1() {
b = a;
a = 2*a;
}
public String toString() {
String s = "null";
if (c != null) s =
"" + c.b;
return a + " " + b
+ " " + s;
}
public void magic(int a) {
Q1 now = this;
while (now != null) {
System.out.println(a + " " + now);
now = now.c;
}
}
public static void main (String[] args) {
Q1 a = new Q1();
Q1 b = new Q1();
Q1 c = new Q1();
c = a;
a.c = b;
c.c.b++;
// draw a picture of "the world" as it exists at
THIS LINE
c.magic(7);
}
}
Question 5
a) In the code for Q4 above, label each variable declaration with a unique integer. Then label each variable use throughout the code with the integer of the corresponding declaration. This will illustrate your understanding of the scope of names in Java.
b) Does method "toString()" above return a value or print a value? How --Java willing-- would you change the code for method "toString()" to make it do the other (print instead of return or vice versa)?
c) Define the term "class".
d) Define the term "method".
e) Explain the difference between public and private.
f) Explain the differences between static and non-static.
Question 6
a) What is the pattern for repeatedly doing something N times? The pattern for processing an input sequence of pairs of values terminated by a pair of stopping values?
b) Androids Trurl and Klapaucius perform a random walk on a grid. Both begin at square (0,0). At each step, they independently move one square to the right and one square up or down, e.g., from (0,0), each android will move independently to either (1,1) or (1,-1). Trurl's probability of moving up is 60%; Klapaucius' probability of moving is 45%.
Write a code segment to simulate 200 steps of their random walk and count how many times they meet during their walk (how often they are standing on the same square). Output this number to the screen. Do not include their starting position in your count.
HINT: The expression Math.random() <= 0.4 will be true with probability 40%. Function Math.random() returns a random number strictly between 0.0 and 1.0.