Overview | Schedule | Announcements | Resources | Assignments | Home |
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.
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; }
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
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
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];
public void init() { for (int row = 0; row < 10; row++) { for (int col = 0; col < 11; col++) { grid[row][col] = 0; } } }
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; }
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
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; }
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:
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) |
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; { }
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; } }
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()); } } }
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); } } }
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; }
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
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 |
DePauw
University , Computer Science
Department , Spring 2005
Maintained by Brian
Howard ( bhoward@depauw.edu
).
Last updated