• Tidak ada hasil yang ditemukan

Student ID:

N/A
N/A
Protected

Academic year: 2023

Membagikan "Student ID: "

Copied!
16
0
0

Teks penuh

(1)

CONTINUED

THE UNIVERSITY OF AUCKLAND

SECOND SEMESTER, 2015 Campus: City

TEST

COMPUTER SCIENCE

Software Construction (Time Allowed: 50 Minutes)

Note:

 The use of calculators is NOT permitted.

 Compare the version number on the Teleform sheet supplied with the version number above.

If they do not match, ask the supervisor for a new sheet.

 Enter your name and Student ID on the Teleform sheet. Your name and Student ID should be entered left aligned. If your name is longer than the number of boxes provided, truncate it.

 Answer Section A on the Teleform answer sheet provided. For Section A, use a dark pencil to mark your answers in the answer boxes on the Teleform sheet. Check that the question

number on the sheet corresponds to the question number in this question/answer book. Do not cross out answers on the Teleform sheet if you change your mind. You must completely erase one answer before you choose another one. If you spoil your sheet, ask the supervisor for a replacement. There is one correct answer per question.

 Answer Section B in the space provided in the Section B Question/Answer Booklet.

 Attempt all questions. Write as clearly as possible. The space provided will generally be sufficient but is not necessarily an indication of the expected length. Extra space is provided at the end of this book.

Surname:

First Name(s):

Student ID:

Login Name (UPI):

MARKERS ONLY

Q1 – Q20 (/27)

Q21

(/6)

Q22

(/7)

Q23

(/6)

Q24

(/4)

Total (/50)

(2)

CONTINUED

SECTION A: MULTIPLE CHOICE QUESTIONS

For each question, choose the best answer according to the information presented in lectures. Select your preferred answer on the Teleform answer sheet provided by shading in the appropriate box.

Question 1

[1.5 marks] Suppose that s1 and s2 are two strings. Which of the statements or expressions is/are valid?

I) String s3 = s1 + s2;

II) String s3 = s1 - s2;

III) boolean b = s1 <= s2;

IV) int n = s1.compareTo(s2);

V) int m = s1.length();

(a)

I, II, IV and V

(b)

I and V only

(c)

I, III and V

(d)

I, IV and V

(e)

None of the above

Question 2

[1 mark] What is the output of the following code fragment?

int g = 3;

System.out.print(++g * 8);

(a)

32

(b)

8

(c)

16

(d)

24

(e)

None of the above.

Question 3

[1.5 marks] What is the output of the following code fragment?

Point p1 = new Point(10, 20);

Point p2 = new Point(10, 20);

Point p3 = p1;

Point p4 = new Point(p1);

System.out.print((p1==p2) + ",");

System.out.print((p1==p3) + ",");

System.out.print((p1==p4) + ",");

(a)

false,true,true,

(b)

false,true,false,

(c)

true,false,true,

(d)

true,false,false,

(e)

None of the above

(3)

CONTINUED Question 4

[1.5 marks] Which of the following code fragments is the correct way to create an ArrayList and add a value of type int?

(a)

ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1);

(b)

ArrayList<int> list = new ArrayList<Integer>(); list.add(1);

(c)

ArrayList<int> list = new ArrayList<int>(); list.add(1);

(d)

ArrayList<Integer> list = new ArrayList<int>(); list.add(1);

(e)

ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1.5);

Question 5

[1 mark] What is the output of the following code fragment?

int i;

for (i=0; i<10; i++) {

/* some code that doesn't modify i */

}

System.out.println("i="+i);

(a)

i=0

(b)

i=9

(c)

i=11

(d)

i=10

(e)

This piece of code doesn't compile since the scope of i is limited to the for loop.

Question 6

[1 mark] You want to initialize all of the elements of a double array a to the same value equal to 1.5. Which of the following should you use assuming that the array has been correctly initialized?

(a)

for(int i=0; i<a.length(); i++) a[i] = 1.5;

(b)

for(int i=0; i<a.length; i++) a[i] = 1.5;

(c)

for(int i=0; i<=a.length; i++) a[i] = 1.5;

(d)

for(int i=1; i<a.length; i++) a[i] = 1.5;

(e)

None of the above.

Question 7

[1.5 marks] Consider the following code fragment:

int number[] = new int[5];

Which of the following is true?

(a)

number.length() is 5.

(b)

number[4] is undefined.

(c)

number[5] is 0.

(d)

number[2] is 0

(e)

None of the above.

(4)

CONTINUED Use the class definition below to answer the next TWO questions:

public class Overload { public int x;

public double y;

public Overload() { this.x = 0;

this.y = 0;

}

public void add(int a , int b) { x = a + b; }

public void add(double c , double d) { y = c + d; } }

Question 8

