Download SampleExam05(Ch7, 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
a.
b.
c.
d.
of the following statements is incorrect?
System.out.println(f.s);
int t = f.imethod();
System.out.println(f.i);
f.smethod();
3.
Which of the following statements is incorrect?
a.
Foo.smethod();
1
b.
c.
d.
System.out.println(Foo.s);
f.imethod();
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.
b.
c.
d.
6.
The program has syntax errors because the variable
radius is not initialized.
The program has syntax errors because a static PI is
defined inside a method.
The program has no syntax errors but will get a
runtime error because radius is not initialized.
The program compiles and runs fine.
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.
b.
c.
d.
7.
The variable t is not initialized and therefore
causes errors.
The variable t is private and therefore cannot be
accessed in the main method.
The variable x is not initialized and therefore
causes errors.
The program compiles and runs fine.
Analyze the following code:
class Test {
public static void main(String[] args) {
2
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.
b.
c.
d.
The program compiles fine, but has a runtime error
because the print() method is private.
The program has a compilation error because the
print() method in class A is private.
The program runs fine and prints Test.
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 = s1.compareTo(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.
3
c.
d.
e.
You can create a string buffer from a string.
You can convert a string buffer into a string.
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.
b.
c.
d.
The program has a syntax error because the size of
the array wasn't specified when declaring the array.
The program has a runtime error because s[0] is null.
The program runs fine and displays s[0] is 0.
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.
4
b.
c.
d.
The program has a compilation error because class Foo
does not have a default constructor.
The program has a compilation error in the println
statement because c has not been assigned an initial
value.
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
a.
b.
c.
d.
e.
of the following will return true?
"Java is neat".matches("\\D*")
"Java is fun".matches("\\d*")
"Java is cool".matches("\\s*")
"Java is powerful".matches("\\S*")
"Java is powerful".matches("[\\w\\s]*")
18.
Which
a.
b.
c.
d.
e.
of the following will return true?
"Java is neat".matches("Java.{3}neat")
"Java is neat".matches("Java.{4}neat")
"Java is fun".matches("Java.{4,}fun")
"Java is fun".matches("Java.{5,}fun")
"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.
5
b.
c.
d.
e.
To obtain the properties of the file such as whether
the file can be read, written, or is hidden.
To rename the file.
To delete the file.
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{
6
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;
int j = 0;
// Please note that i is static
T() {
i++;
j++;
}
}
c.
(2 pts)
class Test {
public static void main(String[] args) {
Count myCount = new Count();
7
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.
8
Loan
-annualInterestRate: double
The annual interest rate of the loan (default: 2.5).
-numberOfYears: int
The number of years for the loan (default: 1)
-loanAmount: double
The loan amount (default: 1000)..
-loanDate: Date
The date this loan was created.
+Loan()
Constructs a default loan object.
+Loan(annualInterestRate: double,
numberOfYears: int,
loanAmount: double)
Constructs a loan with specified interest rate, years, and
loan amount.
+getAnnualInterestRate(): double
Returns the annual interest rate of this loan.
+getNumberOfYears(): int
Returns the number of the years of this loan.
+getLoanAmount(): double
Returns the amount of this loan.
+getLoanDate(): Date
Returns the date of the creation of this loan.
+setAnnualInterestRate(
Sets a new annual interest rate to this loan.
annualInterestRate: double): void
Sets a new number of years to this loan.
+setNumberOfYears(
numberOfYears: int): void
+setLoanAmount(
loanAmount: double): void
Sets a new amount to this loan.
+monthlyPayment(): double
Returns the monthly payment of this loan.
+totalPayment(): double
Returns the total payment of this loan.
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.
9
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 {
10
private x; // missing data type
public void A(double x) {
this.x = x;
}
// void should be removed
}
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;
}
11
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());
}
}
12
13