


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java practice test covering various topics such as expressions, unit tests, loops, and methods. The test includes writing checkmarks for assertions, completing a unit test for a grading method, determining the number of times 'hello' is printed, writing a for loop for a given sequence of integers, writing a method to find the number of occurrences of an integer in a string, and analyzing the output of given java code. The test also includes a bonus question to write a histogram class.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Complete this BEFORE your next section. Note: I did not time this test. It should take you longer than 50 minutes.
public class ExpressionsTest extends junit.framework.TestCase { 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. ______ } }
Percentage Grade
public class GraderTest extends junit.framework.TestCase { public void testGrade() { assertEquals(___________________________________); assertEquals(___________________________________); assertEquals(___________________________________); assertEquals(___________________________________); assertEquals(___________________________________); } 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; } }
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");
import java.util.Scanner; import junit.framework.TestCase; public class TestExists extends TestCase { public void testOccurences() { Scanner scanner = new Scanner("1 2 3 4 1 2 3 1 6"); assertEquals(3, occurencesOf(1, scanner)); assertEquals(2, occurencesOf(3, scanner)); assertEquals(1, occurencesOf(6, scanner)); assertEquals(0, occurencesOf(99, scanner)); }
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]);
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());
public class IntList { private int[] a; private int n; public IntList(int[] x) { a = x; n = a.length; } public String toString() { String result = ""; for(int j = 0; j < n; j++) result = result + a[j] + " "; return result; } // Complete removeAllOccurrencesOf using the initialized instance variables a and n.