Sie sind auf Seite 1von 11

CSE 114 – Sample Midterm Exam 2 Version 1 Solution

Name: First Last

Student ID # _________________________________

Question # Points Points Earned


Available
1 10
2 10
3 10
4 10
5 10
6 10
7 15
8 25
Total: 100

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.

You must write with a pen.

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.

CSE 114 – Midterm Exam 2 Version 1 1


Question 1 (10 Points)
a. (5 points) The code below compiles and executes without error. What output results from running it?

System.out.print( "Hi, ABC, good".matches("ABC ") );

System.out.print( " " );

System.out.println( "Hi, ABC, good".matches(".*ABC.*") );

A. true true
B. true false
C. false true
D. false false

b. (5 points) Analyze the following code.

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).

CSE 114 – Midterm Exam 2 Version 1 2


Question 2 (10 Points)
a. (5 points) Analyze the following code.

public class Test {


int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
Test test = new Test(“a”);
System.out.println(test.x);
}
}

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.

b. (4 points) Which of the following statements are true? Multiple choices:

A. Encapsulating data fields helps prevent programming errors.


B. Encapsulating data fields makes the program easy to maintain.
C. Use the private modifier to encapsulate data fields.
D. Encapsulating data fields makes the program short.

A, B and C

c. (1 point) To declare a constant MAX_LENGTH as a member of the class, you write

A. final static MAX_LENGTH = 99.98; // what is the type of the constant?


B. final static double MAX_LENGTH = 99.98; // Double is the default type for primitives with a decimal point
C. final static float MAX_LENGTH = 99.98; // double is bigger than float, so you need explicit casting to float
D. final double MAX_LENGTH = 99.98; // a constant is a property of the class, so it should be static
E. static double MAX_LENGTH = 99.98; // a constant should be final (otherwise, it could be modified)

CSE 114 – Midterm Exam 2 Version 1 3


Question 3 (10 Points)
a. (5 points) What is the printout for the third print statement in the main method?
public class Foo {
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + (i + j)); // i + j is 5
}
k = i + j;
System.out.println("k is " + k); // k is 2
System.out.println("j is " + j);
}
static int i = 0;
static int j = 0;
}

b. (5 points) Analyze the following code:

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. this.t() may be replaced by t().


B. this.i may be replaced by i.
C. this(1) must be replaced by this(1.0).
D. this(1) must be called before System.out.println("Default constructor").

A and D

CSE 114 – Midterm Exam 2 Version 1 4


Question 4 (10 Points)
a. (5 points) Which of the following statements are true about an immutable object?
A. All properties of an immutable object must be of primitive types.
B. The contents of an immutable object cannot be modified.
C. All properties of an immutable object must be private.
D. An immutable object contains no mutator methods.
E. An object type property in an immutable object must also be immutable.

B, C, D and E

b. (5 points) Analyze the following code:

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. this(1) must be called after System.out.println("Default constructor").


B. t() may be replaced by this.t().
C. this(1) must be replaced by this(1.0).
D. this.i may be replaced by i.

CSE 114 – Midterm Exam 2 Version 1 5


Question 5 (10 Points)
a. (5 points) What is the output of running class C?
class A {
public A() {
System.out.println("The default constructor of A
is invoked");
}
}
class B extends A {
public B() {
// super();
System.out.println("The default constructor of B
is invoked");
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
A. "The default constructor of A is invoked"
"The default constructor of B is invoked"
B. "The default constructor of A is invoked"
C. Nothing displayed
D. "The default constructor of B is invoked"
"The default constructor of A is invoked"
E. "The default constructor of B is invoked"

b. (5 points) Analyze the following code:


public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
}
}
class A {
int x;
public String toString() {
return "A's x is " + x;
}
}
A. When executing System.out.println(a1), the toString() method in the A class is invoked.
B. When executing System.out.println(a1), the toString() method in the Object class is invoked.
C. When executing System.out.println(a2), the toString() method in the Object class is invoked.
D. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by
System.out.println(a1.toString());
A and C

CSE 114 – Midterm Exam 2 Version 1 6


Question 6 (10 Points)
a. (5 points) Analyze the following code.
public class Test {
public static void main(String[] args) {
java.util.Date x = new java.util.Date();
java.util.Date y = x.clone(); // x.clone() has the type Object
System.out.println(x = y);
}
}

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.

b. (5 points) What is the output of running class Test?


public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle9 extends GeometricObject {
/** Default constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
A. BACD
B. CBAE
C. AEDC
D. ABCD
E. BEDC

CSE 114 – Midterm Exam 2 Version 1 7


Question 7 (15 Points)
Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid
password. Suppose the password rule is as follows:
• A password must have at least eight characters.
• A password consists of only letters and digits.
• A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays "valid password" if the rule is followed
or "invalid password" otherwise.

public class Test {


public static void main(String[] args) {
// Prompt the user to enter a password
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a string for password: ");
String s = input.nextLine();
if (isValidPassword(s)) {
System.out.println("Valid password");
} else {
System.out.println("Invalid password");
}
}
/** Check if a string is a valid password */
public static boolean isValidPassword(String s) {
// Check length
if (s.length() < 8)
return false;
// Only letters and digits?
for (int i = 0; i < s.length(); i++) {
if (!Character.isLetter(s.charAt(i)) &&
!Character.isDigit(s.charAt(i)))
return false;
}
// Count the number of digits >= 2
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i)))
count++;
}
if (count >= 2)
return true;
else
return false;
}
}

CSE 114 – Midterm Exam 2 Version 1 8


Question 8 (25 Points)
Design a class named Triangle that extends GeometricObject. The class contains:
• Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of
the triangle.
• A no-arg constructor that creates a default triangle.
• A constructor that creates a triangle with the specified side1, side2, and side3.
• The accessor methods for all three data fields.
• A method named getArea() that returns the area of this triangle.
• A method named getPerimeter() that returns the perimeter of this triangle.
• A method named toString() that returns a string description for the triangle.

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

CSE 114 – Midterm Exam 2 Version 1 9


b. (10 points) Implement the class.
class Triangle extends GeometricObject {
private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

/** Constructor */
public Triangle() { }

/** Constructor */
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

/** Override method getArea in GeometricObject */


public double getArea() {
return 1.0;
}

/** Override method getPerimeter in GeometricObject */


public double getPerimeter() {
return side1 + side2 + side3;
}

/** Override the toString method */


public String toString() {
// Implement it to return the three sides
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}

CSE 114 – Midterm Exam 2 Version 1 10


c. (5 points) Write a test program that creates a Triangle object with sides 1, 1.5, 1, color yellow and filled true,
and displays the area, perimeter, color, and whether filled or not.
public class Midterm_2_8 {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);

System.out.println(triangle);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println(triangle);
}
}

CSE 114 – Midterm Exam 2 Version 1 11

Das könnte Ihnen auch gefallen