1
Name:_______________________
Covers Chapters 9-11
75 minutes
CSCI 1302 OO Programming
Armstrong Atlantic State University
Instructor: Dr. Y. Daniel Liang
I pledge by honor that I will not discuss this exam with anyone until my
instructor reviews the exam in the class.
Signed by ___________________ Date ___________________
Part I.
1. (1 pts) What is wrong in the following code?
2. (1 pts) What is the output of the following code?
public class Foo {
private boolean x;
3. (2 pts) Show the output of the following program:
2
public class Test {
public class Count {
4. (2 pts)
Show the output of following program:
public class Test {
5. (2 pts) The java.util.Date class overrides the equals
method to return true if two objects have the same date and
time. Show the output of the following code.
import java.util.*;
3
Part II:
a. (4 pts) The Point2D class is defined as follows:
Point2D
+Point2D(x: double, y: double)
Constructs a Point2D object with the specified x- and y-coordinates.
The class is partially implemented below. Implement distance(Point2D
p1, Point2D p2) and midpoint(Point2D p):
class Point2D {
}
public Point2D midpoint(Point2D p) {
// Write code to implement it
4
public double getY() {
return y;
}
}
b. (6 pts) Write the following method that returns the maximum value in
an array list of double values. The method returns null if the
list is null or the list size is 0.
5
c. (13) Write a class named Clock that meets the following
requirements:
The class has three instance variables: One of type int
called hours, another of type boolean called isTicking, and
Write a test program that creates a Clock with hour 10,
isTicking false, and diff 5 and then invokes the increment
method once and then the decrement method once.
6
Name ____________________________
Part III: Multiple Choice Questions: (1 pts each)
(Take the multiplechoice questions online from LiveLab.
Log in and click Take Instructor Assigned Quiz. Choose
Quiz1. You have to take it before it is due. You have to
submit within the specified time interval.)
1. Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
#
2. ________ is invoked to create an object.
A. The main method
B. A method with a return type
C. A method with the void return type
D. A constructor
#
3. Given the declaration Circle x = new Circle(), which of the following statement is most
accurate.
A. x contains an object of the Circle type.
B. You can assign an int value to x.
C. x contains a reference to a Circle object.
D. x contains an int value.
#
4. Analyze the following code.
public class Test {
int x;
7
public Test(String t) {
System.out.println(“Test”);
}
public static void main(String[] args) {
Test test = null;
System.out.println(test.x);
}
}
#
5. When invoking a method with an object argument, ___________ is passed.
A. a copy of the object
B. the object is copied, then the reference of the copied object
C. the reference of the object
D. the contents of the object
#
6. Analyze the following code:
public class Test {
public static void main(String[] args) {
double radius;
final double PI= 3.15169;
double area = radius * radius * PI;
System.out.println(“Area is ” + area);
}
}
A. The program compiles and runs fine.
B. The program has no compile errors but will get a runtime error because radius is not
initialized.
C. The program has compile errors because the variable radius is not initialized.
D. The program has a compile error because a constant PI is defined inside a method.
#
7. _________ returns the last character in a StringBuffer variable named strBuf?
8
A. strBuf.charAt(strBuf.capacity() – 1)
B. StringBuffer.charAt(strBuf.capacity() – 1)
C. StringBuffer.charAt(strBuf.length() – 1)
D. strBuf.charAt(strBuf.length() – 1)
#
8. “AbA”.compareToIgnoreCase(“abC”) returns ___________.
#
9. Analyze the following code.
class Test {
public static void main(String[] args) {
String s;
System.out.println(“s is ” + s);
}
}
A. The program has a runtime error because s is not initialized, but it is referenced in the
println statement.
B. The program has a compile error because s is not initialized, but it is referenced in the
println statement.
C. The program has a runtime error because s is null in the println statement.
D. The program compiles and runs fine.
#
10. What is the printout for the third print statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
9
System.out.println(“j is ” + j);
}
}
#
11. Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}
A. The program has a compile error because you cannot assign radius to radius.
B. The program will compile, but you cannot create an object of Circle with a specified
radius. The object will always have radius 0.
C. The program does not compile because Circle does not have a default constructor.
D. The program has a compilation error because it does not have a main method.
#
12. Given the declaration Circle[] x = new Circle[10], which of the following statement
is most accurate.
a. x contains an array of ten int values.
b. x contains a reference to an array and each element in the array can hold a Circle
#
13. What is the value of myCount.count displayed?
public class Test {
public static void main(String[] args) {
Count myCount = new Count();
int times = 0;
for (int i=0; i < 100; i++)
increment(myCount, times);
10
System.out.println(
“myCount.count = ” + myCount.count);
System.out.println(“times = “+ times);
}
}
}
class Count {
int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
#
14. What is the output of the following program?
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date(1234567);
System.out.println(date.getTime());
}
public static void m1(Date date) {
date = new Date(7654321);
}
11
public static void m2(Date date) {
date.setTime(7654321);
}
}
#
15. A subclass inherits _____________ from its superclass.
a. private method
b. protected method
c. public method
d. a and c
e. b and c
#
16. What is the output of running the class C.
public class C {
public static void main(String[] args) {
Object[] o = {new A(), new B()};
}
}
class A extends B {
public String toString() {
return “A”;
}
}
class B {
public String toString() {
return “B”;
}
}
#
17. The method _____ overrides the following method:
protected double xMethod(int x) {…};
12
a. private double xMethod(int x) {…}
b. protected int xMethod(double x) {…}
c. public double xMethod(double x) {…}
d. public double xMethod(int x) {…}
Have you submitted your answer to LiveLib? ______________
# of the corrected answers? ______________