Spring Semester 2002


Please write your name or username here: ___________________________

This exam is open-book, open-notes, but individual work. It lasts 120 minutes (5-7pm).


For each of the following questions please write your answers in the spaces provided.

1. Implement a class Square that has methods getArea() and getPerimeter(). In the constructor supply the width of the square.

Here's a possible answer:

class Square {
    private double width;
    Square(double width) {
	this.width = width; 
    }
    double getArea() {
	return this.width * 
	    this.width; 
    }
    double getPerimeter() {
	return 4 * this.width; 
    }

    public static void main(String[] args) {
	Square a = new Square(10); 
	double area = a.getArea(); 
	double perim = a.getPerimeter(); 
	System.out.println(a.width + "\n" + area + "\n" + perim); 
    }
}
2. Explain what each of the following two program segments compute:
x = 2;
y = x + x;
and
s = "2"; 
t = s + s;

The answer is:

3. How do you get the first character of a string? The last character? How do you remove the first character? The last character?

Assume

String a;
then we have:

You can't remove characters from a String.

All you can do is create copies of the relevant parts, like so:

4. What is a purpose of a graphics context and where in an applet is it used?

  1. Its purpose is to allow drawing.
  2. Without it we wouldn't be able to draw anything.
  3. It is passed (by the Conductor) to the paint method.

5. Explain the difference between

s = 0; 
if (x > 0) s++; 
if (y > 0) s++; 
and
s = 0; 
if (x > 0) s++; 
else if (y > 0) s++; 

  1. In the first snippet the second if statement is always executed.
  2. In the second one only sometimes.
  3. For both x and y positive the first one sets s to 2 while the second sets it to 1.

6. Explain the difference between the == operator and the equals method when comparing strings.

frilled.cs.indiana.edu%cat Example.java
class Example {
    public static void main(String[] args) {
	String a = "one", 
	    b = args[0]; 
	System.out.println("a = " + a + " and b = " + b);   
	System.out.println("a.equals(b) is: " + a.equals(b)); 
	System.out.println("a == b is: " + (a == b)); 
    }
}
frilled.cs.indiana.edu%javac Example.java
frilled.cs.indiana.edu%java Example one
a = one and b = one
a.equals(b) is: true
a == b is: false
frilled.cs.indiana.edu%
7. Rewrite the following for loop using a while loop.

