Sample Prelim III Questions

CS100 Fall 2000

 

Prelim 3 concentrates on what we have covered since Prelim 2 (up to and including Program 5) but will also include material covered by Prelim 1 and 2.  The exam will focus on one and two-dimensional arrays, and on material illustrated in Programs 4 and 5.  You will not be asked to write MatLab programs, but should understand the “implementation" of MatLab developed in lecture.  The sample questions below are generic, i.e., no attempt has been made to illustrate questions that might refer to program 4 and 5 material.

 

1-D Arrays

 

1. Fill in the code for method RightShift() below that shifts all elements of the array 1 position to the right. Elements falling off the right end should be added at the left. Return the right-shifted array as result. Do not modify the 'array' parameter !

 

Example:

 

1 2 3 4 5 --> 5 1 2 3 4

 

int[] RightShift(int[] array){ ___ }

 

2. Write a static Delete(array, element) method that takes an integer array 'array', and integer 'element', and returns a new array consisting of all elements of 'array' except for occurrences of 'element'. The new array's  size should be the size of 'array' minus the number of elements removed.

 

3. Write a static method IsPalindrome(char[] array) that takes an array of characters and returns true if the characters in the array constitute a palindrome, or false otherwise. A palindrome is the same forwards as backwards, e.g. 'a' 'b' 'c' 'b' 'a'.

 

2-D Arrays

 

4. Write a static method Copy(int[][] matrix) that copies a matrix and returns the copy. Take into account that the sides of the matrix may not be equal, that is the matrix may not be a square.

 

5. Write a static method RightShift(int[][] matrix) that shifts the columns of the matrix 1 position to the right. Columns falling off the right end must be added at the left (wrapping around). Return the right-shifted matrix as result. Do not modify the 'matrix' argument ! You may assume that all columns have the same length.

 

Example (matrix is shown in column-major order):

 

  1   5   9  9   1   5

  2   6   10                  10   2   6

  3   7   11 ---->                 11   3   7

  4   8   11                  12   4   8

 

6. Write a static method Avg(int[][] matrix that computes the average of all the cells present in a matrix. Take into account that the matrix may be 'ragged', i.e., all columns may not have the same length.

Example:

 

  2   3   1   4

  9   0   4   6

  7   3    3

  4   4  

  6   8  

  9  

 

(The example is shown in column-major order and is an array of size 4, containing arrays of sizes 5, 6, 2 and 3 respectively.)