cs631midtermsolutions

cs631midtermsolutions - Computer S cience D ept. Loyola...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer S cience D ept. Loyola College Spring 2006 CS631: Computer Science II Midterm Spring Solutions I. Short answer 1. Which statement makes the most sense? Circle the answer. CS631 is-a fun class CS631 has-a fun class. is-a makes the most sense since CS631 is-a class, but you could say has-a as we deal with Java classes all the time – question is whether any of the Java classes are fun. 2. What is the ADT concept, and how does it relate to a Java class? The Abstract Data Type concept is that you have data plus related operations, and the operations represent an abstract, consistent way of treating that data (like a stack allows a symmetric set of push and pop operations). ADTs also have information hiding, so you don’t need to know the details of how the data is represented. Java classes are a language mechanism for creating ADTs with tools like data members, methods as operations and private data members for information hiding. 3. Draw a UML diagram for the segment of Java on the last page. II. Recursion 1. What does this print if called with n = 17654? This program prints the integer n in reverse order – so here it prints 45671. public static void printInt ( int n ) { if (n < 10) System.out.print( n ); else { System.out.print( n % 10 ); printInt ( n / 10 ); } } } 2. Write a recursive program to sum the elements of an integer array that are greater than 10. public int sumInt ( int A, int n ) { if (n == 0) return ( A[0] > 10 ? A[0] : 0 ); else return ( A[0] > 10 ? A[0] : 0 ) + sumInt ( A, n – 1 ); } This answer uses the selection operator ( ? : ). It can also be done with if statements. III. Exceptions and File I/O (20 points) 1. Write a short section of Java code to print the characters in the file “data.dat” to System.out. Use appropriate exception handling. try { FileReader inStream = new FileReader(“data.dat”); BufferedReader ins = new BufferedReader(inStream); line = fileInStream.readLine(); while (line != null) { System.out.println(lineNumber); line = fileInStream.readLine(); } ins.close(); outs.close(); } catch (IOException ex) { System.out.println("i/o error: " + ex.getMessage()); ex.printStackTrace(); } 2. In what order must exception be written if multiple catch blocks are attached to one try block? The most specific block must come first. IV. Linked Lists 1. Given this diagram, draw pictures which show the result of each set of statements (in each case starting from the original list.) a) p = new Node(); p.setItem(3); p.setNext(null); head = p; p = new Node(); p.setItem(3); p.setNext(head.getNext()); head = p; b) 2. Give statements to create a node and insert it after 7. (Starting from original list.) p = new Node(); p.setItem(8); p.setNext(head.getNext().getNext()); head.getNext().setNext(p); 3. Write two linked list routines – one to print a linked list in order using a loop, and one to print a linked list in reverse order using recursion. public void printList( Node p ) { while ( p != null ) { System.out.printf( p.toString() ); p = p.next(); } } public void printReverse ( Node p ) { if ( p != null ) { printReverse ( p.getNext() ); System.out.printf( p.toString() ); } } V. Stack 1. Show the input, stack and output in the conversion of “(1+2)*3” to postfix. input stack output ( 1 + 2 ) * 3 ( ( (+ (+ * * 1 1 12 12+ 12+ 12+3 12+3* 2. Complete the method to push a value on a stack stored in an array S[n] with StackTop the index of the first position past the last value. public class Stack { int A = new int[100] int StackTop = 0; public void push ( int value ) { if (StackTop < 100) { A[StackTop] = value; StackTop++; } else throw new StackException(); } VI. Algorithm efficiency 1. What are the big-Oh functions for the following operation counts? a) 12 * n 4 + 789 * n + 189231 O(n4) b) 89 " log 2 n + 3 * n O(n) c) 45 * n + 19 + n * log 2 n O(n*log2n) ! ! ! 2. Count the assignment operations in this nested loop. for (int i = 1; i < n; i++) for (j = i; j > 0; j--) X = i * j; The outer loop runs n-1 times (1 to n-l). The inner loop runs i times for each value of the outer n ( n " 1) loop, so it goes through the values 1, 2, 3, …, n-1. This gives the sum 2 ! ...
View Full Document

This note was uploaded on 10/21/2009 for the course CS CS 631 taught by Professor Dr.rogerd.eastman during the Spring '09 term at Loyola Maryland.

Ask a homework question - tutors are online