COSC4301
Test-1
6/26/2012
Name (Please PRINT IN CAP):________________________________________
Part-A: Multiple Choice Questions:
(36 points)
1. Which class is in java.lang?
a. System and Math
b. String and Integer
c. Exception and Float
d. All of above. e. two of above.
2. Which class is in java.util?
a. Random and Scanner
b. ArrayList and StringTokenizer
c. Both. d. neither.
3. Which is true of class ArrayList
a. a generic class
b. has more static methods than otherwise
c. both. d. neither.
4. Which is a primitive type?
a. int and char
b. float and double
c. String and Integer
d. All of above. e. two of above.
5. Which is a static method?
a. System.in and s1.equals(s2)
b. System.exit(0)
c. Math.random()
d. All of above. e. two of above.
6. “Good Day”.compareTo (“Beautiful Day”)
Above returns
a. A positive number
b. A negative number
c. boolean false d. none of above.
7. Math.random()
Above can return
a. 0.9
b. 0.0001111
c. 2.2
d. All of above. e. two of above.
8. Which can be private?
a. An inner Class
b. A class that is not an inner class.
c. Instance variables
d. Static methods. e. all of above. f. three of above.
e. Two of above.
9. “SMILES”.substring (2,5)
Above will return
a. “MILE”
b. “MIL”
c. “ILE”
d. “ILES” e. none of above.
10. Which should not have a return type?
a. Static methods
b. Non-static methods
c. Class constructors
d. All of above. e. two of above. f. none of above.
11. Which can be a return type
a. A primitive type such as int or float
b. A class name
c. void d. all of above. e. two of above.
12. Which Unix command will copy all the contents (all inside files and subdirectories to any depth and all files inside any subdirectory) of the current directory into one of its own subdirectories?
a. $ cp –r . subDirec
b. $ cp –r . ../subDirec
c. $ cp . subDirec d. two of above. e. none of above.
Part-B: Other Problems:
(64 points)
Choose and Do five problems:
A. ArrayList <String> toDoList = new ArrayList <String> ();
toDoList.add (“FirstLine”);
toDoList.add (“SecondLine”);
toDoList.add (“ThirdLine”);
toDoList.add (“ForthLine”);
toDoList.add (1, “NewLineAddedAtIndex1”);
toDoList.add (2, “AnotherLineAtIndex2”);
toDoList.set (4, “BeautifulDay”);
String sss = toDoList.remove (2);
System.out.println (“\n REMAINING ITEMS IN THE TODOLIST:::\n”);
for (String moreitem: toDoList) // for-each loop
System.out.println (moreitem);
Give the outputs of the above program segment.
B. Consider the class NodeList of program-4
Public NodeList reverseRecursive (Node
firstnode)
Complete the above method that will (1) reverse the calling object (which must be an instance of this
class NodeList) recursively and (2) return the resulting linked list.
C. Explain this parameter.
D. Write a main() method that does the following assuming that all needed packages are imported:
1. read all input lines from an input file given as the first argument in the command.
2. retrieve all words separated by whitespaces from all input lines taken and store them into an
array of Strings.
3. and, sort these words in alphabetically ascending order and resulting sorted words will be stored back
into the initial array.
E.
Consider the class IntegerArray of Program-2.
Design
and code the following two methods whose headers will be,
respectively:
public
int median() // finds and returns the median of array sorted
instead of array original.
public
float average() // calculates and returns the average of array original.
F.
The (generic) Class ArrayList is defined and included in the java.util
package.
Briefly
compare this class to arrays and also list some useful methods of this
class.
.
H. Describe the effects of each of the following UNIX Commands:
1. $ ls -l *.java
2. $ javac myFirst.java
3. $ chmod 700 *.java
4. $ cp -r dirA dirB
5. $ mv fileA fileB
6. $ rm fileA
7. $ vi fileA
8. $ script myfile.spt