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)

    public int countWords(String word, int len)
    {
        int count = 0;
        for (int i = 0; i < len; i++) {
            if (wordList[i].equals(word)) {
                count++;
            }
        }
        return count;
    }
           
    
  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.

    14  8  17  3  12  6  7  19
    8  14  3  12  6  7  17  19
    8  3  12  6  7  14  17  19
    3  8  6  7  12  14  17  19
    3  6  7  8  12  14  17  19
    3  6  7  8  12  14  17  19
    3  6  7  8  12  14  17  19
           
    
  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");
    }
           
    
    1
    1 3
    1 4
    Hi ho
    2
    2 3
    2 4
    Hi ho
    3
    3 3
    3 4
    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]);
    }
           
    
    Pass 1
    be to
    or to
    not to
    to to
    be to
    Pass 2
    be or
    not or
    or to
    be to
    Pass 3
    be not
    not or
    be or
    Pass 4
    be not
    be not
    Pass 5
    be be
    to
    to
    or
    not
    be
    be
           
    

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?
    There are 10 rows with 11 cells in each row, for a total of 110 ints.
  2. Write a method init that initializes all of the values of grid to be 0.
    public void init()
    {
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 11; col++) {
                grid[row][col] = 0;
            }
        }
    }
       
    
  3. Write a method same that returns true, if grid and grid2 contain exactly the same elements. Otherwise, the method should return false.
    public boolean same()
    {
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 11; col++) {
                if (grid[row][col] != grid2[row][col]) {
                    return false;
                }
            }
        }
        return true;
    }
       
    
  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();
    }
           
    
    23
    34
    45
       
    
  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.
    (Clarification: the method should use the values of grid.) This is easiest if we also write a method sumRow which takes a row index and returns the sum of all the values in that row:
    public int sumRow(int row)
    {
        int sum = 0;
        for (int col = 0; col < 11; col++) {
            sum += grid[row][col];
        }
        return sum;
    }
    
    
    public int minRow()
    {
        int min = 0;  // index of smallest row so far
        int minSum = sumRow(min);
        for (int row = 1; row < 10; row++) {
            int sum = sumRow(row);
            if (sum < minSum) {
                min = row;
                minSum = sum;
            }
        }
        return min;
    }
       
    
  6. Write a method average which returns the average of all of the values in grid2.
    public double average()
    {
        int sum = 0;
        for (int row = 0; row < 10; row++) {
            sum += sumRow(row);  // or write out the loop as above
        }
        return (double) sum / (grid.length * grid[0].length);  // or (10 * 11)
    }
       
    

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?
    The key should be id, since it is the only value which will uniquely identify a given student (two students could easily have the same first name, last name, class number, GPA, or tuition rate).
  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.
    public class Student {
        private String firstName;
        private String lastName;
        private int id;
        private int class;
        private double GPA;
        private int tuition;
        
        public String getFirstName()
        {
            return firstName;
        }
        
        public void setGPA(double newGPA)
        {
            GPA = newGPA;
        {
    }
       
    
  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.
    public class StudentTable {
        private ArrayList students;
        
        public StudentTable()
        {
            students = new ArrayList();
        }
        
        public void add(Student student)
        {
            students.add(student);
        }
        
        public Student find(int id)
        {
            for (int i = 0; i < students.size(); i++) {
                Student student = (Student) students.get(i);
                if (student.getID() == id) {
                    return student;
                }
            }
            return null;
        }
    }
       
    
  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.
    public void displayClass(int class)
    {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getClass() == class) {
                System.out.println(student.getFirstName() + " " + student.getLastName());
            }
        }
    }
       
    
  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.
    public void applyScholarship()
    {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getClass() != 4 && student.getGPA >= 3.5) {
                student.setTuition(student.getTuition() - 2000);
            }
        }
    }
       
    

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.

    public void cyclicShiftLeft(int[ ] myArray)
    {
        int temp = myArray[0];
        for (int i = 1; i < myArray.length; i++) {
            myArray[i - 1] = myArray[i];
        }
        myArray[myArray.length - 1] = temp;
    }
       
    
  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);
    }
       
    
    2
    2
    4
    16
    7
       
    
  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.
    public void printDivisible(int Num1, int Num2)
    {
        for (int i = 1; i <= 100; i++) {
            if (i % Num1 == 0 && i % Num2 == 0) {
                System.out.println(i);
            }
        }
    }
       
    
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