Practice Test Answers for C Sc 227 - Java Programming, Exams of Computer Science

The answers to the practice test for the java programming course, c sc 227. It includes assertions to check, completed unit tests, and solutions to various programming questions. Topics covered include arithmetic operations, character comparison, string methods, control structures, and loops.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-xeq
koofers-user-xeq 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Practice Test 1 Answers for test 1, Friday 15-Feb-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. ______
2. 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) );
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 + " ");
7. Complete method occurencesOf to return how often
public int occurencesOf(int search, 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
5 5 6 2 3
8. 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;
1
pf2

Partial preview of the text

Download Practice Test Answers for C Sc 227 - Java Programming and more Exams Computer Science in PDF only on Docsity!

C Sc 227 Practice Test 1 Answers for test 1, Friday 15-Feb-

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

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

7. Complete method occurencesOf to return how often

public int occurencesOf(int search, 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

8. 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; } }*

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