CIS 265 – Sect 01 - V. Matos Exam-2 Spring-2015

First Name______Last Name ______ID# ______

(15 pts) MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

1)

In the following method, what is the base case?

static int xMethod(int n) {

if (n == 1)

return 1;

else

return n + xMethod(n - 1);

}


1)

______

A)

n is 1.

B)

n is greater than 1.

C)

no base case.

D)

n is less than 1.

2)

What is the return value for xMethod(4) after calling the following method?

static int xMethod(int n) {

if (n == 1)

return 1;

else

return n + xMethod(n - 1);

}


2)

______

A)

12

B)

9

C)

10

D)

11

3)

Analyze the following code:

public class Test {

public static void main(String[ ] args) {

int[ ] x = {1, 2, 3, 4, 5};

xMethod(x, 5);

}

public static void xMethod(int[ ] x, int length) {

System.out.print(" " + x[length - 1]);

xMethod(x, length - 1);

}

}


3)

______

A)

The program displays 5 4 3 2 1.

B)

The program displays 1 2 3 4 6.

C)

The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.

D)

The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

4)

Analyze the following two programs:

A:

public class Test {

public static void main(String[ ] args) {

xMethod(5);

}

public static void xMethod(int length) {

if (length 1) {

System.out.print((length - 1) + " ");

xMethod(length - 1);

}

}

}

B:

public class Test {

public static void main(String[ ] args) {

xMethod(5);

}

public static void xMethod(int length) {

while (length 1) {

System.out.print((length - 1) + " ");

xMethod(length - 1);

}

}

}


4)

______

A)

The two programs produce the same output 1 2 3 4.

B)

Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.

C)

The two programs produce the same output 4 3 2 1.

D)

The two programs produce the same output 1 2 3 4 5.

E)

The two programs produce the same output 5 4 3 2 1.

5)

To declare a class named A with a generic type, use

5)

______

A)

public class AE { ... }

B)

public class A(E, F) { ... }

C)

public class A(E) { ... }

D)

public class AE, F { ... }

6)

Suppose ListString list = new ArrayListString. Which of the following operations are correct?

6)

______

A)

list.add(new ArrayList());

B)

list.add("Red");

C)

list.add(new Integer(100));

D)

list.add(new java.util.Date());

7)

To create a list to store integers, use

7)

______

A)

ArrayListNumber list = new ArrayListInteger();

B)

ArrayListint list = new ArrayListint();

C)

ArrayListObject list = new ArrayListInteger();

D)

ArrayListInteger list = new ArrayListInteger();

8)

Which of the data types below does not allow duplicates?

8)

______

A)

List

B)

Vector

C)

Stack

D)

Set

E)

LinkedList

9)

Which of the data types below could be used to store elements in their natural order based on the compareTo method.

9)

______

A)

Set

B)

LinkedHashSet

C)

TreeSet

D)

Collection

E)

HashSet

10)

Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s1 is ______.

10)

______

A)

[1, 2, 3, 5, 6]

B)

[1, 2, 2, 3, 5, 6]

C)

[2]

D)

[1, 5]

11)

The printout of the following code is ______.

LinkedHashSetString set1 = new LinkedHashSetString();

set1.add("New York");

LinkedHashSetString set2 = set1;

set1.add("Atlanta");

set2.add("Dallas");

System.out.println(set2);


11)

______

A)

[New York, Dallas]

B)

[New York, Atlanta]

C)

[New York]

D)

[New York, Atlanta, Dallas]

12)

If you want to store non-duplicated objects in the order in which they are inserted, you should use ______.

12)

______

A)

ArrayList

B)

LinkedList

C)

HashSet

D)

TreeSet

E)

LinkedHashSet

13)

Which of the following is correct to perform the set union of two sets s1 and s2?

13)

______

A)

s1.addAll(s2)

B)

s1.add(s2)

C)

s1 + s2

D)

s1.union(s2)

14)

Analyze the following code.

import java.util.*;

public class Test {

public static void main(String[ ] args) throws Exception {

Set set = new TreeSet();

set.add("Red");

set.add("Green");

set.add("Blue");

System.out.println(set.first());

}

}


14)

______

A)

The program displays Red

B)

The program may display Red, Blue, or Green.

C)

The program displays Blue

D)

The program displays Green

E)

The program cannot compile, because the first() method is not defined in Set.

15)

Which method do you use to test if an element is in a set or list named x?

15)

______

A)

(element instanceof List) || (element instanceof Set)

B)

x.in(element)

C)

x.contains(element)

D)

x.include(element)

E)

x.contain(element)

16)

Which data type should you use if you want to store duplicate elements and be able to insert or delete elements anywhere efficiently?

16)

______

A)

LinkedList

B)

Vector

C)

Set

D)

Stack

E)

ArrayList

17)

Which of the following is correct to create a list from an array?

17)

______

A)

new List({"red", "green", "blue"})

B)

new LinkedList(new String[ ]{"red", "green", "blue"})

C)

new ArrayList(new String[ ]{"red", "green", "blue"})

D)

new List(new String[ ]{"red", "green", "blue"})

E)

Arrays.asList(new String[ ]{"red", "green", "blue"})


KEY

1)

A

2)

C

3)

D

4)

B

5)

A

6)

B

7)

D

8)

D

9)

C

10)

A

11)

D

12)

E

13)

A

14)

E

15)

C

16)

A

17)

E