Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Practice Test: Coding Exercises and Unit Tests, Exams of Computer Science

Java coding exercises and corresponding unit tests for various programming concepts such as arithmetic operations, string manipulation, loops, and methods. Students can use this document to practice and test their understanding of these concepts.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-38b
koofers-user-38b 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Java Practice Test: Coding Exercises and Unit Tests and more Exams Computer Science in PDF only on Docsity! C Sc 227 Practice Test 1 Section Leader _________ Your Name _________________________ Complete this BEFORE your next section. Note: I did not time this test. It should take you longer than 50 minutes. 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 (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. ______ } } 2. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the nested selection. The method represents 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() { 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; } 1 3. How many times will the following code print "Hello"? 0, Unknown, 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"); 5. Write a for loop that prints this sequence of integers: -10 –5 0 5 10 15, … , 5555 (6pts) 6. Write method occurencesOf that returns the number of times a particular int is found in the Scanner argument's input String. (12pts) @Test 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)); } 7. 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]); 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved