Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Exam 2 Practice

IMPORTANT: This is a practice exam. We sampled topics from the Exam Two Topics document to give you a sense of the exam that is approaching. Memorizing and/or studying the content of this one, single sample set of practice problems cannot substitute for several weeks of thoroughly studying all the class notes, homework problems, textbook readings, laboratory exercises, and programming projects.

  1. Write a method checkMatches (for the class TestGrades) that has no parameters. TestGrades has two fields, classOne and classTwo, which are arrays defined below:

        private int[] classOne = new int[100];
        private int[] classTwo = new int[100];
    

    checkMatches returns a double -- the decimal form of the fraction of cells in parallel positions in the two arrays that contain the same values -- out of all the possible positions where there could be identical values.

    For example, if classOne contains the consecutive integers 1 .. 100, and classTwo also contains these same integers in the same order, then checkMatches would return 1.0, because there were 100 out of 100 matches. If, however, classOne contains the same consecutive integers, 1 .. 100, and classTwo contains the integers in the reverse order, 100 .. 1, then checkMatches would return 0.0, because there were 0 out of 100 matches. Finally, if classOne contains fifty values of 1 followed by fifty values of 2, and classTwo contains fifty values of 1 followed by fifty values of 4, then checkMatches would return 0.5, because there were 50 out of 100 matches.

  2. Write a method called isListOrdered (for the class List). isListOrdered has no parameters. List has a field called theList, which is an ArrayList defined below:

        private ArrayList theList;
    

    The objects in theList are integer objects of the class Integer. The class Integer has an intValue method that returns the int value stored in the Integer object. The List constructor guarantees that theList has at least one object in it. The method isListOrdered should print "in order" on the monitor screen, if the values in theList are in ascending order; and "not in order", otherwise. For example, if theList contains 1, 4, 5, 6, 6, 9, then the method should print "in order". However, if theList contains 3, 7, 2, 1, then the method should print "not in order". Finally, if theList contains just 6, then the method should print "in order", because a list with one number is considered ordered.

  3. As a follow-up, you might write isListOrdered for a class with the same name, List, but change the field to:

        private int[ ] theList = new int [100];
    
  4. Write the class described in the first practice problem (1); that is, write TestGrades. You don't have to rewrite checkMatches, of course. The constructor should generate 101 random test grades (in the range from 0 to 100, inclusive) for each of the two arrays that are fields for the class, classOne and classTwo. Write a method called displayClassOne that will print the grades of classOne on the monitor. Write a method called aGrades that will return the number of As (90 or greater) in classTwo. Write a method called maxClassOne that will return the maximum grade of classOne.

  5. What is the output of the following source code fragment (one field and one method extracted from a class), if the method test is called?

        public int[ ] arr = new int[10];
    
    
        public void test( )
        {
            int j, k;
    
    
            j = 2;
            for ( k = 0; k < arr.length; k++ ) {
                arr[k] = j;
                j = j + k;
            }
    
    
            for ( k = 0; k < arr.length; k++ ) {
                System.out.print(k + " " + arr[k]);
            }
        }
    
  6. Suppose the following field has been declared for some class:

        int[ ] someArray = new int[10];
    

    Also assume that the constructor for the class fills someArray with the following values:

    3 5 15 9 10 19 12 16 17 18
    0 1 2 3 4 5 6 7 8 9

    Show the output if the following method (called test) for this unnamed class is called:

        public void test( )
        {
            int c = 0;
            int t = 0;
            System.out.println("Before:");
            for (int i = 0; i < someArray.length; i++) {
                System.out.println(someArray[i]);
            }
    
    
            for (int i = 1; i < someArray.length; i++) {
                if (someArray[i-1] > someArray[i]) {
                    c++;
                }
            }
            System.out.println("c is " + c);
    
    
            for (int i = 1; i < someArray.length; i++) {
                if (someArray[i-1] > someArray[i]) {
                    t = someArray[i-1];
                    someArray[i-1] = someArray[i];
                    someArray[i] = t;
                }
            }
    
    
            System.out.println("After:");
            for (int i = 0; i < someArray.length; i++) {
                System.out.println(someArray[i]);
            }
        }
    
  7. What is a queue? Describe enqueue and dequeue operations for a queue. If you had to build a Queue class from scratch, would you use an array or an ArrayList to do so? Describe how enqueue and dequeue would be facilitated by your choice.

  8. The hearts of many computer simulations are random numbers. Why are random numbers not really random?

Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University , Computer Science Department , Spring 2005
Maintained by Brian Howard ( bhoward@depauw.edu ). Last updated