Beruflich Dokumente
Kultur Dokumente
Student ID # _________________________________
Directions: You have 80 minutes to complete this exam. Write all answers neatly in the space provided.
Please do not separate exam sheets. No additional exam sheets are provided. Scrap paper is provided per
request.
Closed book exam: you are not allowed to use the textbook, lecture notes, cheat sheets or anything else beside
your brain during the exam.
Use well-named variables and include any documentation where you feel it is necessary to understand your
algorithms.
A. true true
B. true false
C. false true
D. false false
class Test {
public static void main(String[] args) {
StringBuilder strBuf = new StringBuilder(4);
strBuf.append("ABCDE");
System.out.println("What's strBuf.charAt(5)? " +
(strBuf.length()>5&&0<=5?strBuf.charAt(5):””));
}
}
A. The program has a compilation error because you cannot specify initial capacity in the StringBuilder
constructor.
B. The program compiles and runs fine.
C. The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are
appended into the buffer.
D. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is
appended into the buffer. Therefore, strBuf.charAt(5) is out of range.
D
The last character is at index 4: strBuf.charAt(4).
A. The program has a compile error because you cannot create an object from the class that defines the object.
B. The program has a compile error because Test does not have a default constructor.
C. The program has a runtime NullPointerException because test is null while executing test.x.
D. The program has a compile error because x has not been initialized.
E. The program has a compile error because test is not initialized.
A, B and C
class Test {
private double i;
public Test(double i) {
this.t();
this.i = i;
}
public Test() {
System.out.println("Default constructor");
this(1);
}
public void t() {
System.out.println("Invoking t");
}
}
A and D
B, C, D and E
class Test {
private double i;
public Test(double i) {
t();
this.i = i;
}
public Test() {
this(1);
System.out.println("Default constructor");
}
public void t() {
System.out.println("Invoking t");
}
}
A. x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement
inside a statement.
B. A java.util.Date object is not cloneable.
C. x = y in System.out.println(x = y) causes a compile error because you cannot have an assignment statement
inside a statement.
D. The program has a compile error because the return type of the clone() method is java.lang.Object.
a. (10 points) Draw the UML diagram that involves the classes Triangle and GeometricObject.
GeometricObject
-color: String
-filled: boolean
-dateCreated: java.util.Date
+GeometricObject()
+GeometricObject(color: String,
filled: boolean)
+getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
+getDateCreated(): java.util.Date
+toString(): String
+getArea(): double
+getPerimeter(): double
Triangle
-side1: double
-side2: double
-side3: double
+Triangle()
+Triangle(side1: double, side2:
double, side3: double)
+getSide1(): double
+getSide2(): double
+getSide3(): double
+setSide1(side: double): void
+setSide2(side: double): void
+setSide3(side: double): void
/** Constructor */
public Triangle() { }
/** Constructor */
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
System.out.println(triangle);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println(triangle);
}
}