NAME |
___________________________
|
Perm. no. |
_________________ |
CS 10: Introduction to Computer
Programming
Final Examination
Closed-Book, 3 hours, Winter 1998
General Instructions
-
Before you answer any questions, write your name and perm number.
-
Read each question carefully. Make sure that you clearly understand
each question before answering it.
-
Put your answer to each question on its own page.
-
You may wish to work out an answer on scratch paper before writing it on
your answer page; answers that are difficult to read may lose points for
that reason.
Question |
Value |
Score |
1 |
10 |
|
2 |
10 |
|
3 |
15 |
|
4 |
25 |
|
5 |
10 |
|
6 |
30 |
|
Total |
100 |
|
1. (10 points) For the Color constructors below, indicate whether
there is a compile-time error, an execution-time error, or that the constructor
will work. Assume the following declaration and initialization:
Constructor |
Error (Compile, Execution, or None)
|
Color(1, 2, r + 3) |
|
|
|
Color(0.9, 0.9, 0.9) |
|
|
|
Color(r, r, r) |
|
|
|
Color(0, 200, r + 0.5) |
|
|
|
Color(0, r, (float) 0.125) |
|
|
|
Color(1, 2, 3) |
|
|
|
Color(0, (int) r, 225) |
|
|
|
Color((float) 2, 1, 0) |
|
|
|
Color((int) 256.78, 0, 25) |
|
|
|
Color(-1, 0 , 1) |
|
|
|
2. A.
(5 points) Describe the difference between a static method
and one that is not static.
B. (5 points)
Would it ever make sense to create a private static method?
Explain.
3. A. (7 points)
You are given the following declaration.
int s, n, delta;
Give a java expression that evaluates to a random element in
the set:
{s, s + delta, s + 2*delta, s + 3*delta, ..., s + n*delta}.
For example, if s = -15, n = 4, and delta = 10, then
your expression evaluates to a random element in the set
B. (8 points) Give a java expression that evaluates to a random
element in the set: {1, 2, 4, 8, 16, 32, 64}.
You get 6 points, if you can give java statements to do this (but not
a simple expression).
4.
-
(3 points) Write a method abs whose
only parameter, i, is a double and which returns a double
whose value is the absolute value of i. For example, abs(-3.0)
would return a double of value 3.0; abs(3.0) would return
a double of value 3.0.
Instead of writing this method, you can give a
pre-defined method, if you know one.
-
(10 points) Using the method abs (even
if you did not answer part 1), write a method called max that has
an array, a, of double as a parameter, and returns a double
whose value is the absolute value of the element whose absolute value is
maximum over all the elements in a. For example, if max
were passed the array whose values are {-3.0, 4.0, -5.0, 6.0, -7.0}, then
max would return a double whose value was 7.0.
-
(10 points) Using the method max (even
if you did not answer part 2), write a method maxes that has 2 parameters:
-
a 2-dimensional array b of doubles
-
a 1-dimensional array c of doubles
Method maxes, for each element, i,
of c, puts the maximum absolute value of the elements in row b[i]
into c[i]. If b[i] does not exist, then set c[i]
to -1. Method maxes has no return value. For example,
given the declarations:
double c[] = new int[3],
b[][] = {{1, 2, 3}, {-1, -2, -3}}
Then maxes would put 3 into c[0],
3 into c[1], and -1 into c[2].
-
(2 points) When maxes gets invoked
with its 2 array arguments, does the array corresponding to c actually
get changed by the maxes method?
5. (5 points) Given the
following java declaration and initialization,
int x[][] = {{-1, 0, 1, 2, 3}, {-4, -5,
-6, -7}, {8, 9, 10}}
What is the value x[x.length-1][x[x.length-2].length-3]? It may
be a good idea to show your work.
6. A. (20 points) Define a class called
Node that will be used to construct a list of nodes, each representing
a square. Each Node has the following private attributes:
-
an int x coordinate of the location
of its square
-
an int y coordinate of the location
of its square
-
an int e representing the edge length
of its square
-
a Color c representing the color of
its square
-
a Node object, called next, representing
the next node in the list.
It has the following methods:
-
a constructor that takes 4 paramenters: 3 ints
and a Color that correspond to its attributes. It sets the
attributes of the Node being constructed to the values of the parameters
passed. It leaves its next attribute null (not referring to
any Node object).
-
public get and set methods for each
of its attibutes.
-
a public draw method that takes a Graphics
object, g, as a parameter, and draws the corresponding filled square
on g.
-
a public insert method that takes as a parameter
a Node d and:
-
sets d's next attribute to refer to
whatever this Node's next attribute referred to,
-
sets this Node's next attribute to
refer to d,
-
returns a reference to the updated Node.
For example, if we had:
Node a = new Node(3,4,5,Color.red), b
= new Node(6,7,8,Color.black);
and we executed:
then a's next field would refer to
b; b's next field would still be null, and the returned value
would be a reference to a.
B. (10 points) Make the color of a
square a public static attribute of the class Node
(rather than an attribute of Node objects). Give the changes
that need to be made to the Node class. How does the resulting
class behave differently?