

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Fall 2006;
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Complete this BEFORE your next section. Note: I did not time this test. It should take you longer than 50 minutes.
assertEquals(2, j / k); // a. ______ assertEquals(1, j % k); // b. ______
assertEquals('U', s.charAt(1)); // d. ______
assertEquals(2.0, Math.sqrt(j), 1e-12); // h. ______
assertEquals( "A", grade(90.0) ); // or some argument > 90. assertEquals( "B", grade(80.0) ); // or some argument > 80.0 and < 90. assertEquals( "C", grade(70.0) ); assertEquals( "D", grade(60.0) ); assertEquals( "E", grade(50.0) );
int j = 1; int n = 5; while(j <= n) { System.out.print("Hello"); n++;} Infinite int j = 1; while(j <= 11) { System.out.print("Hello"); j = j + 3; } 4 int n = 0; for(int j = 1; j < n; j++) { System.out.print("Hello"); } Zero // Tricky Question for(int j = 1; j <= 11; j++); System.out.print("Hello"); 1
for (int j = -10; j <= 5555; j = j + 5) System.out.print (j + " ");
public void testOccurences() { Scanner scanner = new Scanner("1 2 3 4 1 2 3 1 6"); assertEquals(3, occurencesOf(1, scanner)); // Need to construct a new Scanner each time. Why? scanner = new Scanner("1 2 3 4 1 2 3 1 6"); assertEquals(2, occurencesOf(3, scanner)); scanner = new Scanner("1 2 3 4 1 2 3 1 6"); assertEquals(1, occurencesOf(6, scanner)); … } public int occurencesOf(int search, Scanner scanner) { int result = 0; while (scanner.hasNextInt()) { if (scanner.nextInt() == search) result++; } return result; }
public boolean exists(String search, String[] names, int n) { for (int i = 0; i < n; i++) { if (names[i].compareTo(search) == 0) return true; } return false; }
public class Histogram { private double totalQuizScores; private int numberOfTests; public Histogram() { totalQuizScores = 0; numberOfTests = 0; } public void addAndShowOne(int quizScore) { if (quizScore < 0 || quizScore > 10) System.out.println(quizScore + " is out of range"); else { int counter = quizScore; totalQuizScores += quizScore; numberOfTests += 1; while (counter > 0) { System.out.print("*"); counter -= 1; } System.out.println(" " + quizScore); } } public double getAverage() { return totalQuizScores / numberOfTests; } }
// Note: This gets tricky if you begin at index 0 and valueToRemove // exists at consecutive array indexes. Going backwards helps. public void removeAllOccurrencesOf(int valueToRemove) { for (int index = n - 1; index >= 0; index--) { if (x[index] == valueToRemove) { shiftLeftFrom(index); n--; } } } // or place the loop below inside the loop above public void shiftLeftFrom(int leftIndex) { for (int i = leftIndex; i < n - 1; i++) x[i] = x[i + 1]; }