C Sc 127B Practice Test 1 Answers

1.

1 2 3 4

2 4 6 8

3 6 9 12

4 8 12 16

2.

public void putSumInDiagonal() {

for (int row = 0; row < nRows; row++) {

int sum = 0;

for (int col = 0; col < nCols; col++) {

sum += m[row][col];

m[row][col] = 0;

}

m[row][row] = sum;

}

}

3.

a. _____O(n)______
b. _____O(n)______
c. _____O(log n)______/
d. ___O(n3)______
e. ____O(1)______
f. ____O(n log n)

4. Write an X in the comment to the right of all statements that compile. (6pts)

Object anObject = new Integer(3); // a. _x___

Integer anInt = new Object(); // b. _____

String str = (String)anObject; // c. _x___

int i2 = new Integer(-1); // d. _x___

Object obj = 3; // e. _x___

Double d3 = 12.0; // f. _x___

5. Write an X in the comment to the right of all statements that compile. (3pts)

Object[] elements = new Object[5];

elements[0] = 12; // a. _x___

elements[1] = "Second"; // b. _x___

String str = elements[1]; // c. _____

6. Does this code compile <yes/no> ? _no___

Object obj = 3;

Integer anInt = obj;

7. Will this code generate a runtime error (an exception) when it is run, yes or no __no__? (2pts)

Object obj = new String("abc");

String str = (String)obj;

8. a._DS_ 1D array b._ADT_ PriorityList<E> c._COLL_ ArrayPriortyList<E>

9. Changes in This font

public class BankAccount implements Comparable<BankAccount>

{

private String my_ID;

private double my_balance;

public BankAccount(String initID, double initBalance) {

my_ID = initID;

my_balance = initBalance;

}

public int compareTo(BankAccount other) {

int thisPennies = (int) (getBalance() * 100);

int otherPennies = (int) (other.getBalance() * 100);

return thisPennies - otherPennies;

}

}

10. 5 4 7 3 2 1

11. O(n2)

12. a. What is the value of first.data? __one_____

b. What is the value of first.next.next.data? ____ three______

c. What reference value equals first.next.next.next? ___ first______

d. Write the code that prints all values (make the code general to work on any size list

// Be wary of infinite loops

if (first != null) {

System.out.print(first.data + " ");

for (Node ref = first.next; ref != first; ref = ref.next)

System.out.print(ref.data + " ");

}

13.

Node back = new Node("B");

back.next = new Node("A");

back.next.next = back;

14 addLast

public void addLast(E element) {

// Special Case: list is empty

if (first == null)

first = new Node(element);

else { // Get ref to refer to the end of this list

Node ref = first;

while (ref.next != null) {

ref = ref.next;

}

ref.next = new Node(element); // Add element at the end

}

n++; // Just added an element, so increase size

}

reverse Other answers possible. In any case, it is legal to use other data structures to accomplish a task. In this case, storing elements into an array temporarily helped me

public void reverse() {

Object[] array = new Object[n];

// Put all elements in an array so they can be accessed easily

Node ref = first;

for (int i = 0; i < n; i++) {

array[i] = ref.data;

ref = ref.next;

}

// replace all Node data with the reversed array elements

ref = first;

for (int i = n - 1; i >= 0; i--) {

ref.data = (E) array[i];

ref = ref.next;

}

}