Name:______
Covers Chapters 7-9
50 min (Practice Questions) / CSCI 1302 OO Programming
ArmstrongAtlanticStateUniversity
Instructor: Y. Daniel Liang

(50 minutes)

PartI.(2 pts each)

1. Add the static keyword in the place of ? if appropriate.

public class Test {

private int count;

public?void main(String[] args) {

...

}

public?int getCount() {

return count;

}

public ?int factorial(int n) {

int result = 1;

for (int i = 1; i <= n; i++)

result *= i;

return result;

}

}

2. In the following code, radius is private in the Circle class, and myCircle is an object of the Circle class. Does the following highlighted code cause any problems? Explain why.

public class Circle {

private double radius = 1.0;

/** Find the area of this circle */

double getArea() {

return radius * radius * Math.PI;

}

public static void main(String[] args) {

Circle myCircle = new Circle();

System.out.println("Radius is " + myCircle.radius);

}

}

3. What is wrong in the following code?

public class Test {

public static void main(String[] args) {

java.util.Date[] dates = new java.util.Date[10];

System.out.println(dates[0]);

System.out.println(dates[0].toString());

}

}

4. Describe the role of the this keyword. What is wrong in the following code?

public class C {

int p;

public C() {

System.out.println("C’s no-arg constructor invoked");

this(0);

}

public C(int p) {

p = p;

}

public void setP(int p) {

p = p;

}

}

5. Show the output of the following program when invoked using

java Test I have a dream

public class Test {

public static void main(String[] args) {

System.out.println("Number of strings is " + args.length);

for (int i = 0; i < args.length; i++)

System.out.println(args[i]);

}

}

6. What will happen if you attempt to create a Scanner for a nonexistent file? What will happen if you attempt to create a PrintWriter for an existing file?

Part II:

a. (3 pts) Suppose that the Loan class is given as shown in the following UML. Write a test program that creates a Loan object with loan amount $40000, annual interest rate 5.5%, and number of years 15, and displays the monthly payment and total payment.

  1. (5 pts) Write a program that reads data from a text file. In the text file, each line contains four values (firstname, mi, lastname, and score). Names are strings and score is a double value. Display data to the console.

c. (15 pts) (Geometry: n-sided regular polygon) An n-sided regular polygon has n sides of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains:

  • A private int data field named n that defines the number of sides in the polygon with default value 3.
  • A private double data field named side that stores the length of the side with default value 1.
  • A private double data field named x that defines the x-coordinate of the center of the polygon with default value 0.
  • A private double data field named y that defines the y-coordinate of the center of the polygon with default value 0.
  • A no-arg constructor that creates a regular polygon with default values.
  • A constructor that creates a regular polygon with the specified number of sides and the length of side, and centered at (0, 0).
  • A constructor that creates a regular polygon with the specified number of sides, the length of side, and x-and y-coordinates.
  • The accessor and mutator methods for all data fields.
  • The method getPerimter() that returns the perimeter of the polygon.
  • The method getArea() that returns the area of the polygon. The formula for computing the area of a regular polygon is.

Draw the UML diagram for the class. Implement the class. Write a test program that creates three RegularPolygon objects, created using the no-arg constructor, using RegularPolygon(6, 4), and using RegularPolygon(10, 4, 5.6, 7.8). For each object, display its perimeter and area.

Part III: Multiple Choice Questions: (1 pts each)

7 quizzes for Chapter 7

1 What code may be filled in the blank without causing syntax or runtime errors:
public class Test {
java.util.Date date;
public static void main(String[] args) {
Test test = new Test();
System.out.println(______);
}
}

A. test.date.toString()

B. date.toString()

C. test.date

D. date

2 When invoking a method with an object argument, ______is passed.

A. the object is copied, then the reference of the copied object

B. a copy of the object

C. the contents of the object

D. the reference of the object

3 Analyze the following code.
public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);
System.out.println("n is " + n);
}
void xMethod(int n) {
n++;
}
}

A. The code has a syntax error because xMethod does not return a value.

B. The code prints n is 3.

C. The code has a syntax error because xMethod is not declared static.

D. The code prints n is 2.

E. The code prints n is 1.

4 How many JFrame objects can you create and how many can you display?

A. one

B. three

C. unlimited

D. two

5 Array variable are reference variables.

A. true

B. false

6 You can declare variables of the same name in a method if they are in non-nesting blocks.

A. false

B. true

7 The order of methods in a class is immaterial.

A. false

B. true

8 quizzes for Chapter 8

8 Which of the following is the correct header of the main method?

A. public static void main(String x[])

B. static void main(String[] args)

C. public static void main(String[] args)

D. public static void main(String[] x)

E. public static void main(String args[])

9 "AbA".compareToIgnoreCase("abC") returns ______.

A. 1

B. -1

C. 0

D. 2

E. -2

10 The following program displays ______.
public class Test {
public static void main(String[] args) {
String s = "Java";
StringBuffer buffer = new StringBuffer(s);
change(buffer);
System.out.println(buffer);
}
private static void change(StringBuffer buffer) {
buffer.append(" and HTML");
}
}

A. Java

B. and HTML

C. nothing is displayed

D. Java and HTML

11 Suppose Character x = new Character('a'), ______returns true.

A. x.equals('a')

B. x.equals("a")

C. x.equalsIgnoreCase('A')

D. x.equals(new Character('a'))

E. x.compareToIgnoreCase('A')

12 Which of the following is the correct statement to return a string from an array a of characters?

A. String.toString(a)

B. new String(a)

C. toString(a)

D. convertToString(a)

13 "unhappy".substring(2) returns "happy".

A. false

B. true

14 "ab".compareTo("aB") is ______.

A. equal to 0

B. less than 0

C. less than or equal to 0

D. greater than 0

15 You cannot append a string to a string buffer if the resulting new string exceeds the capacity.

A. true

B. false

16 Which of the following statements are true about an immutable object?

A. An object type property in an immutable object must also be immutable.

B. An immutable object contains no mutator methods.

C. All properties of an immutable object must be private.

D. All properties of an immutable object must be of primitive types.

E. The contents of an immutable object cannot be modified.

17 Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}

A. The program has a compilation error because you cannot assign radius to radius.

B. The program does not compile because Circle does not have a default constructor.

C. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

D. The program has a compilation error because it does not have a main method.

18 Java uses ______to reference the current object.

A. null

B. this

C. that

D. thisObject

19 A constructor can access ______.

A. A public instance variable

B. A local variable defined in any method

C. A private instance variable

D. A static variable

Keys:

1. C
2. D
3. C
4. C
5. A
6. B
7. B
8. ACDE
9. E
10. D
11. AD
12. B
13. B
14. D
15. B
16. ABCE
17. C
18. B
19. AC

1