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

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

<p>CIS 265 – Sect 01 - V. Matos Exam-2 Spring-2015</p><p>First Name______Last Name ______ID# ______</p><p>(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); }</p><p>1) ______n is 1. B) n is greater than 1. no base case. D) n is less than 1. </p><p>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); }</p><p>2) ______12 B) 9 C) 10 D) 11 </p><p>Analyze the following code: public class Test { public static void main(String[ ] args) { int[ ] x = {1, 2, 3, 4, 5}; xMethod(x, 5); }</p><p> public static void xMethod(int[ ] x, int length) {</p><p>System.out.p rint(" " + x[length - 1]); xMethod(x, length - 1); } }</p><p>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. </p><p>Analyze the } following two programs: A: public class Test { public static void main(String[ ] args) { xMethod(5); }</p><p> public static void xMethod(int length) { if (length > 1) {</p><p>System.out.p rint((length - 1) + " "); xMethod(len gth - 1); } } }</p><p>B: public class Test { public static void main(String[ ] args) { xMethod(5); }</p><p> public static void xMethod(int length) { while (length > 1) {</p><p>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. </p><p>To declare a class named A with a generic type, use 5) ______public class A<E> { ... } B) public class A(E, F) { ... } public class A(E) { ... } D) public class A<E, F> { ... } </p><p>Suppose List<String> list = new ArrayList<St ring>. 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()); </p><p>To create a list to store integers, use 7) ______ArrayList<Number> list = new ArrayList<Integer>(); ArrayList<int> list = new ArrayList<int>(); ArrayList<Object> list = new ArrayList<Integer>(); ArrayList<Integer> list 8) = new ArrayList<Integer>(); </p><p>Which of the data types below does not allow duplicates? 8) ______List Vector Stack Set LinkedList </p><p>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 </p><p>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] </p><p>The printout of the following code is ______.</p><p>LinkedHash Set<String> set1 = new LinkedHash Set<String>() ; set1.add("Ne w York");</p><p>LinkedHash Set<String> set2 = set1; set1.add("Atl anta"); set2.add("Da llas");</p><p>System.out.p rintln(set2);</p><p>11) ______[New York, Dallas] B) [New York, Atlanta] [New York] D) [New York, Atlanta, Dallas] </p><p>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 </p><p>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) </p><p>Analyze the following code. import java.util.*; public class Test { public static void main(String[ ] args) throws Exception { Set set = new TreeSet();</p><p> set.add("Red "); set.add("Gre en"); set.add("Blu e");</p><p>System.out.p rintln(set.firs t()); } }</p><p>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. </p><p>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) </p><p>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 </p><p>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</p><p>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</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    161 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us