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.

        public double checkMatches( )
        {
            int numMatches = 0;
            for (int i = 0; i < classOne.length; i++) {
                if (classOne[i] == classTwo[i]) {
                    numMatches++;
                }
            }
            return (double) numMatches / classOne.length;
        }
    
  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.

        public void isListOrdered( )
        {
            int firstNum, secondNum;
            boolean inOrder = true;
            int i = 0;
            while (inOrder && i < theList.size( ) - 1) {
                firstNum = (Integer) theList.get(i);
                secondNum = (Integer) theList.get(i+1); 
                if (firstNum.intVal( ) > secondNum.intVal( )) {
                    inOrder = false;
                }
                i++;
            }
            if (inOrder) {
                System.out.println("in order");
            }
            else {
                System.out.println("not in order");
            }
        }
    
  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];
    
        public void isListOrdered( )
        {
            boolean inOrder = true:
            int i = 0;
            while (inOrder && i < theList.length - 1) {
                if (theList[i] > theList[i+1]) {
                    inOrder = false;
                }
                i++;
            }
            if (inOrder) {
                System.out.println("in order");
            }
            else {
                System.out.println("not in order");
            }
        }
    
  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.

    public class TestGrades {
        private int[ ] classOne = new int[100];
        private int[ ] classTwo = new int[100];
    
    
        public TestGrades( )
        {
            for (int i = 0; i < classOne.length; i++) {
                classOne[i] = (int) (Math.random( ) * 101);
                classTwo[i] = (int) (Math.random( ) * 101):
            }
        }
    
    
        public void displayClassOne( )
        {
            for (int i = 0; i < classOne.length; i++) {
                System.out.println(classOne[i]);
            }
        }
    
    
        public int aGrades( )
        {
            int numAs = 0;
            for (int i = 0; i < classTwo.length; i++) {
                if (classTwo[i] >= 90) {
                    numAs++;
                }
            }
            return numAs;
        }
    
    
        public int maxClassOne( )
        {
            int max = classOne[0];
            for (int i = 0; i < classOne.length; i++) {
                if (classOne[i] > max) {
                    max = classOne[i];
                }
            }
            return max;
        }
    }
    
  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]);
            }
        }
    
    0 2
    1 2
    2 3
    3 5
    4 8
    5 12
    6 17
    7 23
    8 30
    9 38
    
  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]);
            }
        }
    
    Before:
    3
    5
    15
    9
    10
    19
    12
    16
    17
    18
    c is 2
    After:
    3
    5
    9
    10
    15
    12
    16
    17
    18
    19
    
  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.

    A queue is a computing structure that holds data. New data may be added to the rear of the structure, and data may be deleted from the front of the structure only. Enqueue adds data to the end/rear of the queue; dequeue removes data from the front of the queue. ArrayLists could be used to implement queues easily, because ArrayLists are flexible in size. Also, the add method adds data to the end/rear of the ArrayList – enqueue. Likewise, the remove method could remove data from the zero-indexed position at the front of the queue – dequeue.

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

    All data that is output from source code is created by algorithms – not by chance or randomly. The numbers can appear to be random, because they behave much as truly random numbers do. The numbers, however, can be replicated (because they are created by algorithms (series of steps)) by repeating the same steps again – unlike true random numbers.

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