1
Name:_______________________
Covers Chapters 9-11
75 min
CSCI 1302 OO Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Part I.
1. (1 pts) What is wrong in the following code?
class Test {
public static void main(String[] args) {
A a = new A();
a.print();
}
}
A: Class A does not have a no-arg constructor. So you cannot use new A().
2. (1 pts) What is the printout of the following code?
public class Foo {
private boolean x;
A: false. (By default, a boolean data field has a value false)
3. (2 pts) Show the output of the following program:
2
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);
public class Count {
private int count;
Count(int c) {
count = c;
}
A:
count is 101
times is 0
4.
5.
(C) (1) false
(2) true
Part II:
class Point2D {
private double x;
private double y;
3
public static double distance(Point2D p1, Point2D p2) {
// Write code to implement it
return Math.sqrt((p1.xp2.x) * (p1.xp2.x) + (p1.yp2.y)
* (p1.yp2.y));
}
Point2D p1 = new Point2D(4, 5);
Point2D p2 = new Point2D(3.5, 5.6);
b.
public static Double max(ArrayList<Double> list) {
if (list == null || list.size() == 0)
return null;
c.
4
public class Clock {
private int hours;
private boolean isTicking;
private Integer diff;
public void decrement() {
if (hours > 0) hours–;
}
}
5
Part III:
1. Analyze the following code:
public class Test {
private int t;
public static void main(String[] args) {
int x;
System.out.println(t);
}
}
A. The program compiles and runs fine.
B. The variable t is private and therefore cannot be accessed in the main method.
C. The variable x is not initialized and therefore causes errors.
D. t is non-static and it cannot be referenced in a static context in the main method.
E. The variable t is not initialized and therefore causes errors.
#
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;
public Test(String t) {
System.out.println(“Test”);
}
6
public static void main(String[] args) {
Test test = null;
System.out.println(test.x);
}
}
A. The program has a compile error because x has not been initialized.
B. The program has a compile error because test is not initialized.
C. The program has a runtime NullPointerException because test is null while executing
test.x.
D. The program has a compile error because you cannot create an object from the class
that defines the object.
E. The program has a compile error because Test does not have a default constructor.
#
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?
7
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 ___________.
A. 0
B. 2
C. -2
D. -1
E. 1
#
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;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println(“i + j is ” + i + j);
}
8
k = i + j;
System.out.println(“k is ” + k);
System.out.println(“j is ” + j);
}
}
A. j is 3
B. j is 2
C. j is 1
D. j is 0
#
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
object.
c. x contains an array of ten objects of the Circle type.
d. x contains a reference to an array and each element in the array can hold a reference to
a Circle object.
#
13. What is the value of myCount.count displayed?
public class Test {
public static void main(String[] args) {
9
Count myCount = new Count();
int times = 0;
for (int i=0; i&lt;100; i++)
increment(myCount, times);
System.out.println(
“myCount.count = ” + myCount.count);
System.out.println(“times = “+ times);
}
public static void increment(Count c, int times) {
c.count++;
times++;
}
}
class Count {
int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
a. 99
b. 101
c. 100
d. 98
#
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);
m1(date);
System.out.print(date.getTime() + ” “);
m2(date);
System.out.println(date.getTime());
}
public static void m1(Date date) {
date = new Date(7654321);
}
public static void m2(Date date) {
date.setTime(7654321);
}
}
a. 7654321 1234567
b. 7654321 7654321
c. 1234567 1234567
#
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()};
System.out.print(o[0]);
System.out.print(o[1]);
}
}
class A extends B {
public String toString() {
return “A”;
}
}
class B {
public String toString() {
return “B”;
}
}
a. AB
b. BA
c. AA
d. BB
e. None of above
#
17. The method _____ overrides the following method:
protected double xMethod(int x) {…};
a. private double xMethod(int x) {…}
b. protected int xMethod(double x) {…}
c. public double xMethod(double x) {…}
d. public double xMethod(int x) {…}