Java Programming Practice Quiz 2: Array Manipulation and Loops, Quizzes of Computer Science

A java programming quiz consisting of six problems. The problems involve array manipulation, method completion, and determining the tightest upper bound runtimes of loops. Students are required to complete as many problems as they can and write the expected output for certain code snippets.

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-tve
koofers-user-tve 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Practice Quiz 2 Name_________________________________ 50pts
Complete as many Array2 JavaBat problems as you can. Here are some other sample questions
1. Write the output generated by the following code (4pts)
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 a String is found at least once in an array of Strings. If the String
is not found in the array, return false. (10pts)
@Test public void testExists() {
String[] names = { "Li", "Devon", "Sandeep", "Chris", "Regan" };
assertTrue(exists("Li", names, 5));
assertTrue(exists("Sandeep", names, 5));
assertTrue(exists("Regan", names, 5));
assertFalse(exists("NOT HERE", names, 5));
}
public boolean exists(String search, String[] names, int n) {
3. Complete method sortEnds such that the smallest value is swapped with the value at index 0 and the
largest value is swapped with the value at index n-1. The assertions in the test method must pass. (10pts)
@Test public void testSortEnds () {
int[] x = { 4, 3, 1, 5, 7, 6, 2 };
sortEnds(x, x.length);
assertEquals(1, x[0]);
assertEquals(3, x[1]);
assertEquals(4, x[2]);
assertEquals(5, x[3]);
assertEquals(2, x[4]);
assertEquals(6, x[5]);
assertEquals(7, x[6]);
}
public void sortEnds(int[] x, int n) {
pf3

Partial preview of the text

Download Java Programming Practice Quiz 2: Array Manipulation and Loops and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227 Practice Quiz 2 Name_________________________________ 50pts

Complete as many Array2 JavaBat problems as you can. Here are some other sample questions

1. Write the output generated by the following code (4pts)

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 a String is found at least once in an array of Strings. If the String

is not found in the array, return false. (10pts)

@Test public void testExists() { String[] names = { "Li", "Devon", "Sandeep", "Chris", "Regan" }; assertTrue(exists("Li", names, 5)); assertTrue(exists("Sandeep", names, 5)); assertTrue(exists("Regan", names, 5)); assertFalse(exists("NOT HERE", names, 5)); }

public boolean exists(String search, String[] names, int n) {

3. Complete method sortEnds such that the smallest value is swapped with the value at index 0 and the

largest value is swapped with the value at index n-1. The assertions in the test method must pass. (10pts)

@Test public void testSortEnds () { int[] x = { 4, 3, 1, 5, 7, 6, 2 }; sortEnds(x, x.length); assertEquals(1, x[0]); assertEquals(3, x[1]); assertEquals(4, x[2]); assertEquals(5, x[3]); assertEquals(2, x[4]); assertEquals(6, x[5]); assertEquals(7, x[6]); }

public void sortEnds( int [] x, int n) {

4. Write the output generated by the following code using class TableOfInts. (6pts)

TableOfInts aTable = new TableOfInts(3, 3); System.out.println(aTable.toString()); public class TableOfInts { private int[][] my_data; private int my_lastRow; private int my_lastCol; public TableOfInts(int maxRow, int maxCol) { // Construct a maxRow by maxCol array of integers my_lastRow = maxRow; my_lastCol = maxCol; my_data = new int[my_lastRow][my_lastCol]; // Initialize the two-dimensional array elements for(int row = 0; row < my_lastRow; row++) { for(int col = 0; col < my_lastCol; col++) { my_data[row][col] = (row+1) * (col+1); } } } @Override public String toString() { // Return all elements as a string in a tabular fashion (rows on separate lines) String result = ""; for(int row = 0; row < my_lastRow; row++) { for(int col = 0; col < my_lastCol; col++) { result += " " + my_data[row][col]; result += "\n"; } return result; } // Question 5: Add method average() here }

5. To class TableOfInts above add method average that returns the average of all elements in the

TableOfInts object. Using the object above, aTable. average() should return 4.0. (10pts)

Write output here: