Name:_______________________



Name:_______________________

Covers Chapters 7-9 |CSCI 1302 OO Programming

Armstrong Atlantic State University

Instructor: Y. Daniel Liang | |

(50 minutes)

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

1. Analyze the following code:

public class Test {

public static void main(String args[]) {

Test nc = new Test();

nc.t = nc.t++;

}

int t;

Test() {

}

}

a. The program has a compilation error because t is not initialized.

b. The program does not compile because the parameter list of the main method is wrong.

c. The program compiles, but has a runtime error because t has no initial value.

d. The program has a compilation error because you attempt to create an object of the Test inside the Test class.

e. The program compiles and runs fine.

In the following code, suppose that f is an instance of Foo. Answer Questions 2–3.

class Foo {

int i;

static int s;

void imethod() {

}

static void smethod() {

}

}

2. Which of the following statements is incorrect?

a. System.out.println(f.s);

b. int t = f.imethod();

c. System.out.println(f.i);

d. f.smethod();

3. Which of the following statements is incorrect?

a. Foo.smethod();

b. System.out.println(Foo.s);

c. f.imethod();

d. System.out.println(Foo.i);

4. To restrict access of a data member or a method to the class itself:

a. Use the private modifier.

b. You cannot use the private modifier with the static modifier.

c. Use the static modifier.

d. None of the above.

5. Analyze the following code:

class Test {

public static void main(String[] args) {

double radius = 5;

final static double PI = 3.15169;

double area = radius * radius * PI;

System.out.println("Area is " + area);

}

}

a. The program has syntax errors because the variable radius is not initialized.

b. The program has syntax errors because a static PI is defined inside a method.

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

d. The program compiles and runs fine.

6. Analyze the following code:

class Test {

private int t;

public static void main(String[] args) {

Test test = new Test();

int x;

System.out.println(test.t);

}

}

a. The variable t is not initialized and therefore causes errors.

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. The program compiles and runs fine.

7. Analyze the following code:

class Test {

public static void main(String[] args) {

A a = new A("test");

a.print();

}

}

class A {

String s;

A(String s) {

this.s = s;

}

private void print() {

System.out.println(s);

}

}

a. The program compiles fine, but has a runtime error because the print() method is private.

b. The program has a compilation error because the print() method in class A is private.

c. The program runs fine and prints Test.

d. None of the above.

8. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

a. String s = new String("new string");

b. String s3 = s1 + s2;

c. String s3 = s1.concat(s2);

d. s1 >= s2

e. int i = s1.length();

9. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

a. String s3 = s1 - s2;

b. int i = pareTo(s2);

c. char c = s1[0];

d. char c = s1.charAt(s1.length() - 1);

e. a and c.

10. What is displayed by the following code?

public static void main(String[] args) throws Exception {

String[] tokens = "Welcome to Java".split("o");

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

System.out.print(tokens[i] + " ");

}

}

a. Welcome to Java

b. Welc me to Java

c. Welc me t Java

d. Welcome t Java

11. Which of the following statement is false?

a. The contents of a string can be partially changed.

b. You can add, insert, or delete characters from a string buffer.

c. You can create a string buffer from a string.

d. You can convert a string buffer into a string.

e. All of the above

12. How can you initialize a string with "123"?

a. String[] string = {'1', '2', '3'};

b. String string = {'1', '2', '3'};

c. String s = "123";

d. String s = new String("123");

e. c and d are both fine, but c is better.

13. Analyze the following code.

class Test {

public static void main(String[] args) {

String[] s = new String[3];

System.out.println("s[0] is " + s[0].toString());

}

}

a. The program has a syntax error because the size of the array wasn't specified when declaring the array.

b. The program has a runtime error because s[0] is null.

c. The program runs fine and displays s[0] is 0.

d. None of the above.

14. What is wrong in the following code?

class Test {

public static void main(String[] args) {

C c = new C(5.0);

System.out.println(c.value);

}

}

class C {

int value = 2;

}

a. The program has a compilation error because class C does not have a default constructor.

b. The program has a compilation error because class C does not have a constructor with an argument of the double type.

c. The program compiles fine, but it does not run because class C is not public.

d. a and b.

15. What is wrong in the following code?

public class Foo {

public void method1() {

Circle c;

System.out.println("What is radius " + c.getRadius());

c = new Circle();

}

}

a. The program has a compilation error because class Foo does not have a main method.

b. The program has a compilation error because class Foo does not have a default constructor.

c. The program has a compilation error in the println statement because c has not been assigned an initial value.

d. The program compiles fine, but it has a runtime error because variable c is null when the println statement is executed.

16. What is wrong in the following code?

public class Foo {

public static void main(String[] args) {

method1();

}

public void method1() {

method2();

}

public void method2() {

System.out.println("What is radius " + c.getRadius());

}

Circle c = new Circle();

}

a. method2 should be declared before method1, since method2 is invoked from method1.

b. c should be declared before method2, since c is used in method2.

c. The program has a compilation error in the println statement where c has not been defined.

d. The program compiles fine, but it has a runtime error because variable c is null when the println statement is executed.

e. method1 is an instance method and cannot be invoked in the static context in the main method.

17. Which of the following will return true?

