CS 2336 Exam 2
Analyze the following code:
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
setI(20);
// System.out.println("i from B is " + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
} - Correct Answers-The constructor of class A is called and it displays "i from A is 60".
Suppose list is a LinkedList that contains 1 million int values. Analyze the following code:
A:
for (int i = 0; i < list.size(); i++){
sum+= list.get(i);
}
B:
for (int i : list){
sum += i;
} - Correct Answers-Code fragment B runs faster than code fragment A.
, Fill in the most appropriate code in the blanks in the MyInt class?
public class MyInt implements _______ {
int id;
public MyInt(int id) {
this.id = id;
}
public String toString() {
return String.valueOf(id);
}
public int compareTo(_______ arg0) {
if (id > arg0.id)return 1;
else if (id < arg0.id)
return -1;
else
return 0;
}
} - Correct Answers-Comparable / MyInt
The iterator() method returns an instance of the ________ interface. - Correct Answers-Iterator
public class Test1 {
public static void main(String[] args) {
System.out.println(f1(3));
System.out.println(f2(3, 0));
}
public static int f1(int n) {
if (n == 0) return 0;
else { return n + f1(n - 1);
}
}
public static int f2(int n, int result) {
if (n == 0) return result;
else return f2(n - 1, n + result);
}
} - Correct Answers-f2 is tail recursion, but f1 is not
Analyze the following code:
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
setI(20);
// System.out.println("i from B is " + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
} - Correct Answers-The constructor of class A is called and it displays "i from A is 60".
Suppose list is a LinkedList that contains 1 million int values. Analyze the following code:
A:
for (int i = 0; i < list.size(); i++){
sum+= list.get(i);
}
B:
for (int i : list){
sum += i;
} - Correct Answers-Code fragment B runs faster than code fragment A.
, Fill in the most appropriate code in the blanks in the MyInt class?
public class MyInt implements _______ {
int id;
public MyInt(int id) {
this.id = id;
}
public String toString() {
return String.valueOf(id);
}
public int compareTo(_______ arg0) {
if (id > arg0.id)return 1;
else if (id < arg0.id)
return -1;
else
return 0;
}
} - Correct Answers-Comparable / MyInt
The iterator() method returns an instance of the ________ interface. - Correct Answers-Iterator
public class Test1 {
public static void main(String[] args) {
System.out.println(f1(3));
System.out.println(f2(3, 0));
}
public static int f1(int n) {
if (n == 0) return 0;
else { return n + f1(n - 1);
}
}
public static int f2(int n, int result) {
if (n == 0) return result;
else return f2(n - 1, n + result);
}
} - Correct Answers-f2 is tail recursion, but f1 is not