[1.5 marks] What will be the content of the array variable table after executing the following code?

int[][] table = new int[3][3];

for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if( j == i)

table[i][j] = 1;

else table[i][j] = 0;

(a)

1 0 0, 1 1 0, 1 1 1

(b)

0 0 1, 0 1 0, 1 0 0

(c)

1 0 0, 0 1 0, 0 0 1

(d)

0 0 0, 0 0 0, 0 0 0

(e)

None of the above.

Question 9

[1 mark] What is the output of the following code fragment?

Overload obj = new Overload();

obj.add(2, 2);

System.out.println(obj.x + " " + obj.y);

(a)

4 0

(b)

5.2 0

(c)

4 6.4

(d)

0 6.4

(e)

None of the above.

Question 10

[1.5 marks] What is the output of the following code fragment?

Overload obj = new Overload();

obj.add(2, 3.2);

System.out.println(obj.x + " " + obj.y);

(a)

0 6.4

(b)

4 6.4

(c)

0 5.2

(d)

4 0

(e)

None of the above.

(5)

CONTINUED Question 11

[1.5 marks] Which of the following statements is true regarding the protected modifier?

(a)

protected members may be accessed by any class anywhere.

(b)

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

(c)

If a class has a protected field, then only it can access that field.

(d)

Only fields can have the protected modifier.

(e)

None of the above.

Question 12

[1.5 marks] Consider the class below:

public class Card { private int _code;

public Card(int code) { _code = code;

}

public boolean equals(Object obj) { return _code == obj._code;

}

public boolean equals(Card[] cards) { for (Card card: cards) {

if (_code == card._code) { return true;

} }

return false;

} }

Which of the following statements is true?

(a)

The code will not compile because the second equals implementation does not determine if one object is equal to only one other object.

(b)

The code will not compile as _code is declared as private.

(c)

The code will not compile because it has two methods named equals.

(d)

The code will not compile as _code is not accessible from the variable obj of type Object.

(e)

None of the above.

Question 13

[1.5 marks] Which of the following statement best describes the relationship between superclass and subclass types?

(a)

A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.

(b)

A subclass reference can be assigned to a superclass variable and a superclass reference can be assigned to a subclass variable.

(c)

A superclass reference can be assigned to a subclass variable, but a subclass reference cannot be assigned to a superclass variable.

(d)

A subclass reference cannot be assigned to a superclass variable and a superclass reference cannot be assigned to a subclass variable.

(e)

None of the above.

(6)

CONTINUED Use the classes below to answer the next TWO questions:

class ClassA {

public void method1() {

System.out.println("A.method1");

} }

class ClassB extends ClassA { public void method1() {

System.out.println("B.method1");

}

public void method2() {

System.out.println("B.method2");

} }

Question 14

[1 mark] The method1() method in ClassB is an example of

(a)

Information hiding

(b)

Method overriding

(c)

Procedural abstraction

(d)

Method overloading

(e)

None of the above

Question 15

[1.5 marks] Consider the following declarations:

ClassA ob1 = new ClassA();

ClassA ob2 = new ClassB();

Which of the following method calls will cause an error (compile time/run time)?

I) ob1.method2();

II) ob2.method2();

III) ((ClassB) ob1).method2();

IV) ((ClassB) ob2).method2();

(a)

I, II and III

(b)

I only

(c)

I, II, III and IV

(d)

I and II

(e)

None of the above.

(7)

CONTINUED Question 16

[1.5 marks] Consider the following classes:

class Bird {}

class Parrot extends Bird {}

class Owl extends Bird {}

class Parakeet extends Parrot {}

public class BirdStuff {

public static void main(String[] args) { Bird bird2 = new Parrot();

Parrot parrot2 = new Parakeet();

} }

Which of the following will cause an error?

(a)

Parakeet q = (Parakeet) parrot2;

(b)

Parrot p = (Parrot)bird2;

(c)

System.out.println(bird2 instanceof Owl);

(d)

System.out.println(parrot2 instanceof Owl);

(e)

None of the above.

Question 17

[1.5 marks] Consider the classes below:

class Base { int x = 10;

}

class Derived extends Base{

int x = 20;

}

What is the output of the following code fragment?

Base b = new Base();

Derived d = new Derived ( );

Base bd = new Derived();

System.out.println(b.x + " " + d.x + " " + bd.x);

(a)

20 10 20

(b)

10 20 20

(c)

10 20 10

(d)

20 20 10

(e)

None of the above.

Question 18

[1.5 marks] Which of the following statements about interfaces is false assuming that we are using Java 7 or below?

(a)

An interface describes a set of methods that can be called on an object, not providing concrete implementation for the methods.

(b)

An interface describes a set of methods that can be called on an object, providing a default implementation for the methods.

(c)

