Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Final Exam Practice

IMPORTANT: This is a practice exam. We sampled topics from the Final Exam 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.

Group 1, Problems Stressing Material After Exam Two

  1. Consider the following field declaration for a class:

    String[ ] wordList = new String[100];
           
    

    Write a method for the class that takes a parameter named word of class String and a parameter named len of type int. The method should return the number of times that word appears in cells 0..len - 1 of the array wordList. (Assume len <= 100)

  2. Consider an array whose contents are:

    17  14  8  19  3  12  6  7
           
    

    Show how this array would look after each step of the Bubble Sort algorithm is carried out.

  3. What output is produced by the following code fragment?

    for (int ct = 1; ct <= 3; ct++) {
        System.out.println(ct);
        for (int i = 3; i <= 4; i++) {
            System.out.println(ct + " " + i);
        }
        System.out.println("Hi ho");
    }
           
    
  4. What is the output from the following code fragment?

    String sentence = "to be or not to be";
    String[ ] words = sentence.split(" ");
    int len = words.length;
    
    
    for (int pass = 1; pass < len; pass++) {
        System.out.println("Pass " + pass);
        for (int i = 0; i < len - pass; i++) {
            if (words[i].compareTo(words[i + 1]) > 0) {
                String temp = words[i];
                words[i] = words[i + 1];
                words[i + 1] = temp;
            }
            System.out.println(words[i] + " " + words[i + 1]);
        }
    }
    
    
    for (int i = len - 1; i >= 0; i--) {
        System.out.println(words[i]);
    }
           
    

Questions 5 - 10 refer to the following field declarations for a class called Matrix:

int[ ][ ] grid = new int[10][11];
int[ ][ ] grid2 = new int[10][11];
  1. How many integers can grid store?
  2. Write a method init that initializes all of the values of grid to be 0.
  3. Write a method same that returns true, if grid and grid2 contain exactly the same elements. Otherwise, the method should return false.
  4. What output is produced by the following fragment?

    for (int row = 1; row <= 2; row++) {
        for (int col = 1; col <= 3; col++) {
            grid[row][col] = row + col;
        }
    }
    
    
    for (int col = 1; col <= 3; col++) {
        for (int row = 1; row <= 2; row++) {
            System.out.print(grid[row][col]);
        }
        System.out.println();
    }
           
    
  5. Write a method minRow which computes the sum of the values in each row, and returns the index of the row with the smallest sum.
  6. Write a method average which returns the average of all of the values in grid2.

For the remaining questions in this group, consider the following database table:

Students
firstName lastName id class GPA tuition
George Williams 314159 4 3.82 25000
Susanna Maigret 271828 2 3.95 26000
Alice Victor 161803 1 2.75 23000
(data omitted)
  1. Which column should be the key of this table, and why?
  2. Give a portion of a class definition for a Student object. You should show the field definitions, one of the accessors, and one of the mutators.
  3. Give a portion of a class definition for a StudentTable object. You should show the field definitions, the constructor, an add method which takes a Student parameter, and a find method which takes a student id and returns a matching Student object (or null if there is no match). You should ignore anything to do with reading from or writing to files.
  4. Write a method displayClass which takes a class number (1 to 4) as parameter and prints a table giving the first and last names of all of the members of the given class. You should assume that this method belongs to a Query class, which has a StudentTable field named students.
  5. Write a method applyScholarship which takes no parameters. It should reduce by 2000 the tuition of every student who is not a senior (class 4) and who has a GPA of at least 3.5. In the table above, only Susanna Maigret would qualify among those students shown; her tuition would drop to 24000. Again, this method should belong to a Query class, so it has access to the field students.

Group 2, Problems Stressing Material Previous to Exam Two

  1. Consider the following field declaration

    int[ ] myArray = new int[10];
           
    

    Performing a cyclic shift left on an array means to shift every element of the array one cell to the left. The element that used to be in the first cell (0) of the array becomes the last cell in the array.

    For example, if the original array contains the values:

    7 5 4 3 8 6 2 4 7 9
           
    

    The new array would be:

    5 4 3 8 6 2 4 7 9 7
           
    

    Write a method that takes myArray as a parameter and modifies it by performing the cyclic shift left operation on the array.

  2. What output is produced by the following code fragment?

    int X = 1;
    while (X <= 4) {
        if (X == 1) {
            X++;
            System.out.println(X);
        }
        else if (X == 2) {
            X = X + 2;
        }
        else if (X == 3) {
            System.out.println(X + 1);
        }
        else if (X == 4) {
            System.out.println(X * X);
            X = X + 3
        }
        System.out.println(X);
    }
       
    
  3. Write a method that takes two parameters Num1 and Num2 of type int. The method should print all the integers between 1 and 100 inclusive which are evenly divisible by both Num1 and Num2.
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