Download Computer Science II

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 240 Exam 1
Page 1
Computer Science II
Spring 2010, Wednesday, 2/10/10
100 points
Name _____________________________
2/10/10
1. Explain or give the UNIX or vi commands/keystrokes to do the following tasks. A single command should suffice.
[28 pts]
a. Display all filenames and information including their
modification dates, sizes and protections that are in your current directory. ______________________[2]
b. Display only the .java filenames found in your
home directory, assuming it’s not the working directory _______________________________ [2]
c. Delete all .class files in your current directory ____________________[2]
d. Create a subdirectory in your current directory called projArray. ___________________________[2]
e. Move all .class files in your current directory to this subdirectory projArray
________________________[2]
f.
Change your current working directory to its parent directory ________________________[2]
g. Display only the last 10 lines of the file log . ______________________[2]
h. Begin capturing the screen display to a file for printing later. ____________________ [1]
i.
Print your typescript file, 2 pages per sheet. ___________________________________ [2]
j.
Start the visual editor on file exam.java ______________________[2]
k. Compile the source file exam.java _______________________________[2]
l.
Run the Java program exam using two input strings mydata and yourdata as a command line arguments.
__________________________________________________________ [2]
m. True/false on Unix commands [5]
_______The ctrl-Z key combination designates end of input in Unix.
_______The ctrl-S key combination stops and cancels a program in Unix.
_______The command options prefixed with - can be entered in any order.
_______In the command wc aesop.txt > words the file aesop.txt captures the output of the wc command.
_______All directories, disk drives, file storage structures are unified into one directory tree.
2. Give the alphabetic or punctuation character(s) in vi to perform the following actions
[7 pts]
a. _____ move the cursor down 1 character (1 character)
b. _____redo the previous action (1 char)
c. _____ delete the current character (1 char)
d. _____ start insertion mode (1 char)
e. _____ end insert mode (1non standard character)
f.
_____ delete the current word (2 chars)
g. _____ exit and save the file or its changes (2 chars)
CS 240 Exam 1
2/10/10
Page 2
3. Arrays.
[12 pts]
a. Give a Java code segment to 1) define an integer array sizes with args.length elements
and 2) use a for loop to initialize each element of sizes to the lengths of the strings in the args array
(the command line arguments array). [6]
b. Give a Java code segment to determine and print the position of the longest string. [6]
4. Object-oriented programming design principles. (True/False)
[8 pts]
a. ____ An Abstract Data Type exposes its data structure organization and hides operations.
b. ____ Reusability is a quality software goal in object-oriented class design.
c. ____ An abstract class can be used to define shared methods and instance variables inheritable by related
subclasses.
d. ____ An interface defines abstract 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 it will be used in all cases.
g. ____ A pre-condition specifies what must be true about the inputs at the end of a method.
CS 240 Exam 1
2/10/10
Page 3
5. Miscellaneous Java concepts (True/False)
[10 pts]
a. _____ Each Java class should be defined in its own separate file with the extension .java and the file name
must match the class name.
b. _____ The contents of the .class file called bytecodes can be executed on any machine that has a Java
Virtual Machine.
c. _____ The class that extends an existing class is called a superclass.
d. _____ All Java classes ultimately inherit from the Object class.
e. _____ Garbage collection in Java is the automatic recovery of memory from objects not used recently.
f.
_____ The types of int and double are not true classes for performance purposes.
g. _____ Two strings are compared by the <, <=, ==, =>, > and != operations.
h. _____ The toString method must be defined uniquely for each class.
i.
_____ An iterator provides a method so that all elements of a collection can be accessed in some order.
j.
_____ The equals method is an example of a method that is inherited but can be overridden.
6. Number the steps of the software development cycle (Waterfall method) from 1 to 6 in correct order.
[5 pts]
_____ Analysis
_____ Implementation
_____ Testing
_____ Maintenance
_____ Requirements identification
_____ Design
7. 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(n log n)
O(n)
O(n2)
O(log n)
O(1)
O(2n)
8. 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. Best means the smallest dominance function, so O(n!) is not an
acceptable answer.
[8 pts]
_______ f(n) = 7n2+ 4n + 2000
_______ f(n) = n3 + 95n2 + 500n + 100
_______ f(n) = 8n2 + 16n log n
_______ f(n) = n8 +4n
CS 240 Exam 1
2/10/10
9.
Page 4
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.
[12 pts]
____________
____________
____________
____________
____________
____________
for(j=1; j<=n; j++){
sum += j;
}
for (i=0; i<n; i++)
for(j=0; j<100; j++)
for(k=j; k<100; k++){
// 4 assignments
// 2 square root function calls
}
for (i=0; i<n; i++)
for(j=0; j<n; j++)
for(k=j; k<n; k++){
// 3 assignments
}
i = n;
while(i>0){
for(j=i; j<=n; j=j+2){
foo(j,n); //foo executes in linear time
}
// 3 assignment statements
i = i-1;
}
for (k=n; k>0; k--){
j=0;
while (j<10){
bar(j,n); //bar executes in log n time
j=j+1;
}
}
i=0;
while(i<n){
j = 1;
while(j<n){
//3 assignments
j=j*2;
}
i++;
}