CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming Language. You may use extra sheets if need be. Q1: Concepts and Definitions 25 marks By declaring an array that will contain 10 floating-point numbers, show the difference between the following: Array size, Array element Array index List two example situations where you may wish to use two-dimensional arrays in organizing your program data Give one example where you might wish to use a three dimensional array 1
Sorting is generally more time consuming than searching. Yet, fast searching algorithms require sorting as an initial step. Give one examples of a situation where it may be useful to sort the data before it is searched. Give one reason (or an example) when it won t be necessary to sort the data before it is searched. List two parameters that are generally considered in the analysis of an algorithm Given the functions that grow proportional to the following formulae, arrange them in increasing order of size (as N becomes large): N N 2 N 4 N 3 N log N N log N log (log N) Which of the following are not valid Java keywords? Just circle them java, class, Class, extends, extend, final, finalize, object, JFrame, swing, awt, null, this, Scanner, interface, FileReader, PrintWriter, implements 2
Q2: Errors in C++ programs or C++ statements 30 marks In each of the following programs or program segments, find the error(s) in the program. Note that the error could be a syntax error, a logic error, missing declaration, missing comments, missing statements, etc. There could be more than one error on the same line. Mark ALL the errors. (a) Question2.java Java program for question 2 Public class Question2() final int N = 20; static Scanner ins = new Scanner(System.in); static int main(string [] args) int array (N); int product for (int k =0; k <= N; k++) array (k) = ins.nextint(); // function call product = arrayproduct (int array, N); //function definition void arrayproduct(int array, int N); float product; float k; product = 0; for (k=0; k > N; k++) System.out.println ( processing element + k +); product = product * array (k); return product; 3
(b) Indicate the syntax errors in the following program. Note, this is supposed to be a Java program, and not just a program segment. (You can also correct the errors if that is easier) public class RTwo extends JFrame public RTwoProgram() private JLabel length, width, area; settitle("good day area"); length = JLabel("Enter the length); width = JLabel("Enter the width); area = JLabel("Area: "); containerpane = ContentPane(); pane.setlayout(gridlayout(4, 1)); setsize(width, HEIGHT); setvisible(); setdefaultcloseoperation(exit_on_close); public static void main( String args[] ) RTwoProgram R2 = new RTwoProgram (); 4
Q3: True or False Question s. 10 marks For each of the following, indicate whether the assertion is TRUE or FALSE. s/n Assertion True or False 1 In Java, the array index can be any number, example double, float, or int 2 A file stream is automatically connected to a physical file at the time the stream is declared 3 In Java event handling, clicking a button can be taken as one event 4 A file To use a 2-dimensional array in a function, we are only required to specify the number of rows (and not the columns) in defining the function. 5 To pass a one-dimensional array to a function, we must always specify the size of the array. 6 Any multi-dimensional array can be represented as a set of some one-dimensional arrays 7 In general, binary search is always faster than sequential search 8 A program that requires time that is proportional to N is generally more efficient than one that requires running time that is proportional to N log N 9 Given a class, we can declare an array of objects of the given class. Conversely, we can also have a class that contains an array in its definition 10 In Java, JFrame is a class 5
Question 4: Program Outputs Ignore syntax errors in this section. 10 marks Consider the following program segment: int numbers[3][3]; int k, j, count; count =0; for (k=0; k< 3; k++ ) for (j=0; j< 3; j++) numbers[k][j] = 0; count++; // second nested for loop for (k=0; k< 3; k++ ) for (j=0; j< 3; j++) if (j==k) numbers[k][j]=(k+j)*(k+j); System.out.print( + numbers[k][j] ) ; // one space inside quotation marks System.out.println(); b1. List the content of the array numbers when execution reaches the line marked second nested for loop. b2. What will be the output of the program segment when it is executed? 6
Question 5: Writing a Program 25 marks (a) You are required to write a program to display the square root of the numbers in a matrix. The program will be based on a user-defined method called matrixelementsqroot( ). The method will take one 2-D array (i.e. a matrix) A as its input, and produce another matrix (2-D array) C, whereby each element in the output matrix C will correspond to the square root of elements at the corresponding position in A. That is C = ij A ij Assume each matrix is of size N x N, where N is 4. You can also assume that the matrix is already initialized to some values. You program must include the following: b. How this method will be called by the main() program, c. The definition for the method matrixelementsqroot( ) Please see last question on next page 7