CPSC 124, Fall 1998
Old Exam Questions


This page contains questions from the final exams in Computer Science 124 from previous terms. Most of the questions from the exams given in Fall 1996 and Winter 1998 are here. A few questions that would be inappropriate, given differences in the exact material covered, are omitted.


(1) Define each of the following terms, as they relate to this course:


(2) Two fundamental concepts in talking about programming languages are syntax and semantics. Explain these terms, and discuss the syntax and semantics of the statement " z = 3*x + 7;".


(3) Discuss what is meant by asynchronous events and how they are used in Java programs.


(4) Write a complete Java program that will create a file named "squares.dat" containing the squares of the first 100 integers. That is, the file should contain the numbers 1, 4, 9, 16, 25, and so forth. You will need to use the classes PrintWriter and FileWriter. Don't forget that the creation of a FileWriter can produce an IOException, so you will need to use a try statement when you open the output file.


(5) Write a Java code segment that will compute the sum of the reciprocals of the first 10000 positive integers. Use a for statement. The sum you should compute is:

             1/1 + 1/2 + 1/3 + 1/4 + . . . + 1/10000

(6) Write a static method in Java that will compute the average of three numbers. The method should have three parameters of type double representing the numbers to be averaged. (The average of x, y, and z is given by (x+y+z) / 3.0.)


(7) This problem concerns storing data about the population of various countries. The data for a single country is stored in an object belonging to a class called CountryData. The data for all the countries is stored in a class called Atlas. This class uses a "partially full array" to hold the data. The problem is to complete the definition of the instance methods addCountry, getPopulation, and getTotalPopulation in the class Atlas.

   class CountryData {  // stores data about one country
        String name;     // the name of the country
        int population;  // the population of the country
        CountryData(String theName, int thePopulation) {
          name = theName;
          population = thePopulation;
        }
    }// end of class CountryData
    

    class Atlas { // stores data about an array of countries
       CountryData data = new CountryData[200];  // data for countries
       int countryCount; // number of countries in the data array
       void addCountry(String theName, int thePopulation) {
             // add data for a new country to the end of the data array
             
          // Fill in the definition of addCountry!
             
       } // end of addCountry() method
       

       int getPopulation(String countryName) {
            // Find the country named "countryName" in the array, and return
            // the population of that country.  (If the country does not exist
            // in the array, then a value of -1 should be returned.)
            
          // Fill in the definition of getPopulation!
             
       } // end of getPopulation() 
       
       
       int getTotalPopulation() {
           // return the total population of all the countries in the array
           
          // Fill in the definition of getTotalPopulation!
             
       } //end of getTotalPopulation() method
    } // end of class Atlas
    

(8) Suppose that a bird watching club has collected data on the number of sightings of 20 different bird species in each of 100 different cities, for the year 1995. The data has already been stored in an array, sightings, which was created by the statement

                int[][] sightings = new int[20][100];

The value stored in sightings[i][j] is the number of times that a bird belonging to species number i was sighted in city number j.

a) Write a code segment that will count the total number of sightings of species number 17 in all 100 cities.

b) Write a code segment that will compute and print the total number of bird sightings for city number 0, then for city number 1, then for city number 2, and so forth, up to city number 99. You can print the answers to standard output using System.out.println().

c)> Let's say that a species is endangered if it has been sighted in only 4 cities or fewer. (Species number i was sighted in city number j if sightings[i][j] is greater than zero.) Write a code segment that will print a list of all the species that are endangered. You can print the answers to standard output using System.out.println().


(9) One of the major ideas in computer science is abstraction. An abstraction is often expressed as a black box, with a separation of interface from implementation. Write an essay that discusses all these terms and explains why they are important. Discuss the examples of subroutines and classes as black boxes.


(10) Streams in Java are an important example of abstraction. What does this mean, why is it true, and why is it important?


(11) Suppose that color is a variable of type Color. Write a code segment that will set color to one of the four standard colors Color.black, Color.red, Color.blue, or Color.green. The color should be chosen at random, with equal probability for each color. (Recall that (int)(4*Math.random()) is an integer in the range 0 to 3, inclusive.)


(12) Suppose that your program needs to read a real number from the user that is definitely between 0.0 and 1.0 (inclusive). Write a while loop that will read numbers entered by the user until the number entered satisfies this condition. Use the TextIO class for input.


(13) Write a complete subroutine that counts the number of divisors of a positive integer, N. The integer N should be a parameter to the subroutine. The number of divisors should be the return value of the subroutine. (Recall that a number D is said to be a divisor of N if D is between 1 and N and if the remainder when N is divided by D is zero.)


(14) A variable in a class can be declared with one of the "access modifiers" public, private, or protected, or it can be declared with no access modifier at all. Discuss the effect of each of these four possibilities.


(15) Write a complete Java subroutine that will determine whether a given int, N, occurs in a given two-dimensional array of ints, A. The variables N and A should be parameters to the subroutine. The subroutine should return its answer as a boolean value.


(16) Here is a simplified class for representing books owned by library:

           class Book {
              String title, author;  // title and author
              String callNumber; // call number, such as "QA567.23 P23"
           }

Suppose that a library owns 27,532 books and that information about all the books has already been stored in an array

           Book[] books = new books[27532];

a) Let's say that a science book is one whose call number begins with the letter 'Q'. Write a code segment that will count the number of science books owned by the library.

b) Does the library own more books by "Isaac Asimov" or more books by "William Shakespeare"? Write a code segment that will find out. Print the answer with System.out.println()


(17) Suppose that LastChance is a class. List and briefly describe each of the four steps that the computer goes through when it executes the statement

              LastChance x = new LastChance(42);

(18) Suppose that you are writing a simple two-player game, that uses standard input/output (rather than running as an applet). Suppose that you've already written three classes: Player, Game, and Move (where an object of type Move represents one move by one player in a game). The classes include the following methods:

(The first of these communicates with the player to find out what move the player wants to make.) Using just this information, you can write a subroutine to play one one full game. Complete the following subroutine so that it will do so. The two players should take turns making moves, until the game is over. Note that the Players in the game are passed as parameters to the subroutine, but you must create the Game object that represents the game itself.

          static void playOneGame(Player player1, Player player2) {
          
                // Fill in definition of playOneGame()
          
          } // end playOneGame()

(19) Consider the terms variable, type, object, and class. These are fundamental concepts related to data representation in computer programming. Discuss these terms. What are the relationships among them? What are the differences?


(20) a) Complete the following little applet. When it starts up, the applet should display the message "Hello World." When the user clicks on the applet, the message should change to "Goodbye World." If the user clicks again, it should change back to "Hello World," and so forth. Use the paint() method to display the strings. You will need to add an instance variable to keep track of the message that is currently displayed.

          class Greetings extends Applet implements MouseListener {
          
          

             public void init() { // (No need to change this.)
                addMouseListner(this);
             }
             
             public void paint(Graphics g) {
             
                         
                         
             };
             
             
             public void mousePressed(MouseEvent evt) {
             
             
             
             };   
     
             public void mouseReleased(MouseEvent evt) { } // Junk required by 
             public void mouseClicked(MouseEvent evt) { }  //     the MouseListener
             public void mouseEntered(MouseEvent evt) { }  //          interface.
             public void mouseExited(MouseEvent evt) { }
             
          } // end class Greetings

b) Explain how you would go about publishing your applet on the World Wide Web. Also explain what this means, and why you might want to publish your applets on the Web.