Practice Test for ControlFun.java: Java Methods and Unit Testing, Exams of Computer Science

A practice test for the controlfun.java file, which includes various java methods and their corresponding unit tests. The test covers topics such as assertions, grading policy, loops, and custom methods. Students are expected to complete the test method and write additional methods as instructed.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-tih
koofers-user-tih 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
C Sc 227 Practice Test 1, Section Leader __________ Your Name __________________________ 100pts
1. Write a checkmark to the right of any assertion in the following test method if it would pass. Leave the line in the
comment blank if the assertion would fail. (8pts)
@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. ______
}
2a. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the nested selection.
Assume the method grade will be added to class ControlFun. Grade must represent this grading policy. (10pts)
Percentage Grade
90.0 percentage "A"
80.0 percentage < 90.0 "B"
70.0 percentage < 80.0 "C"
60.0 percentage < 70.0 "D"
percentage < 60.0 "E"
@Test public void testGrade() {
ControlFun cf = new ControlFun();
assertEquals(___________________________________);
assertEquals(___________________________________);
assertEquals(___________________________________);
assertEquals(___________________________________);
assertEquals(___________________________________);
}
2b. Complete method grade as yet another method in class ControlFun. (10pts)
public String grade(double percent) {
pf3
pf4

Partial preview of the text

Download Practice Test for ControlFun.java: Java Methods and Unit Testing and more Exams Computer Science in PDF only on Docsity!

C Sc 227 Practice Test 1, Section Leader __________ Your Name __________________________ 100pts

1. Write a checkmark  to the right of any assertion in the following test method if it would pass. Leave the line in the

comment blank if the assertion would fail. (8pts)

@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. ______ }

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

Assume the method grade will be added to class ControlFun. Grade must represent this grading policy. (10pts)

Percentage Grade

90.0  percentage "A"

80.0  percentage < 90.0 "B"

70.0  percentage < 80.0 "C"

60.0 percentage < 70.0 "D"

percentage < 60.0 "E"

@Test public void testGrade() { ControlFun cf = new ControlFun(); assertEquals(___________________________________);

assertEquals(___________________________________);

assertEquals(___________________________________);

assertEquals(___________________________________);

assertEquals(___________________________________); }

2b. Complete method grade as yet another method in class ControlFun. (10pts)

public String grade (double percent) {

3. How many times will each code fragment print "Hello"? zero and Infinite are legitimate answers (8pts)

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

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

Assume occurencesOf will yet another method in class ControlFun. These assertions must pass (10pts)

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

public class ControlFun { //... // Assume this method is in ControlFun.java // Precondition: scanner has only valid integers to scan public int occurencesOf (int target, Scanner scanner) {

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

int[] x = { 5, 6, 2, 3, 5 }; int n = x.length; for(int j = n - 1; j > 0; j--) x[j] = x[j-1]; for(int j = 0; j < n; j++) System.out.print(" " + x[j]);

9. Add method toString to class IntList to return a string with all integer elements followed by a blank " ". Also

add method removeAllOccurencesOf tot removes every occurrence of the int argument in an IntList object. The

IntList class must store elements in an array. The code to the left must generate the output shown (18pts)

int[] x = { 1, 5, 3, 4, 5, 5, 5, 6, 5, 8 }; IntList list = new IntList(x);

System.out.println(list.toString());

list. removeAllOccurrencesOf(5) ;

System.out.println(list.toString());

Output when removing all 5s

Note: toString has a blank after every number

public class IntList { private int[] a; private int n;

public IntList(int[] x) { a = x; n = a.length; }

// Complete toString @Override public String toString() {

// Write removeAllOccurrencesOf including the method heading