Name:_______________________



Name:_______________________

Covers Chapters 8-10

50 min |CSCI 1302 OO Programming

Armstrong Atlantic State University

Instructor: Y. Daniel Liang | |

Part I. (2 pts each)

1. What is wrong in the following code?

class Test {

public static void main(String[] args) {

A a = new A();

a.print();

}

}

class A {

String s;

A(String s) {

this.s = s;

}

public void print() {

System.out.print(s);

}

}

2. What is the printout of the following code?

public class Foo {

private boolean x;

public static void main(String[] args) {

Foo foo = new Foo();

System.out.println(foo.x);

}

}

3. Show the output of the following program:

[pic]

4. 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]);

}

}

Part II:

a. (15) (Geometry: The MyRectangle2D class) Define the MyRectangle2D class that contains:

• Two double data fields named x and y that specify the center of the rectangle with get and set methods. (Assume that the rectangle sides are parallel to x- or y- axes.)

• The double data fields width and height with get and set methods.

• A no-arg constructor that creates a default rectangle with (0, 0) for (x, y) and 1 for both width and height.

• A constructor that creates a rectangle with the specified x, y, width, and height.

• A method getArea() that returns the area of the rectangle.

• A method getPerimeter() that returns the perimeter of the rectangle.

• A method contains(double x, double y) that returns true if the specified point (x, y) is inside this rectangle. See Figure 1(a).

• A method contains(MyRectangle2D r) that returns true if the specified rectangle is inside this rectangle. See Figure 1(b).

• A method overlaps(MyRectangle2D r) that returns true if the specified rectangle overlaps with this rectangle. See Figure 1(c).

[pic] [pic] [pic]

(a) (b) (c)

Figure 1

(a) A point is inside the rectangle. (b) A rectangle is inside another rectangle. (c) A rectangle overlaps another rectangle.

Draw the UML diagram for the class.

Implement the method getArea(), getPerimeter(), contains(double x, double y), contains(MyRectangle2D r), and overlaps(MyRectangle2D r).

This page is left blank intentionally.

2. (10 pts) (Occurrences of a specified character) Write a method that finds the number of occurrences of a specified character in the string using the following header:

public static int count(String str, char a)

For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character and displays the number of occurrences of the character in the string.

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

(Take the multiple-choice questions online from LiveLab. Log in and click Take Instructor Assigned Quiz. 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);

   }

}

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.

Key:D

#

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

Key:D

#

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.

Key:C

#

4.  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 = 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.

Key:C

#

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

Key:C

#

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.

Key:C

#

7.  _________ returns the last character in a StringBuffer variable named strBuf?

A. strBuf.charAt(strBuf.capacity() - 1)

B. StringBuffer.charAt(strBuf.capacity() - 1)

C. StringBuffer.charAt(strBuf.length() - 1)

D. strBuf.charAt(strBuf.length() - 1)

Key:D

#

8.  "AbA".compareToIgnoreCase("abC") returns ___________.

A. 0

B. 2

C. -2

D. -1

E. 1

Key:C

#

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.

Key:B

#

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

     }

     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

Key:D

#

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.

Key:B

#

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.

Key:d

#

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

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

Key:b

#

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

d. 1234567 7654321

Key:d

#

15. 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 has compile errors because the variable radius is not initialized.

b. The program compiles and runs fine.

c. The program has no compile errors but will get a runtime error because radius is not initialized.

d. The program has a compile error because a constant PI is defined inside a method.

Key:a

[pic]

class MyRectangle2D {

private double x, y; // Center of the rectangle

private double width, height;

public MyRectangle2D() {

x = y = 0;

width = height = 1;

}

public MyRectangle2D(double x, double y, double width, double height)

{

this.x = x;

this.y = y;

this.width = width;

this.height = height;

}

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double getPerimeter() {

return 2 * (width + height);

}

public double getArea() {

return width * height;

}

public boolean contains(double x, double y) {

return Math.abs(x - this.x) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download