Exam 1
Computer Science 125
Lecture 3 (St. John)
Boise State University
Thursday, 24 September 1998

1.
TRUE or FALSE:
(a)
The type double is an example of an object.
(b)
The logical expression $!(A\&\&B)$ is equivalent to $!A\&\&!B$.
(c)
The logical expression $A\&\&B$ evaluates B first, then A.
(d)
A function has exactly one return value.
(e)
A function has at most one return value.
(f)
A function with no parameters must have a void return type.
(g)
Object variables hold references, not values.
(h)
Every object belongs to a class.
(i)
If an action is legal, than it's ethical.
(j)
Those caught committing software crime are most often low-level employees.

2.
Give the Java code to construct the following objects:
(a)
The current time:
(b)
Your next birthday:
(c)
The president as an employee (use $200,000 as the salary):
(d)
A circle with center (-1,-1) and radius 3:
(e)
A line running from (-10,-10) to (10,10):

3.
What is the output?
(a)
String s = "exam";
boolean okay = false;

if ( s.substring(1,2).equals("e") )
   okay = true;

if ( okay )
   System.out.println("Okay!");
else
   System.out.println("Not Okay!");

(b)
int year = 1900;
boolean leap = (year % 4 == 0) && !(year % 100 == 0);
leap = leap || (year % 400 == 0);

if ( leap )
   System.out.println("Leap!");
else
   System.out.println("Not Leap!");

4.
What does this program produce as output?
import ccj.*;
public class Mystery
{ public static void main(String[] args)
    { int x = 12345;
       if ( myst(x) % 3 == 0 )
          System.out.println("Divisible!");
       else
          System.out.println("Indivisible!");
    }
    public static int myst(int x)
    { if ( x < 10 )
          return x;
       return ( (x%10) + myst(x/10) );
    }
}

5.
What does this program produce as output?
import ccj.*;
public class MysteryApplet extends GraphicsApplet
{ public void run()
    {  Point p1 = new Point(-5,-5);
       Circle c1 = new Circle(p1, 4);
       c1.draw();
       p1.move(7,7);
       p1.draw(); 
       Line l1 = new Line(p1, new Point(0,0));
       l1.draw();
    }
}

6.
Write a function that takes 2 points as input and returns the distance between the points. (Recall that the distance between (x1,y1) and (x2,y2) is $\sqrt{{(x_1-x_2)}^2 + {(y_1 - y_2)}^2}$.)

7.
Write a recursive function that takes a string and returns true if the string is a palindrome (the same written backwards as forwards).

8.
A friend gives you a nice Java package that helps you finish your project for your new boss. You later find out that this nice Java package is copyrighted by the competitor. No one is likely to find out that you are using this package (it has been compiled into bytecode). However, if they do, you will be fired and your company sued. What should you do?

(a)
Who has something at stake in this dilemma?
(b)
What should you do? Briefly explain why:
(c)
Is what you did legal? Briefly explain why:
(d)
What added knowledge would help you make this decision? Or cause you to change your decision? Briefly explain why:
9.
Given the following function definitions:
public static double hare(double x) { return 2 * hatter(x/2); }
public static double hatter(double x) { return x * alice(x); }
public static double alice (double x) { return mouse(x) - 1 ; }
public static double mouse(double x) { return x*x - x; }

(a)
alice(5) =
(b)
hare(4) =
10.
Write a recursive function that takes an integer and breaks it into a sequence of individual digits. For example, 16384 would be displayed as
1 6 3 8 4
(Hint: Use recursion.)