Key for Exam 1
Computer Science 125
Boise State University
Friday, 19 February 1999

1.
TRUE or FALSE:
(a)
F The Cuckoo's Egg is fiction.

(b)
T The logical expression $!(A\&\&B)$ is equivalent to !A||!B.

(c)
F The logical expression $A\&\&B$ evaluates B only if A is false.

(d)
T Cliff Stoll created the Strategic Defense Initiative Network as bait for the hacker.

(e)
F To execute an applet you type java followed by the filename.

(f)
F David La Macchia was convicted of wire fraud.

(g)
F Objects should always be compared with ==.

(h)
F The compiler will locate all of the errors in every program.

(i)
F Most computer crimes are committed by professional computer programmers.

(j)
T It violates the BSU Math/CS Departmental computer policy to let your friend use your account to read their e-mail.

2.
Give the Java code to construct the following objects:
(a)
The time spring break officially begins this semester
(Hint: it starts March 19 at 5:00pm).
Time SpringBreak = new Time(1999,3,19,17,0,0);

(b)
An employee named Sam Smith whose salary is $28,000.
Employee peon = new Employee("Sam Smith", 28000);

(c)
A line through (0,0) which has slope 2.
Line l = new Line(new Point(0,0),new Point(1,2));

3.
What is the output for each of the following sections of code? There are no intentional syntax errors.
(a)

String s = "the fun begins";
String t = "now";
String m = "later";

System.out.print(s + m+" or");
System.out.println(t + s);

Output:
the fun beginslater ornowthe fun begins

(b)
String s = "123,457";
int n = s.length();
System.out.println(s.substring(n-4,n);

Output:
,457

4.
What is the output for the following sections of code? There are no intentional syntax errors.
int choice = 3;
boolean valid = true;
if (choice == 1)
{ System.out.println("You selected 1");
  valid = true;
}
else if (choice == 2)
{ System.out.println("You selected 2");
  valid = true;
}
else
{ System.out.println("Your choice is not valid");
  valid = false;
}

Output:
Your choice is not valid

5.
(a)
What does this program produce as output?
import ccj.*;
public class Exam
{public static void main(String[] args)
  {  int A = 105;
     int B = A/60;
     System.out.println(""+(A+B));
  }
}

Output:
106

(b)
What does this program produce as output?
import ccj.*;
public class Example
{ public static void main(String[] args)
  { int x = 16;
    if ((x % 2 == 0) && (x % 3 == 0))
       System.out.println("The value of x is divisible by 6");
    else
       System.out.println("The value of x is not a multiple of 6");
  }
}

Output:
The value of x is not a multiple of 6

6.
(a)
What does this program produce as output?

import ccj.*;
public class MysteryApplet1 extends GraphicsApplet
{public void run()
  {Point p1 = new Point(-3,4);
   Point p2 = new Point(3,4);
   Line line1 = new Line(p1,p2);
   Circle c = new Circle(new Point(0,4),3);
   c.draw();
   line1.draw();
  }
}
Output:

(b)
What does this program produce as output?
import ccj.*;
public class MysteryApplet extends GraphicsApplet
{ public void run()
    {  Point p1 = new Point(-5,-5);
       Point p2 = new Point(5,5);
       Line line1 = new Line(p1, p2);
       line1.draw();
       line1.move(3,3);
       line1.draw();
    }
}
Output:

7.
Evaluate the following Java expressions.

(a)
$990 \% 100$ = 90

(b)
4.4 * 2/4 + 6 = 8.2

(c)
$8 \% 3 * 5 - 16/3 * 4$ = -10

(d)
3.4 <= 8 - 9/2 = true

(e)
x < 5 || x >= 5 = true

8.
Which of the ethical approaches discussed in class, Utilitarian, Rights, Fairness, Common-Good, or Virtue, comes closest to the way you make decisions?




Answer the following question based on the approach that you selected. Justify your answer in terms of your chosen approach.

In the Cuckoo's Egg Bob Morris (Sr.) was the head of the NSA and his son Robert T. Morris (Jr.) was responsible for writing a virus that brought the internet to a standstill. Was Bob Morris (Sr.) ethically bound to turn in his son?

9.
Below is an applet that will draw a cylinder. Modify the code so that it will draw a cylinder whose radius and height are both doubled. Cross out each of the statements that you are changing and write the new statement beside the one that is changed.

import ccj.*;
public class Cylinder extends GraphicsApplet
{public void run()
 { Point origin = new Point(0,0);
   int radius = 3; //CROSS OUT THIS and add: int radius = 6;
   int height = 4; //CROSS OUT THIS and add: int height = 8;
   Circle bottom = new Circle(origin,radius);
   bottom.draw();
   Point p1 = new Point(-radius,0);
   Point p2 = new Point(radius,0);
   Point p3 = (Point)p1.clone();
   Point p4 = (Point)p2.clone();
   p3.move(0,height);
   p4.move(0,height);
   Line l1 = new Line(p1,p3);
   Line l2 = new Line(p2,p4);
   l1.draw();
   l2.draw();
   bottom.move(0,height);
   bottom.draw();
 }
}

10.
Write a complete stand-alone(non applet) Java program which prompts a user to enter a 7 digit integer without commas and then prints the integer with the commas separating the millions and thousands and the thousands and hundreds.

import ccj.*

public class Commas
{
  public static void main(String[] args)
  {
    System.out.print("Enter a 7 digit number:");
    int inNum = Console.in.readWord();
    String outNum = inNum.substring(0,1) + ","
               + inNum.substring(1,4) + ","
               + inNum.substring(4,7);
    System.out.print(outNum);
  }
}

//Can also be done using integers and % and /