CIS 265 - Fall 2014 Exam-1 (Review) Fist Name______Last Name______

MULTIPLE CHOICE. (25points) Choose the one alternative that best completes the statement or answers the question. 1) The following code displays ______. double temperature = 50; if (temperature >= 100)

System.out.pri ntln("too hot"); else if (temperature <= 40)

System.out.pri ntln("too cold"); else

System.out.pri ntln("just right");

1) ______just right B) too cold too hot D) too hot too cold just right

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? 2) ______(1 > x > 100) || (x < 0) 1 < x < 100 && x < 0 ((x < 100) && (x > 1)) && (x < 0) ((x < 100) && (x > 1)) || (x < 0)

To check whether a char variable ch is an uppercase letter, you write ______. 3) ______(ch >= 'A' && ch >= 'Z') B) (ch >= 'A' && ch <= 'Z') ('A' <= ch <= 'Z') D) (ch >= 'A' || ch <= 'Z')

What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.pri ntln(y);

4) ______12 B) 45 C) 11 D) 13 E) 10

What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum >= 4) continue; } while (item < 5);

5) ______16 B) 15 C) 17 D) 18

If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ______. 6) ______0 B) 2 C) 1 D) 4 E) 3

If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ______. 7) ______2.0 3.4 5.5 3.4 undefined 8 )

In the following code, what is the printout for list2? class Test { public static void main(String[ ] args) { int[ ] list1 = {1, 2, 3}; int[ ] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i+ +)

System.out.pri nt(list2[i] + " "); } }

8) ______1 2 3 B) 0 1 2 C) 1 1 1 D) 0 1 3

______represents an entity in the real world that can be distinctly identified. 9) ______An object B) A data field A method D) A class

10) Analyze the following code: public class Test { public static void main(String[ ] args) { int[ ] x = {1, 2, 3, 4}; int[ ] y = x;

x = new int[2];

for (int i = 0; i < y.length; i++)

System.out.pri nt(y[i] + " "); } }

10) ______The program displays 0 0 0 0 B) The program displays 0 0 The program displays 1 2 3 4 D) The program displays 0 0 3 4

11) ______is a construct that defines objects of the same type. 11) ______A class B) A data field A method D) An object

12) An object is an instance of a ______. 12) ______data B) program C) class D) method

13) ______is invoked to create an object. 13) ______A constructor A method with the void return type The main method A method with a return type

14) Given the declaration Circle[ ] x = new Circle[10], which of the following statement is most accurate? 14) ______x contains a reference to an array and each element in the array can hold a reference to a Circle object. x contains a reference to an array and each element in the array can hold a Circle object. x contains an array of ten objects of the Circle type. x contains an array of ten int values.

15) What is the output of the following code? String s = "University"; s.replace("i", "ABC"); System.out.pri ntln(s);

15) ______University B) UnABCversity UniversABCty D) UnABCversABCty

16) You can create an ArrayList using ______. 16) ______new ArrayList[100] B) new ArrayList[ ] ArrayList() D) new ArrayList()

17) Object- oriented programming allows you to derive new classes from existing classes. This is called ______. 17) ______abstraction B) generalization encapsulation D) inheritance

18) What is the return value of "SELECT".sub string(0, 5)? 18) ______"SELECT" B) "SELE" "SELEC" D) "ELECT"

19) Invoking ______removes all elements in an ArrayList x. 19) ______x.clean() x.remove() x.delete() x.clear() x.empty()

20) Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]? 20) ______x.add(1, "Chicago") B) x.add(0, "Chicago") x.add(2, "Chicago") D) x.add("Chicago")

21) Invoking ______returns the first element in an ArrayList x. 21) ______x.get(1) B) x.get() C) x.get(0) D) x.first()

22) Invoking ______returns the number of the elements in an ArrayList x. 22) ______x.length(1) B) x.size() x.getSize() D) x.getLength(0)

23) Inheritance means ______. 23) ______that data fields should be declared private that a variable of supertype can refer to a subtype object that a class can extend another class that a class can contain another class

24) Composition means ______. 24) ______that a class can contain another class that data fields should be declared private that a variable of supertype can refer to a subtype object that a class can extend another class

25) Encapsulation means ______. 25) ______that a class can contain another class that a variable of supertype can refer to a subtype object that data fields should be declared private that a class can extend another class CIS 265 - Fall 2012 Exam- 1 Fist Name______Last Name______

(25 points) JAVA PROGRAMM ING Write a method to determine whether or not two strings are an anagram. Assume the strings only contain letters and spaces. Any two words or phrases that exactly include the same letters are called an anagram. For instance: “dad ” and “ add”, “stressed” and “desserts”, “George Bush” and “He bugs Gore”. Your method must not be case sensitive. The signature of the method follows:

public static boolean isAnagram (String s1, String s2)

1 ) A 2 ) D 3 ) B 4 ) B 5 ) B 6 ) E 7 ) A 8 ) B 9 ) A 1 ) 0 C 1 ) 1 A 1 ) 2 C 1 ) 3 A 1 ) 4 A 1 ) 5 A 1 ) 6 D 1 ) 7 D 1 ) 8 C 1 ) 9 D 2 ) 0 A 2 ) 1 C 2 ) 2 B 2 ) 3 C 2 ) 4 A 2 ) 5 C

Exam 1 KEY 1) A 2) D 3) B 4) B 5) B 6) E 7) A 8) B 9) A 10) C 11) A 12) C 13) A 14) A 15) A 16) D 17) D 18) C 19) D 20) A 21) C 22) B 23) C 24) A 25) C