CS 10: Introduction to Computer Programming
Final Examination
Closed-Book, 3 hours, Fall 1997
General Instructions
-
Before you answer any questions, write your name and perm number
on your answer booklet.
-
Read each question carefully. Make sure that you clearly understand
a question before answering it.
-
Please put your answer to each question on its own page in your blue-book.
-
Answers that do not show work will not get partial credit.
-
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.
-
Each question is worth 10 points.
1. Describe the difference between public, private, and protected.
Ignore differences having to do with packages.
public class members
are accessible by name outside the class;
private class members
are not accessible by name outside the class;
protected class members
are accessible by name within the defining class and its subclasses.
2. What is the meaning of the keyword this?
Illustrate the meaning with a class definition that uses it.
The keyword this
is used when one wants an object to refer to itself . Here is a simple
example where an object's method returns the object itself (look at the
Solar System applet on the Lectures page for another example):
public class Complex {
private
double r, i;
public
Complex() { r = i = 0; }
public
double getR(){ return r; }
public
void setR(double R){ r = R; }
public
Complex add(Complex c) {
r += c.r;
i += c.i;
return this;
}
}
3. Describe what it means
when a data member is declared static.
Illustrate the meaning with a class definition that uses it.
Solution:
When a data member is declared
static, only one instance of it exists for the whole class, no matter how
many of the class's objects have been instantiated. This is useful,
for example, when a class constant is created. Since only
one is needed for the whole class, there is no need to instantiate such
a constant for every object in the class. Another example is when you want
to characterize something about the state of the class, such as a count
of how many of it's objects exist at any point in time. Here is a
simple example.
public class Student {
static int count;
private String name;
public Student(String NAME) {
name = NAME;
count++;
}
public void finalize() { count--; }
// other methods go here
}
4. Give an example of an overloaded constructor
(of your own design or from Java's class libraries).
Solution:
One example is the Color constructors. One
Color constructor takes 3 int arguments (each of whose values are in [0,
255]), and another that takes 3 float arguments (each of whose values are
in [0,1]).
5. Give a method that takes a 2-dimensional
array of double as an argument, and puts a random number in [0,
1) in each element.
void summer(double a[][]) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
a[i][j] = Math.random();
}
6. Give a method that takes a 3-dimensional
array of int as an argument, and returns the sum of the elements
whose indices have a sum that is evenly divisible by 3. For example,
if the array has element [1][1][1], then that element's value is part of
the sum, because 1 + 1 + 1 is evenly divisible by 3. On the other
hand, if the array has element [1][1][2], then that element's value
is not part of the sum, because 1 + 1 + 2 is not evenly divisible by 3.
int summer(int a[][][])
{
int sum = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
for (int k = 0; k < a[i][j].length; k++)
if ((i + j + k) % 3 == 0) sum += a[i][j][k];
return sum;
}
7. Give a method that has 2 int array arguments.
Assume that they each have at least 2 elements. The method puts the
largest element of the first array into the first element of the second
array, and the second-largest element of the first array into the second
element of the second array.
public void largest2(int
a[], int b[]) {
if (a[0] < a[1]) {
b[0] = a[1];
b[1] = a[0];
}
else {
b[0] = a[0];
b[1] = a[1];
}
for (int i = 2; i < a.length; i++)
if (a[i] > b[0]) {
b[1] = b[0];
b[0] = a[i];
}
else if (a[i] > b[1])
b[1] = a[i];
}
8. Give an applet that draws a 8 X 8
checker board (of black and red squares), where each square on the board
has an edge length of 10 pixels. The upper left corner of the board
has pixel coordinates (0, 0).
import java.awt.*;
import java.applet.*;
public class test extends Applet {
public void paint(Graphics
g) {
g.fillRect(0, 0, 80, 80);
g.setColor(Color.red);
for (int i = 0; i < 8; i++)
for (int j = i % 2; j < 8; j +=2)
g.fillRect(10*j, 10*i, 10, 10);
}
}
9. As applied to methods, what
is the meaning of the keyword super?
Illustrate the meaning with some class definitions. (Hint: at least 2 class
definitions are needed).
When a subclass method overrides a superclass
method, the superclass method may be accessed from the subclass by preceding
the superclass method name with super followed by the dot operator.
A superclass constructor can be invoked simply by substituting super
for the name of the superclass.
import java.awt.*;
public class Box {
protected int x, y, edge;
public Box(int X, int Y, int E) {
x = X;
y = Y;
edge = E;
}
public void draw(Graphics g) {
g.setColor(Color.blue);
g.fillRect(x, y, edge, edge);
}
}
import java.awt.*;
public class OutlinedBox extends Box {
public OutlinedBox(int
X, int Y, int E) {
super(X, Y, E);
}
public void draw(Graphics
g) {
super.draw(g);
g.setColor(Color.black);
g.drawRect(x, y, edge, edge);
}
}
10. Give an example of call-by-value and call-by-reference.
Describe the difference.
Call-by-value
is when a copy of the value of an argument is passed to a
method. The method cannot change the value of the argument (only
its copy). Call-by-reference is when an argument is referred to directly
by the method; in this case, the method can change the value of the argument.
Below, the int variable i
is passed call-by-value; the int array a
is passed call-by-reference. The method sum changes the caller's
int array argument; it does not change the caller's
the int argument.
public void sum(int
i, int a[]) {
a[i++]++;
a[i]++;
}