

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


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]);
@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)); }
@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]); }
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 }