Once a class implements an interface, all objects of that class have an is-a relationship with the interface type.

(d)

Interfaces are useful when attempting to assign common functionality to possibly unrelated classes.

(e)

None of the above.

(8)

CONTINUED Question 19

[1.5 marks] Consider the classes below:

class BaseClass { private int x;

MemberClass n;

public BaseClass(int x) { n = new MemberClass();

this.x = x;

}

public String toString() { return n.toString();

}

class MemberClass { int x=20;

public String toString() {

/*implementation not shown */

} }

public static void main(String[] args) { BaseClass b1 = new BaseClass(10);

BaseClass b2 = new BaseClass(100);

System.out.println(b1);

System.out.println(b2);

} }

Which of the following statements could be used in the toString() method to produce the following output:

base:10, member:20 base:100, member:20

(a)

return "base:" + BaseClass.x + ", member:" + x

(b)

return "base:" + this.x + ", member:" + MemberClass.x ;

(c)

return "base:" + this.x + ", member:" + x ;

(d)

return "base:" + BaseClass.this.x + ", member:" + x ;

(e)

None of the above.

Question 20

[1 mark] Why are generics used?

(a)

Generics make code more fast.

(b)

Generics add stability to your code by making more of your bugs detectable at compile time.

(c)

Generics add stability to your code by making more of your bugs detectable at run time.

(d)

Generics make code more optimised and readable.

(e)

None of the above.

(9)

CONTINUED

Question 21: UML Class Diagram [6 marks]

Two of the most commonly used accounts are saving and cheque. Each of these accounts has various options. A customer may open any number of accounts. The banking system requires keeping track of a customer’s name, address, phone number and the IRD number. All bank accounts at a bank must

maintain 3 common attributes (the customer ID, the account number and the balance of money remaining in the account). Also, an account, by default, should have simple behaviours to deposit and withdraw from the account. Withdrawals can only be made if sufficient funds are in the account and each withdrawal fee is $1.0.

The purpose of savings account is to allow us to save money. Account holder can make unlimited number of deposits but ONE withdrawal per month only. The withdrawal fee is $1.0. Again, withdrawals can only be made if sufficient funds are in the account. Saving account pays interest rate at 0.5% monthly.

Money maker account pays interest (1.75%) at a higher rate than the rate paid on savings. Account holder can make unlimited number of deposits and but withdrawal is not allowed with this account.

A cheque account is a bank account that allow users to withdraw or transfer money from the account. The cheque accounts permit the balance to dip below zero, up to some imposed limit. No interest is paid on balances.

Draw a UML class diagram for the above system. To receive full marks, you must show the relationships, multiplicities, operations, and attributes if these are important in your design.

(6 marks)

(10)

CONTINUED

Question 22 [7 marks]

Consider the classes below:

class BankAccount {

protected String ownerID, accountNumber;

protected double balance;

protected static double WITHDRAWAL_FEES = 1.0;

public BankAccount(String customerID, String accNum) { ownerID = customerID;

accountNumber = accNum;

}

public String toString() {

return this.getClass().getName() + " with $" + this.balance;

}

public boolean withdraw(double amount) { if (balance + WITHDRAWAL_FEES > amount) {

balance = balance – amount - WITHDRAWAL_FEES;

return true;

}

return false;

}

public void deposit(double amount) { this.balance += amount; } ...

}

class SavingsAccount extends BankAccount { protected static double INTEREST_RATE = 0.5;

private boolean hasWithdrawn = false; // ONE withdrawal per month only public SavingsAccount(String customerID, String accNum) {

super(customerID,accNum );

}

public boolean withdraw(double amount) { /*implementation not shown */ } public void addMonthlyInterest() {

double interest = balance * this.INTEREST_RATE /100;

this.deposit(interest);

hasWithdrawn = false;

} ...

}

class MoneyMakerAccount extends SavingsAccount { protected static double INTEREST_RATE = 1.75;

public MoneyMakerAccount(String customerID, String accNum) { super(customerID,accNum );

}

public boolean withdraw(double amount) { /*implementation not shown */ } ...

}

class ChequeAccount extends BankAccount { private double overdraftLimit;

public ChequeAccount(String customerID, String acNum, double od) { super(customerID,acNum );

overdraftLimit = od;

}

public boolean withdraw(double amount) { /*implementation not shown */ } ...

}

(11)

CONTINUED For example, the following code:

BankAccount ba001 = new BankAccount("001", "B001");

ba001.deposit(200);

System.out.println(ba001.withdraw(500));

System.out.println(ba001);

BankAccount ba002= new BankAccount("002", "B002");

ba002.deposit(1000);

System.out.println(ba002.withdraw(500));

System.out.println(ba002);

will produce the output:

false

BankAccount with $200.0 true

BankAccount with $499.0

