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 the output of the following program?

import java.util.StringTokenizer;

class TestStringTokenizer {

public static void main(String[] args) {

// Create a string and string tokenizer

String s = "I+am-learning?Java.";

StringTokenizer st = new StringTokenizer(s, "+-");

// Retrieve and display tokens

while (st.hasMoreTokens())

System.out.print(st.nextToken() + " ");

}

}

a. I am learning Java.

b. I am learning?Java.

c. I+am-learning Java.

d. None of the above.

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. A subclass inherits _____________ from its superclasses.

a. private data

b. protected data

c. public data

d. a and c

e. b and c

18. When you implement an instance method that is defined in a superclass, you __________ the original method.

a. overload

b. override

c. copy

d. call

19. 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

20. What is the output of running class C?

class A {

public A() {

System.out.println(

"The default constructor of A is invoked");

}

}

class B extends A {

public B(String s) {

System.out.println(s);

}

}

public class C {

public static void main(String[] args) {

B b = new B("The constructor of B is invoked");

}

}

a. none

b. "The constructor of B is invoked"

c. "The default constructor of A is invoked"

"The constructor of B is invoked"

d. "The constructor of B is invoked"

"The default constructor of A is invoked"

e. "The default constructor of A is invoked"

21. 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) {…}

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( ){

}

}

D. (Runtime error)

public class Test {

public static void main(String[] args) {

Object object = new Fruit();

Object object1 = (Apple)object;

}

}

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;

}

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)

import java.util.*;

public class Test {

public static void main(String[] args) {

Date date = new Date();

Object o = date;

Date d = (Date)o;

System.out.println(date == o);

System.out.println(date == d);

}

}

d. (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]

a. (3 pts) Given the following UML for a class named MyDate, implement the class.

[pic]

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

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

Google Online Preview   Download