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

Total Page:16

File Type:pdf, Size:1020Kb

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

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) ______n is 1. B) n is greater than 1. no base case. D) n is less than 1.

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) ______12 B) 9 C) 10 D) 11

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.p rint(" " + x[length - 1]); xMethod(x, length - 1); } }

3) ______The program displays 5 4 3 2 1. The program displays 1 2 3 4 6. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoun dsException. The program displays 5 4) 4 3 2 1 and then raises an ArrayIndexOutOfBoun dsException.

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.p rint((length - 1) + " "); xMethod(len gth - 1); } } }

B: public class Test { public static void main(String[ ] args) { xMethod(5); }

public static void xMethod(int length) { while (length > 1) {

System.out.p rint((length - 1) + " "); xMethod(len gth - 1); } } 4) ______The two programs produce the same output 1 2 3 4. Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely. The two programs produce the same output 4 3 2 1. The two programs produce the same output 1 2 3 4 5. The two programs produce the same output 5 4 3 2 1.

To declare a class named A with a generic type, use 5) ______public class A { ... } B) public class A(E, F) { ... } public class A(E) { ... } D) public class A { ... }

Suppose List list = new ArrayList. Which of the following operations are correct? 6) ______list.add(new ArrayList()); B) list.add("Red"); list.add(new Integer(100)); D) list.add(new java.util.Date());

To create a list to store integers, use 7) ______ArrayList list = new ArrayList(); ArrayList list = new ArrayList(); ArrayList list = new ArrayList(); ArrayList list 8) = new ArrayList();

Which of the data types below does not allow duplicates? 8) ______List Vector Stack Set LinkedList

Which of the data types below could be used to store elements in their natural order based on the compareTo method. 9) ______Set LinkedHashSet TreeSet Collection HashSet

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

The printout of the following code is ______.

LinkedHash Set set1 = new LinkedHash Set() ; set1.add("Ne w York");

LinkedHash Set set2 = set1; set1.add("Atl anta"); set2.add("Da llas");

System.out.p rintln(set2);

11) ______[New York, Dallas] B) [New York, Atlanta] [New York] D) [New York, Atlanta, Dallas]

If you want to store non- duplicated objects in the order in which they are inserted, you should use ______. 12) ______ArrayList LinkedList HashSet TreeSet LinkedHashSet

Which of the following is correct to perform the set union of two sets s1 and s2? 13) ______s1.addAll(s2) B) s1.add(s2) s1 + s2 D) s1.union(s2)

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("Gre en"); set.add("Blu e");

System.out.p rintln(set.firs t()); } }

14) ______The program displays Red The program may display Red, Blue, or Green. The program displays Blue The program displays Green The program cannot compile, because the first() method is not defined in Set.

Which method do you use to test if an element is in a set or list named x? 15) ______(element instanceof List) || (element instanceof Set) x.in(element) x.contains(element) x.include(element) x.contain(element)

Which data type should you use if you want to store duplicate elements and be able to insert or delete elements anywhere efficiently? 16) ______LinkedList Vector Set Stack ArrayList

Which of the following is correct to create a list from an array? 17) ______new List({"red", "green", "blue"}) new LinkedList(new String[ ]{"red", "green", "blue"}) new ArrayList(new String[ ]{"red", "green", "blue"}) new List(new String[ ] {"red", "green", "blue"}) 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 1 0) A 1 1) D 1 2) E 1 3) A 1 4) E 1 5) C 1 6) A 1 7) E

Recommended publications