Study Guide for Midterm, CS 110, Fall 2004

The test will take place on Thursday, October 7, 2004 during class time. The test will be open book and open notes. The only thing excluded is computer use; so do not bring a laptop. If you want to bring a calculator that is fine, although I doubt that you will find it useful.

The test will cover material presented in class or from the assigned reading to date (chapters 1 - 8 of Bell and Parr along with Appendix C). Note that the intent is to determine whether you could write a simple program in Java, and that you understand the basics of object-oriented design and implementation. Questions will not be based on small esoteric details from the book or class.

Q & A

Starting 9/30 and continuing until 10/6 at noon, I will accept questions via email. I will post responses to these questions on the course web site. I will not respond to emailed questions of the form "what is the answer to question 5?" However, if you come to my office I will not reject such questions. I will read my email at least once a day at noon for questions. I will not be available for questions after 3pm on 10/6.

Sample Questions:

Note: there are more sample questions than there will be on the actual test. Also, on the test I will leave room for answers.

Very Short Answer

  1. what does the acronym URL stand for?
  2. Three types of Java variables are: boolean, int and ?
  3. javac is used to ____________________ programs.
  4. Suppose you created Java program in the class Foo.java. You want Foo to show up in a 300 wide by 300 high space on a web page. What code do you need to put in your web page to make Foo appear in it?
  5. Suppose you added a file named HiGeoff.html to the html subdirectory of your CS account. What would the URL of that page be?
  6. What is the name of the operating system you are using in this class?
  7. The following program would work, but for syntax errors. For full credit, find and fix at least 5 (there are 7)
    import java.awt.*
    import java.applet.applet;
    public class A extend Applet {
        public paint(Graphics clown) {
    	int a = 40;
    	clown.drawString("aaa", 30);
    	float b = 90;
    	clown.fillRect(a, b, a, b)
        }
    }
    
  8. In the code for question 7, what is the class definition line?
  9. What is java convention for capitalization of class names?

Longer Short Answers

  1. What does "extends" mean in the class definition line of a Java class?
  2. At each step after the declarations, show the value of each variable in the following program fragment:
       int j = 22;
       float k = 22.0f;
       int h = 4;
       j = j / h;
       k = k / h;
       j = (j * 6) / 5;
    
  3. What is the final value of the variables m and n in the following program?
      int m = 107;
      int n = 0;
      while (m > 7) {
         m = m / 8;
         n = n + 1;
      }
    
  4. Draw (approximately) the figure created by the following paint method
    public void paint(Graphics cc) {
      int aa = 20;
      cc.drawLine(20, 20, 20, 300); 
      cc.drawLine(20, 300, 300, 300); 
      while (aa < 300) {
        cc.drawline(20, aa, aa, 300);
        aa = aa + 100;
      }
    }
    

Problem solving in Java

  1. The following program gets two random numbers and shows them. Missing is a statement of whether or not the smaller number is an even divisor of the larger (e.g, 3 is an even divisor of 6, 4 is not an even divisor of 6). Finish the program so that paint writes the word "TRUE" if the smaller is and even divisor and "FALSE" otherwise.
    public void paint(Graphics goo) {
      int larger = 1000 + (int)(Math.random()*1000);
      int smaller = 1 + (int)(Math.random()*1000);
      goo.drawString("larger: "+larger, 20, 20);
      goo.drawString("smaller: "+smaller, 20, 50);
      // your code here
    }
    
  2. The following paint method is incomplete. It should also display the Nth power of the number 5. Complete it.
    public void paint(Graphics g42) {
       int n = (int)(Math.random()*10);
       g42.drawString("Power: " + n, 20, 20);
       // your code here
    }
    
  3. Write a complete class that contains an interface with two widgets: a button and a text field. The program should display the number of times the button has been hit and independently the text in the text field along with the number of times that the return key was hit in the text field.
  4. Complete the following program by writing the missing "drawStar" method. The drawStar method should draw a circle with a horizontal and a vertical line through it. The horizontal and vertical lines should cross in the center of the circle and extend 20 units beyond the outside of the circle. The parameters to drawStar are: the x location, the y location, the diameter of the circle and the graphics thing in which to draw.
    import java.awt.*;
    import java.applet.Applet;
    public class Stars extends Applet {
      public void paint(Graphics gx) {
        drawStar(40, 40, 50, gx);
        drawStar(100, 100, 90, gx);
    }
    }