Program Design and Development - Solution Key for Test 1 - 2006 | C SC 227, Exams of Computer Science

Material Type: Exam; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Fall 2006;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-u4z
koofers-user-u4z 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ANSWERS C Sc 227 Practice Test 1 Fall 2006
Complete this BEFORE your next section. Note: I did not time this test. It should take you longer than 50 minutes.
1. Write a checkmark Ö to the right of any assertion if it would pass. Leave the line in the comment blank
if the assertion would fail (8pts)
assertEquals(2, j / k); // a. ______
assertEquals(1, j % k); // b. ______
assertEquals(-0.5, x + 1, 1e-12); // c. ___Ö___
assertEquals('U', s.charAt(1)); // d. ______
assertEquals(5, s.indexOf("Arizona")); // e. __Ö____
assertEquals(0, s.indexOf("U of A")); // f. ___Ö___
assertTrue(s.compareTo("ASU") > 0); // g. ____Ö__
assertEquals(2.0, Math.sqrt(j), 1e-12); // h. ______
2. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the
nested
assertEquals( "A", grade(90.0) ); // or some argument > 90.
assertEquals( "B", grade(80.0) ); // or some argument > 80.0 and < 90.0
assertEquals( "C", grade(70.0) );
assertEquals( "D", grade(60.0) );
assertEquals( "E", grade(50.0) );
3. How many times will the following code print "Hello"? 0, Unknown, and Infinite are legitimate answers
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
5. Write a for loop that prints this sequence of integers: -10 –5 0 5 10 15, … , 5555 (6pts)
for (int j = -10; j <= 5555; j = j + 5)
System.out.print (j + " ");
6. Write method occurencesOf that returns the number of times a particular int is found in the Scanner
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;
}
1
pf2

Partial preview of the text

Download Program Design and Development - Solution Key for Test 1 - 2006 | C SC 227 and more Exams Computer Science in PDF only on Docsity!

ANSWERS C Sc 227 Practice Test 1 Fall 2006

Complete this BEFORE your next section. Note: I did not time this test. It should take you longer than 50 minutes.

1. Write a checkmark Ö to the right of any assertion if it would pass. Leave the line in the comment blank

if the assertion would fail (8pts)

assertEquals(2, j / k); // a. ______ assertEquals(1, j % k); // b. ______

assertEquals(-0.5, x + 1, 1e-12); // c. Ö

assertEquals('U', s.charAt(1)); // d. ______

assertEquals(5, s.indexOf("Arizona")); // e. __Ö____

assertEquals(0, s.indexOf("U of A")); // f. Ö

assertTrue(s.compareTo("ASU") > 0); // g. ____Ö__

assertEquals(2.0, Math.sqrt(j), 1e-12); // h. ______

2. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the

nested

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) );

3. How many times will the following code print "Hello"? 0, Unknown, and Infinite are legitimate answers

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

5. Write a for loop that prints this sequence of integers: -10 –5 0 5 10 15, … , 5555 (6pts)

for (int j = -10; j <= 5555; j = j + 5) System.out.print (j + " ");

6. Write method occurencesOf that returns the number of times a particular int is found in the Scanner

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; }

7. Write the output generated by the following code (8pts)

8. Complete method exists to return true if a String is found at least once in an array of Strings.

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; }

9. Write an entire Java class named Histogram that allows for a visual peek at quiz scores where "*"

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; } }

10. Add method removeAllOccurencesOf that removes each occurrence of a given int in an

// 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]; }