int s = 0; 
for (int i = 1; i <= 10; i++) s = s + i; 
int s = 0, i = 1;
while (i <= 10) {
  s = s + i; 
  i++; 
} 
8. You compile and run the following program. What's its output?
class One {
    public static void main(String[] args) {
	double a = 1.0, b = 6.0; 
	System.out.println("(" + a + ", " + b + ")"); 
	One.swap(a, b); 
	System.out.println("(" + a + ", " + b + ")"); 
    }
    public static void swap(double a, double b) {
	double temp = a;
	a = b; 
	b = temp; 
    }
}
Please briefly explain the answer.

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	double a = 1.0, b = 6.0; 
	System.out.println("(" + a + ", " + b + ")"); 
	One.swap(a, b); 
	System.out.println("(" + a + ", " + b + ")"); 
    }
    public static void swap(double a, double b) {
	double temp = a;
	a = b; 
	b = temp; 
    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
(1.0, 6.0)
(1.0, 6.0)
frilled.cs.indiana.edu%
Since parameters are passed by value the swap is local to the method.

9. Here's a program that is intended to compute the average of two numbers:

public class Two {
    public static void main(String[] args) {
	int x1 = 3, 
	    x2 = 4; 
	System.out.println((x1 + x2) / 2); 
    }
}
What's the output if you run it, and what needs to be done to obtain the correct result.

  1. The output is 3.
  2. To obtain 3.5 one needs to cast at least one of the three values in the formula to a double.

10. You are writing an applet like the one you did for Homework Five (the two eyes are watching the mouse). What steps do you need to take to make your program respond to mouse movement as it did then? Be succinct (i.e., short, complete, and precise) in your answer. There are three steps.

  1. Provide the code for a MouseMotionListener
  2. Instantiate or locate an actual instance of a MouseMotionListener
  3. Add the listener to the applet.
11. What's the use of a toString() method, and what is special about it?

One can define such a method to obtain custom String representations of objects. Evaluating an object reference in a String context amounts to an invocation of this method on the object reference.

12. You compile and run the following program. What is its output. Explain.

class Horse {
    int fun() { return -1; }
} 
class Unicorn extends Horse { 
    int fun() { return 1; } 
}

class Three {
    public static void main(String[] args) {
	Horse   a = new Horse(); 
	Unicorn b = new Unicorn(); 
	Horse   c = new Unicorn(); 
	System.out.println(a.fun() + 
			   b.fun() + 
			   c.fun()); 
    } 
}

The answer is -1 + 1 + 1 which amounts to 1.

It is the type of the object and not that of the reference that matters.

13. Write a method that takes in an array of ints and returns the largest element in the array.

Take a look at the program presented with the next problem.

14. Write a method that takes in an array of ints and sorts it in ascending order.

Here's an answer to both 13 and 14.

class Methods {
    public static void main(String[] args) {
	int[] a = {1, 6, 2, 5, 3, 4}; 
	show(a); 
	System.out.println(largest(a)); 
	sort(a); 
	show(a);
    }
    public static int largest(int[] a) {
	int max = a[0];
	for (int i = 1; i < a.length; i++)
	    if (max < a[i])
		max = a[i]; 
	return max;
    }
    public static void sort(int[] a) {
	for (int i = 0; i < a.length - 1; i++)
	    for (int j = i; j < a.length; j++)
		if (a[i] > a[j]) {
		    int temp = a[j]; 
		    a[j] = a[i]; 
		    a[i] = temp; 
		}
    }
    public static void show(int[] a) {
	for (int i = 0; i < a.length; i++) 
	    System.out.print(a[i] + " "); 
	System.out.println(); 
    }
}
15. What's the difference between a Vector and an array of Objects? What are the similarities between the two? What is the difference between a Vector and any array? What similarities do you see between a Vector and an array?

A Vector

  1. is like an array of Objects, except that
  2. an array of Objects cannot grow and shrink automatically.
By the same token:
  1. Vectors have automatic memory management, arrays do not.

  2. Both Vectors and arrays use integer indices to refer to their elements.

16. What's the difference between a Vector and a Hashtable? What are the similarities, if any?
  1. Both have automatic storage management.
  2. One uses integer indices, the other Objects, to refer to their elements.
17. What's the difference between an instance variable and a static one? Can a static method invoke an instance method (directly or indirectly)? How?

A static variable is global to all (if any) instances of that class. An instance variable is global to only the instance methods (if any) of (the blueprint) of that class. No matter how many instances are created there will be only one copy of any static variable that the class defines. Instance variables, however, will be located inside instances and therefore there will be as many instance variables as instances (if any). No, a static method cannot call an instance method directly, it needs to use an object reference for that. See for example the following:

frilled.cs.indiana.edu%cat Test.java
class Test {
    public static void main(String[] args) {
	fun(); 
    }
    void fun() {
	System.out.println("Hello, world!"); 
    }
}
frilled.cs.indiana.edu%javac Test.java
Test.java:3: non-static method fun() cannot be referenced from a static context
	fun(); 
        ^
1 error
frilled.cs.indiana.edu%

18. What's recursion? Give an example of a recursive method.

Recursion is the ability to describe an operation in terms of itself (with a base case).

For example:

int sum(int n) {
  if (n == 1) return 1; 
  else return n + sum(n - 1); 
}
To calculate the sum of the first n integers you add n to the sum of the first n -1 integers. The sum of the first 1 integer(s) is 1 (assuming that we are taking into account only strictly positive numbers).


Last updated: May 4, 2002 by Adrian German for A201