Java Practice Quiz: Unit Tests and Method Implementations, Quizzes of Computer Science

A java practice quiz for a university or high school course. It includes a set of unit tests for various methods, as well as instructions to complete the implementation of a tax calculation method. The quiz also includes a question about the number of times 'hello' is printed in different loops and a request to write a new method for counting the occurrences of a specific integer in a given input string.

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-8ik
koofers-user-8ik 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Summer 2007 Practice Quiz 1 Your Name ___________________________ /50
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 (16pts)
@Test
public void testExpressions() {
int j = 5;
int k = 3;
double x = -1.5;
String s = "U of Arizona";
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 tax (fill in the blanks) so it executes every branch of code in the nested selection.
The method represents the tax policy for Ebonia. Also complete the method
Income
$0 to $10,000 3% of income
$10,000 to $20,000 $300, plus 4% of the amount over $10,000
$20,000 to $30,000 $700, plus 5% of the amount over $20,000
$30,000 and over $1,200, plus 6% of the amount over $30,000
@Test
public void testGrade() {
assertEquals(__________, tax(____________), 1e-12);
assertEquals(__________, tax(____________), 1e-12);
assertEquals(__________, tax(____________), 1e-12);
assertEquals(__________, tax(____________), 1e-12);
assertEquals(__________, tax(____________), 1e-12);
}
// Complete this method here
public double tax(int income)
1
pf2

Partial preview of the text

Download Java Practice Quiz: Unit Tests and Method Implementations and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227 Summer 2007 Practice Quiz 1 Your Name ___________________________ /

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 (16pts)

@Test public void testExpressions() { int j = 5; int k = 3; double x = -1.5; String s = "U of Arizona"; 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 tax (fill in the blanks) so it executes every branch of code in the nested selection.

The method represents the tax policy for Ebonia. Also complete the method

Income $0 to $10,000 3% of income $10,000 to $20,000 $300, plus 4% of the amount over $10, $20,000 to $30,000 $700, plus 5% of the amount over $20, $30,000 and over $1,200, plus 6% of the amount over $30, @Test public void testGrade() { assertEquals(__________, tax(____________), 1e-12); assertEquals(__________, tax(____________), 1e-12); assertEquals(__________, tax(____________), 1e-12); assertEquals(__________, tax(____________), 1e-12); assertEquals(__________, tax(____________), 1e-12); } // Complete this method here public double tax(int income)

  1. How many times will each code fragment print "Hello"? 0, Unknown, and Infinite are legitimate answers int j = 1; int n = 5; while(j <= n) { System.out.print("Hello"); n++; } int j = 1; while(j <= 11) { System.out.print("Hello"); j = j + 3; } int n = 0; for(int j = 1; j < n; j++) { System.out.print("Hello"); } // Tricky Question for(int j = 1; j <= 11; j++); System.out.print("Hello");

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

5. Write occurencesOf to return how often a particular int is found in the Scanner argument's input String.

@Test public void testOccurences() { Scanner scanner = new Scanner("1 2 3 4 1 2 3 1 6"); assertEquals(3, occurencesOf(1, scanner)); assertEquals(1, occurencesOf(6, scanner)); assertEquals(0, occurencesOf(99, scanner)); }