a) Complete the withdraw() method of the SavingsAccount class. For example, the following code:

SavingsAccount sa001 = new SavingsAccount("001", "S001");

sa001.deposit(200);

System.out.println(sa001.withdraw(50)); //successful!

System.out.println(sa001.withdraw(50)); //fail! ONE withdrawal only System.out.println(sa001);

will produce the output:

true false

SavingsAccount with $149.0

public boolean withdraw(double amount) { // return: true if withdraw successful

}

(3 marks)

(12)

CONTINUED b) Complete the withdraw() method of the MoneyMakerAccount class. For example, the

following code:

MoneyMakerAccount mm001 = new MoneyMakerAccount("001", "M001");

mm001.deposit(200);

System.out.println(mm001.withdraw(50)); //fail! no withdrawals System.out.println(mm001);

will produce the output:

false

MoneyMakerAccount with $200.0

public boolean withdraw(double amount) {

}

(1 mark)

c) Complete the withdraw() method of the ChequeAccount class. For example, the following code:

ChequeAccount c001 = new ChequeAccount("001", "C001", 1000);

c001.deposit(200);

System.out.println(c001.withdraw(500)); //successful!

System.out.println(c001.withdraw(2000)); //fail! Over the limit System.out.println(c001);

will produce the output:

true false

ChequeAccount with $-301.0

public boolean withdraw(double amount) { // return: true if withdraw successful

}

(3 marks)

Question 23 [6 marks]

Consider the classes below:

(13)

CONTINUED abstract class Circle {

protected double radius;

public Circle() { radius = 0; }

public Circle(double r) { radius = r; } public double getArea() {

return Math.PI*radius*radius;

}

public abstract double getVolume();

public String toString() {

return this.getClass().getName() + ",r=" + radius;

} }

class Sphere extends Circle { public Sphere() { }

public Sphere(double r) { super(r); }

public double getArea() { /*implementation not shown */ } public double getVolume() { /*implementation not shown */ } }

class Cylinder extends Circle { protected double height;

public Cylinder() { }

public Cylinder(double r) { /*implementation not shown */ }

public Cylinder(double r, double h) { /*implementation not shown */ } public double getArea() { /*implementation not shown */ }

public double getVolume() { /*implementation not shown */ } public String toString() {

return this.getClass().getName() + ",r=" + radius + ",h=" + height;

} }

The above code illustrates an abstract class Circle, which has two non-abstract derived classes, Sphere and Cylinder. Generally 2D means shapes has length, width and a base area. 3D means shapes has length, width, a base area and height. We calculate the surface area and volume based on the base area and the height.

a) Complete the two constructors of the Cylinder class. For example, the following code:

Cylinder c1 = new Cylinder(4); //radius is 4, height is 1 Cylinder c2 = new Cylinder(8, 12); //radius is 8, height is 12 System.out.println(c1);

System.out.println(c2);

will produce the output:

Cylinder,r=4.0,h=1.0 Cylinder,r=8.0,h=12.0

public Cylinder(double r) {

(14)

CONTINUED }

(1 mark) public Cylinder(double r, double h) {

}

(1 mark)

b) Complete the getArea() method of the Sphere class which calculates the surface area of a sphere. The surface area of a Sphere is

∗ _

where _ is the area of the base circle. For example, the following code:

Sphere s1 = new Sphere(2);

Sphere s2 = new Sphere(10);

System.out.println(s1.getArea());

System.out.println(s2.getArea());

will produce the output:

50.26548245743669 1256.6370614359173

public double getArea() {

// calculates the surface area of a sphere

}

(2 marks)

c) Complete the getVolume() method of the Sphere class which calculates the volume of a sphere. The volume of a Sphere is

_ ∗ ∗ /

where _ is the surface area of the sphere. For example, the following code:

(15)

CONTINUED Sphere s1 = new Sphere(2);

Sphere s2 = new Sphere(10);

System.out.println(s1.getVolume());

System.out.println(s2.getVolume());

will produce the output:

33.510321638291124 4188.790204786391

public double getVolume() {

// calculates the volume of a sphere

}

(2 marks)

Question 24 [4 marks]

Both abstract classes and interfaces provide a level of abstraction about what functionalities their

"subtype" classes must provide. However, each provides a capability not provided by the other.

What capability do abstract classes provide that interfaces do not and what capability do interfaces provide that abstract classes do not? You are required to write TWO capabilities only.

1.

2.

(4 marks)

(16)

OVERFLOW PAGE

(If you have used this page, please indicate clearly under the

relevant question that you have overflowed to this page)

________________________________________

Referensi

Dokumen terkait

Hal ini menunjukkan bahwa model regresi yang terbentuk signifikan dan dapat digunakan untuk memprediksi profitabilitas perbankan pada LQ-45, ini juga berarti bahwa