

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
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


assertEquals(2, j / k); // a. ______ assertEquals(1, j % k); // b. ______
assertEquals('U', s.charAt(1)); // d. ______
assertEquals(2.0, Math.sqrt(j), 1e-12); // h. ______
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) );
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
for (int j = -10; j <= 5555; j = j + 5) System.out.print (j + " ");
public int occurencesOf(int search, Scanner scanner) { int result = 0; while(scanner.hasNext() ) { int nextInt = scanner.nextInt(); if (nextInt == search) result++;; } return result; }
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; } }*
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; }