a. "Java is neat".matches("\\D*")

b. "Java is fun".matches("\\d*")

c. "Java is cool".matches("\\s*")

d. "Java is powerful".matches("\\S*")

e. "Java is powerful".matches("[\\w\\s]*")

18. Which of the following will return true?

a. "Java is neat".matches("Java.{3}neat")

b. "Java is neat".matches("Java.{4}neat")

c. "Java is fun".matches("Java.{4,}fun")

d. "Java is fun".matches("Java.{5,}fun")

e. "Java is fun".matches("Java.{4,6}fun")

19. What are the reasons to create an instance of the File class?

a. To determine whether the file exist.

b. To obtain the properties of the file such as whether the file can be read, written, or is hidden.

c. To rename the file.

d. To delete the file.

e. To read/write data from/to a file

20. Which method can be used to create an input object for file temp.txt?

a. new Scanner("temp.txt")

b. new Scanner(temp.txt)

c. new Scanner(new File("temp.txt"))

d. new Scanner(File("temp.txt"))

21. Which method can be used to create an output object for file temp.txt?

a. new Formatter("temp.txt")

b. new Formatter(temp.txt)

c. new Formatter(new File("temp.txt"))

d. new Formatter(File("temp.txt"))

Part II. (6 pts) Explain why the underlined code is wrong.

A. (Syntax errors)

public class Test {

private int x;

public static void main(String[] args) {

new Test();

}

public Test(int x) {

this.x = x;

}

}

B. (Syntax errors)

public class Test {

public static void main(String[] args) {

A a = new A(5.5);

System.out.println(a.x);

}

}

public class A {

private x;

public void A(double x) {

this.x = x;

}

}

C. (Syntax errors)

public class A{

String[] myStrings = new String[2];

myStrings[0] = new String("A");

public A( ){

}

}

Part III: Show the printout of the following code:

a. (2 pts each)

public class Test {

public static void main(String[] args) {

T t = new T();

swap(t);

System.out.println("e1 = " + t.e1 + " e2 = " + t.e2);

}

public static void swap(T t) {

int temp = t.e1;

t.e1 = t.e2;

t.e2 = temp;

}

}

class T {

int e1 = 1;

int e2 = 2;

}

b. (2 pts)

public class Test {

public static void main(String[] args) {

T t1 = new T();

T t2 = new T();

System.out.println("t1's i=" + t1.i + " and j=" + t1.j);

System.out.println("t2's i=" + t2.i + " and j=" + t2.j);

}

}

class T {

static int i = 0; // Please note that i is static

int j = 0;

T() {

i++;

j++;

}

}

c. (2 pts)

class Test {

public static void main(String[] args) {

Count myCount = new Count();

int times = 0;

for (int i = 0; i < 10; 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;

}

}

Part IV:

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.

[pic]

b. (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.

Key

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

1. e

2. b

3. d

4. a

5. b

6. d

7. b

8. d

9. e

10. c

11. a

12. e

13. b

14. b

15. c

16. e

17. ae

18. bce

19. abcd

20. c

21. ac

Part II. (6 pts)

A. (Syntax errors)

public class Test {

private int x;

public static void main(String[] args) {

new Test(); // Test doesn’t have a default constructor

}

public Test(int x) {

this.x = x;

}

}

B. (Syntax errors)

public class Test {

public static void main(String[] args) {

A a = new A(5.5); // A does not have this constructor

System.out.println(a.x); // x is private

}

}

public class A {

private x; // missing data type

public void A(double x) { // void should be removed

this.x = x;

}

}

C. (Syntax errors)

myStrings[0] = new String("A");

is a statement, which must be placed inside a method.

D. (Runtime error)

public class Test {

public static void main(String[] args) {

Object object = new Fruit();

Object object1 = (Apple)object; //ClassNotFoundException when casting object to Apple.

}

}

class Apple extends Fruit {

}

class Fruit {

}

}

Part III: Show the printout of the following code:

a. (2 pts each)

public class Test

{

public static void main(String[] args)

{

T t = new T();

swap(t);

System.out.println("e1 = " + t.e1 + " e2 = " + t.e2);

}

public static void swap(T t)

{

int temp = t.e1;

t.e1 = t.e2;

t.e2 = temp;

}

}

class T

{

int e1 = 1;

int e2 = 2;

}

Answer:

e1 = 2 e2 = 1

b. (2 pts)

public class Test

{

public static void main(String[] args)

{

T t1 = new T();

T t2 = new T();

System.out.println("t1's i="+t1.i+" and j="+t1.j);

System.out.println("t2's i="+t2.i+" and j="+t2.j);

}

}

class T

{

static int i = 0;

int j = 0;

T()

{

i++;

j++;

}

}

Answer:

t1’s i=2 and j = 1

t2’s i=2 and j = 1

c. (2 pts each)

true

true

d. (2 pts each)

myCount.count is 11

times is 0

Part IV

a.

public class Test {

public static void main(String[] args) {

Loan loan = new Loan(5.5, 15, 40000);

System.out.println("Monthly payment is " + loan.monthlyPayment());

System.out.println("Total payment is " + loan.totalPayment());

}

}

................
................

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

Google Online Preview   Download