2/11/09 CS 240 Exam 1 Page 3

Computer Science II

Spring 2009, Wednesday, 2/11/09

100 points

Name ______

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

[28 pts]

1.  Display all filenames (including hidden ones) found in your current directory. ______[2]

2.  Display only the .class filenames found in your subdirectory cs2 ______[2]

3.  Delete all files that contain temp in the name. ______[2]

4.  Create a subdirectory in your home directory (~) called library. ______[2]

5.  Move all .class files in your current directory to this subdirectory library ______[2]

6.  Change your current working directory to library directory ______[1]

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

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

9.  Print your typescript file, 2 pages per sheet. ______[1]

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

11.  Compile the source file test.java ______[2]

12.  Run the Java program first using the filename mydata as a command line argument. ______[2]

13.  Run the Unix program filter redirecting the file data for input and sorts output stream. Just displays results. ______[3]

14.  What key combination designates end of input in Unix? ____
What key combination cancels a program in Unix? ____
What characters designate the parent directory? _____
What character designates the top level directory? _____ [4]

15.  Give the alphabetic or punctuation character(s) in vi to perform the following actions

[8 pts]

_____ move the cursor right 1 character (1 character)

_____redo the previous action (1 char)

_____ start insertion mode (1 char)

_____ end insert mode (1non standard character)

_____ delete the current character (1 char)

_____ delete the current word (2 chars)

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

_____exit and save the file (2 chars)

16.  Arrays.

[12 pts]

a. Give a Java code segment to define an integer array nums of size elements and use a for loop to initialize each element to the value size-index (element 0 contains size, the last element contains 1, etc.). [6]

b. Give a Java code segment to make a full cloned copy of the array nums called numsCopy. [6]

17.  Object-oriented programming design principles. (True/False)

[8 pts]

a.  ____ An Abstract Data Type encapsulates a data structure organization and its operations.

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

c.  ____ An abstract class can be used to define shared methods and instance variables inheritable by related subclasses.

d.  ____ An interface defines methods that must be implemented by a class that then may be used by other classes.

e.  ____ ADT specifications depend on the implementation language, e.g. Java.

f.  ____ The information hiding principle prevents the user from seeing the implementation but the implementer knows how his/her code will be used.

g.  ____ A pre-condition specifies what must be true about the inputs at the end of a method.

18.  Miscellaneous Java concepts (True/False)

[10 pts]

a. _____ Each Java class should be defined in its own separate file with the extension .java but the file name can be anything.

b _____ The contents of the .class file are called bytecodes, which are independent of a particular machine’s architecture.

c. _____ Garbage collection in Java is the automatic recovery of memory from inaccessible objects.

d. _____ The toString method that should be defined for each class is an example of an accessor.

e. _____ The toString method is an example of a method that is inherited but can be overridden.

f. _____ Two strings compared by the compareTo() results in a Boolean value.

g. _____ An iterator ensures that all elements of a collection can be accessed in some order.

h. _____ The class that extends an existing class is called the subclass.

i. ______The types of int and double are not true classes for performance purposes.

j. ______Reusability is a quality software goal in object-oriented class design.

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

[6 pts]

_____ Design _____ Requirements identification

_____ Testing _____ Maintenance

_____ Analysis _____ Implementation

20.  Rank the algorithm complexities from "fastest" to "slowest" (increasing dominance). 1 is fastest (good, low cost) and 6 is slowest (bad, high cost).

[10 pts]

O(n2) / O(n) / O(2n) / O(n log n) / O(1) / O(log n)

21.  For each of the cost functions below give the “best” big-O equivalence where n is the data set size and k is some constant not related to the size of data.

[8 pts]

______f(n) = 5n2+ kn + 7

______f(n) = n3 + 900n2 + 50k + 100

______f(n) = kn2 + 4k4

______f(n) = 8n4 +4n

22.  Estimate the running times using the big-O notation for each of these code segments. The size of the data set is n. Assume all variables are integer.

[10 pts]


______/ for(j=0; j<n; j++){
// 3 assignments
}
______/ for (i=1; i<=n; i++)
for(j=1; j<=100; j++)
for(k=i; k<=n; k++){
// 4 assignment and 2 i/o statements
}

______/ i = n;
while(i>0){
if(i % 2 == 1){ // if i is odd
for(j=i; j<=n; j++){
foo(j,n); //foo executes in linear time
}
// 3 assignment statements
i = i-3;
}

______/ for (k=n; k>0; k--){
j=0;
while (j<10){
bar(j,n); //bar executes in quadratic time
j=j+1;
}
}

______/ i=0;
while(i<n){
j = 1;
while(j<n){
//3 assignments
j=j*2;
}
i++;
}