Program Design and Development - Practice Test with Answers | 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 2008;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-ywb-1
koofers-user-ywb-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
ANSWERS C Sc 227 Practice Test , Friday 26-Sep-2008
1. Write a checkmark to the right of any assertion if it would pass. Leave the line in the comment blank
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. ______
2a. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the
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) );
2b.
public String grade(double percentage) {
String result;
if (percentage >= 90.0)
result = "A";
else if (percentage >= 80.0)
result = "B";
else if (percentage >= 70.0)
result = "C";
else if (percentage >= 60.0)
result = "D";
else
result = "E";
return result;
}
3. How many times will the following code print "Hello"? 0, Unknown, and Infinite are legitimate
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
4. Write a for loop that prints this sequence of integers: -10 –5 0 5 10 15, … , 5555
for (int j = -10; j <= 5555; j = j + 5)
System.out.print (j + " ");
pf2

Partial preview of the text

Download Program Design and Development - Practice Test with Answers | C SC 227 and more Exams Computer Science in PDF only on Docsity!

ANSWERS C Sc 227 Practice Test , Friday 26-Sep-

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

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 a. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the

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

2b.

public String grade( double percentage) { String result; if (percentage >= 90.0) result = "A"; else if (percentage >= 80.0) result = "B"; else if (percentage >= 70.0) result = "C"; else if (percentage >= 60.0) result = "D"; else result = "E"; return result; }

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

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

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

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

5. Complete method occurencesOf to return how often

public int occurencesOf(int target, Scanner scanner) { int result = 0; while(scanner.hasNext() ) { int nextInt = scanner.nextInt(); if (nextInt == search) result++;; } return result; }

6. Write the output generated by the following code

7. public boolean exists (Search target, String[] a, int n) {

for ( int i = 0; i < n; i++) {

// Can use equals, but do NOT use ==

if (search.compareTo(names[i]) == 0)

return true ;

return false ;

  1. Note: The method heading was required in this , usually 3 points

public boolean equals(String227 other) { for ( int i = 0; i < Math. min ( this .length(), other.length()); i++) { if (charAt(i) != other.charAt(i)) { // Fond 2 that are not equal return false ; } } // Reach this point if all chars were equal up to the last char of the shorter one // If both have same length (number of chars), return true return this .length() == other.length(); }

9. @Override

public String toString() { String result = ""; for ( int j = 0; j < n; j++) result = result + a[j] + " "; return result; }

// Searching right to left makes this easier. Also. you could use a nested loop to shift. public void removeAllOccurrencesOf( int valueToRemove) { for ( int index = 0; index < n; index++) { if (a[index] == valueToRemove) { shiftLeftFrom(index); n--; index--; } } }

private void shiftLeftFrom( int leftIndex) { for ( int i = leftIndex; i < n - 1; i++) a[i] = a[i + 1]; }