9/21/05 CS 240 Exam 1 Page 4

Computer Science II

Fall 2005, Wednesday, 9/21/05

100 points

Name ______

Explain or give the UNIX or vi commands or keystrokes to do the following tasks.

[27 pts]

1.  Display java source filenames in your current directory. ______[2]

2.  Display all file details found in /academic/cs2 ______[2]

3.  Create a subdirectory called test. ______[2]

4.  Change your current working directory to test ______[2]

5.  Move all .java files from /academic/cs2 to this current directory ______[2]

6.  Delete all files that contain tmp anywhere in the name from the directory one level up. ______[2]

7.  Display only the last 10 lines of the file test.java . ______[2]

8.  Start the visual editor on file test.java ______[2]

9.  Begin capturing the screen display to a file for printing later. ______[1]

10.  Give the command or keystroke to end the screen display capture. ______[1]

11.  Compile the code in prog.java ______[2]

12.  Run the Java program prog using the file data as a command line argument. ______[2]

13.  What keystroke cancels or kills a program in Unix? ____
What keystroke may work as backspace? _____ [2]

14.  Run the Unix program filter (not a Java class file) using the file data for input and pipe the output stream through more. ______[3]

15.  Give the keystroke(s) in vi to perform the following actions

[8 pts]

_____ move the cursor down 1 line (1 char)

_____ start insertion mode (1 char)

_____ end insert mode (1 char)

_____ delete the current character (1 char)

_____ repeat the previous editing operation (1 char)

_____undo the previous action (1 character)

_____exit and save the file (2 chars)

_____ exit and do not save the file or its changes (3 chars)

15. Arrays.

[12 pts]

Give a code segment to define an array of 100 double type elements and a for loop that initializes each element to its reciprocal (element 0 contains a 0 as an exception because…., element 1 contains a 1, element 2 contains 0.5, element 3 contains 1/3, etc.). [7]

b. Explain in general terms how a one dimension array can be “extended” such as doubled in size. [5]

16. Abstract Data Types. (True/False)

[5 pts]

a.  ____ The ArrayList class is meant to replace the deprecated vector class.

b.  ____ The ArrayList requires the use of wrappers in order to store primitive types.

c.  ____ All ADTs implemented as Java classes ultimately inherit from the Object class.

d.  ____ An abstract class can be used to define only shared methods inheritable by related subclasses.

e.  ____ An ADT defines the data organization and its operations as well as the implementation.

17. Object-oriented programming design principles. Briefly define the following terms.

[8 pts]

a. Pre-condition

b. Post-condition

c. Information Hiding

d. Black box testing


18. Miscellaneous Java concepts (True/False)

[10 pts]

a. _____ Each regular Java class must be defined in its own separate file with the extension .java and that file name must match the class name.

b _____ The contents of the .class file are called bytecodes, which are machine dependent.

c. _____ Garbage collection in Java runs in the background to recover memory from the unreferenced objects.

d. _____ The toString method that can be defined for each class is an example of a mutator.

j. _____ Protected variables are inheritable and public to the user of the subclass.

e. _____ An iterator ensures that all elements of a collection can be accessed in turn.

f. _____ A class that inherits from an existing class is called the subclass.

g. _____A typical class is one usually composed of private instance variables and public methods.

h. _____The Exception class has the property of being throwable.

i. _____ Two strings compared by the compareTo() results in an integer whose magnitude has significance.

19. Number the steps of the software development cycle (Waterfall method) from 1 to 6 in correct order.

[5 pts]

_____ Design _____ User request

_____ Integration _____ Maintenance

_____ Analysis _____ Implementation

20. We have discussed the differences in the implementations between an UnsortedStringList and SortedStringList. Describe for each of the abstract operations how the operation is best accomplished in a sorted setting and an unsorted setting, assuming an array is used to store the list.

[12 pts]

Abstract operation / For UnsortedStringLists / For SortedStringLists
Insert a new unique element into the list / /
Delete the first element from the list / /
Determine if an element exists in the list / /

21. Below are segments of code similar from class and your recent project. Assume Date is a class that holds the day, month and year as ints along with a String description. A constructor can take three integers and a String, or another date instance and a string. The toString() method returns a string only in mo/day/yr format, getMonth(), getDay() and getYear() are obvious accessor methods and method getDescription() returns the description string. Subclass IncDate extends Date and adds the increment() method that advances the day by one and rolls over to the next month or year as necessary.

[18 pts]

0 try {

1 IncDate [] events;

2 events = new IncDate[5];

3 events[0] = new IncDate (8,29,2005, “Classes start”);

4 events[1] = new IncDate (9,5,2005, “Labor Day”);

5

6 IncDate today = new IncDate(9,21,2005,”CS 240 Exam 1”);

7 IncDate tomorrow = new IncDate(today,”Work on Program 3”);

8 tomorrow.increment();

9 events[2] = today;

10 events[3] = tomorrow;

11 events[4] = new IncDate (9,31,2005,”Mountain Day”);

12 } catch (Exception e) {

13 System.out.println(“Error in initialization: “+e.getMessage());

14 }

15 for(i=0; i<____; _____){

16 System.out.println( ______[____] + “--“ +

17

18 ______[____] . ______());

19

20 }

21

a.  Fill in lines 15 and 18 to have the date and its description printed. [4]

b.  Draw a picture of the memory layout for the array and objects resulting from executing lines 3-10. [4]

c.  Lines 6-7 invoke two different constructors by the same name. What object oriented feature is this? [1]

d.  Describe the sequence of actions Java carries out in lines 11-13 Hint: note the data error? [4]

e.  What is actually printed by the program segment in lines 15-20? [5]