Download Practice Test 3 for Computer Science 127A with Java Code and Exercises and more Exams Computer Science in PDF only on Docsity! C Sc 127A Practice Test 3 SL__________ Name ___________________________ 100pts 1. 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. Complete method exists to return true if the String argument is found at least once in an array of Strings. If the String is not found, return false. (12pts) @Test public void testExists() { ManyMethods m = new ManyMethods(); String[] names = { "Li", "Devon", "Sandeep", "Chris", "Regan" }; assertTrue(m.exists("Li", names)); assertTrue(m.exists("Sandeep", names)); assertFalse(m.exists("NOT HERE", names)); } public boolean exists(String target, String[] a) { 3. Given an array of integers, return true if 3 appears in the array exactly three times and no 3's are next to each other (12pts). haveThree({3, 1, 3, 1, 3}) → true haveThree({3, 1, 3, 3}) → false haveThree({3, 4, 3, 3, 4}) → false public boolean haveThree(int[] nums) { 1 3. In class String127A, complete the equals method to return true if two String127A objects have the same exact characters at the same indexes and their lengths are the same. Return false otherwise. These assertions must pass: (12pts) String127A s1 = new String127A("abc"); String127A s2 = new String127A("abc"); String127A s3 = new String127A("ABC"); assertTrue(s1.equals(s2)); assertFalse(s1.equals(s3)); ///////////////////////////////////////////////////// public class String127A { private int n; private char[] theChars; public String127A(String initialString) { theChars = new char[128]; n = initialString.length(); for (int i = 0; i < n; i++) theChars[i] = initialString.charAt(i); } public boolean equals(String127A other) { // Complete the equals method here public void removeAll(char ch) { 4. In class String127A above, complete method removeAll to remove all occurrences of the given character (if there are any) in the String127A object. These assertions must pass: 16pts) String127A str = new String127A("abABac"); str.removeAll('a'); 2