| CIT591 Midterm Exam Fall, 2004 |
Name_________________________________________ |
Please keep all your answers short and to the point. Do not provide extra information that was not asked for.
|
|
String s = "^(^12^3^,-4^56^^)".replaceAll("^", ""); "(123,-456)"
int i = s.indexOf(","); 4
String first = s.substring(1, i); "123"
int number1 = new Integer(first).intValue(); 123
String second = s.substring(i + 1, s.length() - 2); "-45"
int number2 = new Integer(second).intValue(); -45
|
// Declare the class
class Question4 {
// Write the header for the main method
public static void main(String[] args) {
// Declare an integer variable "number"
int number;
// Set the number to a value gotten from the command line (args[0])
number = new Integer(args[0]).intValue();
// Begin a loop whose test is at the bottom
do {
// Print the current value of "number"
System.out.println(number);
// If the number is even...
if (number % 2 == 0) {
// Divide it by 2
number = number / 2;
// But if the number is odd...
} else {
// Multiply it by 3 and subtract 1
number = 3 * number - 1;
}
// Exit the loop if the number is 1
} while (number != 1);
// End the method and the program
}
}
|
private Fiddle(int x) {
super(x + 1);
. . . }39FF
? 3A00
|
|
int n = Foo.bar().length();
in a program. Classify each of the following statements as true (T),
probably true (P),
unlikely (U),
or false (F):
bar() is a static method of Foo.bar() is defined in class Foo.Foo.bar() returns a String as a result.Foo.bar(x).length() returns an int
result.protected classes are accessible from
more places than "package" classes.m is an int[], then
m instanceof Object is true. .java file may contain only one class. .java file may contain only one public
class.private constructor can never be used.private class can never be used for
anything.