Computer Science 125-002 First Examination Fall 1998

1. Assume that Foobar is a class. Draw a diagram which illustrates the various relationships after executing the following statements: [10 points]

Foobar a;
Foobar b = new Foobar();
Foobar a = b;

2. For each of the following algebraic expressions, give an equivalent expression using correct Java syntax (your calculations should use double quantities - but you can use any variable without declaration). [7 points each]

(a) $\displaystyle{\frac{1}{a + b}}$

(b) $\displaystyle{b^2 - 4ac}$

(c) $\displaystyle{\sqrt{\frac{1}{1 + \frac{1}{x}}}}$

3. Write a complete stand-alone (non applet) Java program which prompts a user for the weight of a first-class letter in ounces, computes and displays the postage in cents for the letter according to the following rules:

Use an int function Charges for calculating the postage charges using a single double parameter which carries the weight of the letter. Do all input and output within the main function. [12 points]

4. Consider the following recursive function (assume that some suitable enclosing class definition exists). [12 points]

    public void funny(int m, int n)
    {
        if ( n > 0)
            return funny(n, m % n);
        else
            return m;
    }

Suppose that the above function is called from another class method as follows:

System.out.println(funny(15, 12));

Show the chain of calls for the above and indicate the value which is printed.

5. Explain the differences in usage between a static class method and an instance method. [8 points]

6. Indicate the output which is produced in the following program: [10 points]

    public class Example
    {
        public static void main(String[] args)
        {
            int Value = 4;
            String s = new String("Hello");

            mystery(Value, s);
            System.out.println(Value + " " + s);
        }

        public static void mystery(int n, String foo)
        {
            n = 8;
            foo = "Bye";
        }
    }

Explain your responses.

7. Write an if or nested if equivalent to each of the following flow diagrams: [9 points each]

8. Simplify the following boolean expression as much as possible: [9 points]

(!( !(x>1) || (y<3) ))