C Sc 127A Practice Test 2 Section Leader ______Name ______(150pts)

0. What is your favorite color? None is a valid answer ______

1. How many times will each code fragment print "Hello"? Zero and infinite are legitimate answers.

int n = 3;
int c = 1;
while(c <= n) {
System.out.print("Hello ");
c = c + 1;
}
/ Scanner scanner = new Scanner("a b c d e f");
while(scanner.hasNext()) {
System.out.println("Hello ");
System.out.println(scanner.next());
System.out.println(scanner.next());
}
int c = 10;
while(c > 0) {
System.out.print("Hello ");
c = c - 3;
} / // Tricky question
Scanner scanner = new Scanner("a b c d e f");
while(scanner.hasNext()) {
System.out.println("Hello ");
}

2. Write a while loop that prints all strings in the scanner two times.

Scanner scanner = new Scanner("a b c d e f"); // Output: a a b b c c d d e e f f

3. How many times will each code fragment print "Hello"? Zero and infinite are legitimate answers.

int n = 10;
for(int c = 0; c <= n; c = c + 3) {
System.out.print("Hello ");
}
/ int n = 15;
for(int c = 1; c <= n; c += 3) {
System.out.print("Hello ");
}
int n = 5;
for( int c = 1; c <= n; c++ ) {
System.out.print( "Hello " );
} / int n = 5; // Tricky question
for( int c = 1; c <= n; c-- ) {
System.out.print( "Hello " );
c++;
}

4. Write a for loop that print this sequence: -10 -5 0 5 10 15, ... , 5550, 5555 (6pts)

5. Complete method mixStrings that when given two strings, A and B, it returns a bigger string made of the first char of A, the first char of B, the second char of A, the second char of B, and so on. Any leftover chars go at the end of the result. (14pts)

mixString("abc", "xyz") → "axbycz"

mixString("Hi", "There") → "HTihere"

mixString("xxxx", "There") → "xTxhxexre"

public String mixStrings(String a, String b) {

6. Given a non-empty String like "Code" return a string like "CCoCodCode". It is the first char, followed by the first two chars, followed by the first three chars, and so on. Also complete the test method such that it has any two assertions you desire that are not the same as these three. (14pts)

stringSplosion("Code") → "CCoCodCode"

stringSplosion("abc") → "aababc"

stringSplosion("x") → "x"

assertEquals( ______, stringSplosion(______));

assertEquals( ______, stringSplosion(______));

public String stringSplosion(String str) {

7. Complete method occurencesOf to return how often a particular integer is found in the Scanner argument's input String. These assertions that help explain the expected output must pass.

Scanner scanner = new Scanner("11 2 3 11 2 3 11");

assertEquals(3, cf.occurencesOf(11, scanner));

Scanner scanner2 = new Scanner("6 4 5 6");

assertEquals(2, cf.occurencesOf(6, scanner2));


// Precondition: scanner has only valid integers to scan, but it may also be empty.

public int occurencesOf(int target, Scanner scanner) {

8. Complete the code to read all lines from an input file and print the number of words per line. When the file named inputText has the lines to the left, the output should be what is shown on the right.

the quick brown
fox
jumped over the lazy,
very lazy
dog / line 1: 3
line 2: 1
line 3: 4
line 4: 0
line 5: 2
line 6: 1

Scanner inFile = new Scanner(new File("inputText"));

Use this initialization to answer all questions that follow:

int[] x = new int[100];

9. ______How many integers may be properly stored in x?

10. ______Which integer is used as the index to reference the first element in x?

11. ______What is the value of x[23]?

12. ______Write the code that stores 78 into the 2nd last element of x.

13. What is the difference between x and x[0]?

14. Write the output generated by the following code:

int[] x = { 5, 6, 2, 3, 5 };

for(int c = x.length - 1; c > 0; c--)

x[c] = x[c-1];

for(int c = 0; c < x.length; c++)

System.out.print(x[c] + " ");

15. Complete method exists to return true if the String argument is found at least once in an array of Strings. If the string argument target is not found, return false.

String[] names = { "Li", "Devon", "Sandeep", "Chris", "Regan" };

assertTrue(af.exists("Li", names, 5));

assertTrue(af.exists("Sandeep", names, 5));

assertFalse(af.exists("NOT HERE", names, 5));

public boolean exists(String target, String[] a, int n) {

16. Complete method post4 such that when given a full array of ints, post4 returns a new array containing the elements from the original array that come after the right most 4 in the original array argument. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0. (12pts)

post4({2, 4, 1, 2}) returns {1, 2}

post4({4, 1, 4, 2}) returns {2}

post4({4, 4, 1, 2, 3}) returns {1, 2, 3}

post4({4}) returns {}

public int[] post4(int[] nums) {

17. Write the output generated by the following code.

int target = 11;

int[] x = { 1, 3, 5, 7, 9 };

int left = 0;

int right = x.length - 1;

int mid = (left + right) / 2;

while (left <= right) {

System.out.println(left + " " + mid + " " + right);

if (target == x[mid]) {

System.out.println("target");

break;

}

else if (target > x[mid])

left = mid + 1;

else

right = mid - 1;

mid = (left + right) / 2;

}

18. Given a non-empty array of ints, return a new array containing the elements from the original array that come after the last 4 in the original array. The original array will contain at least one 4. Note that it is valid in Java to create an array of length 0. (12pts)

post4({2, 4, 1, 2}) → {1, 2}

post4({4, 1, 4, 2}) → {2}

post4({4, 4, 1, 2, 3}) → {1, 2, 3}

public int[] post4(int[] nums) {

19. Given a filled array of non-zero integer, modify it so all negative values are left of the positive values, in the lower indexes. The method must also return the index of the first or leftmost positive integer.

public void testNegativesLeft() {

int[] a = { 1, -2, 5, 3, -4, 5, -6 };

int firstPositive = rearrange(a, 7);

assertEquals(3, firstPositive);

assertTrue(a[0] < 0);

assertTrue(a[1] < 0);

assertTrue(a[2] < 0);

assertTrue(a[firstPositive] > 0);

assertTrue(a[4] >= 0);

assertTrue(a[5] >= 0);

assertTrue(a[6] >= 0);

}

public int rearrange(int[] nums) {

5