Sie sind auf Seite 1von 10

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 10 Text Processing and More About

out Wrapper Classes Multiple Choice


1. Few programmers use wrapper classes because a. They are immutable b. They are not easy to use c. They provide useful static functions d. (a) and (b) ANS: D 2. The Character wrapper class provides numerous methods for a. Testing String objects b. Testing and converting char variables c. Converting String variables d. Adding two char variables ANS: B 3. What will be printed after the following code is executed? String str = abc456; int i = 0; while ( l < 6 ) { if (Character.isLetter(str.charAt(i)) System.out.println(Character.toUpperCase(charAt(i))); i++; } a. abc456 b. ABC456 c. ABC d. 456 ANS: C 4. If String str = RSTUVWXYZ, what will be the value returned from str.charAt(5)? a. U b. V c. W d. X ANS: C 5. If a non-letter is passed to the toLowerCase or toUpperCase method, it is returned unchanged.

a. b.

True False

ANS: A 6. What will be the value of matches after the following code is executed? boolean matches; String[] productCodes = {456HI345, 3456hj}; matches = productCodes[0].regionMatches(true, 1,productCodes[1], 2, 3) a. 56H b. 56h c. True d. False ANS: C 7. What will be the value of loc after the following code is executed? int loc; String str = The cow jumped over the moon. loc = str.indexOf(ov); a. 15 b. 16 c. 17 d. 18 ANS: A 8. In the following statement, what data type must recField be: str.getChars(5, 10, recField, 0); a. String b. int c. char d. char[] ANS: D 9. When you are writing a program with String objects that may have unwanted spaces at the beginning or end of the strings, use the ____ method to delete them. a. replace b. trim c. valueOf d. substring ANS: B 10. The valueOf() method accepts a string representation as an argument and returns its equivalent integer value. a. True

b.

False

ANS: B 11. StringBuffer objects are immutable. a. b. True False

ANS: B 12. The following StringBuffer constructor will StringBuffer str = new StringBuffer(25) a. Give the object, str, 25 bytes of storage and store spaces in them b. Give the object, str, 25 bytes of storage and not store anything in them c. Give the object, str, 25 or more bytes of storage and store spaces in them d. Give the object, str, 0 bytes of storage ANS: B 13. What would be the results of executing the following code? StringBuffer str = new StringBuffer(Little Jack Horner) str.append(sat on the); str.append(corner); a. The program would crash b. str would equal Little Jack Horner c. str would equal Little Jac Horner sat on the d. str would equal Little Jack Horner sat on the corner ANS: D 14. When using the StringBuffer insert method, you cannot insert a. A primitive type b. A String object c. Another StringBuffer type d. A char array ANS: C 15. What will be the value of strbuff after the following statements are executed? StringBuffer strbuff = new StringBuffer(We have lived in Chicago, Trenton, and Atlanta. strbuff.replace(17, 24, Tampa); a. We have lived in Tampa, Trenton, and Atlanta. b. We have lived in Chicago, Tampa, and Atlanta. c. We have lived in Chicago, Trenton, and Tampa. d. We have lived in Chicago, Tampaon, and Atlanta.

ANS: A 16. In a string that contains a series of words or other items of data separated by spaces or other characters, the programming term for the spaces or other characters is a. Token b. Delimiter c. Buffer d. Separator ANS: B 17. To use the class StringTokenizer, you must have the following import statement. a. import java.util.StringTokenizer; b. import java.util.String; c. import java.StringTokenizer; d. import java.String; ANS: A 18. What will be the tokens in the following statement? StringTokenizer strToken = new StringTokenizer(123-456-7890, -, true); a. 123, 456, and 7890 b. c. 123, 456, 7890, and d. None of the above ANS: C 19. For the following code, how many times would the while loop execute? StringTokenizer strToken = new StringTokenizer(Cars, trucks, and SUVs are all types of automobiles.); while (strToken.hasMoreTokens) { System.out.println(strToken.nextToken()); } a. 1 b. 5 c. 7 d. 9 ANS: D 20. If you are using characters other than whitespaces as delimiters, you will probably want to trim the string before tokenizing; otherwise, the leading and/or following whitespaces will become part of the first and/or last token. a. b. True False

ANS: A 21. What does the following statement do? Double number = new Double(8.8); a. It creates a Double object b. It initializes that object to 8.8 c. It assigns the objects address to the number variable d. All of the above ANS: D 22. To convert the string, str = 285.74 to a double and store it in the variable x, use the following statement a. double x = str; b. double x = Double.parseDouble(str); c. double x = Double.Double(str); d. double x = str,Double.parseDouble; ANS: B 23. To convert the integer variable, number = 56, to a string, use the following statement. a. String str = Integer.toString(number); b. String str = integer.toString(number); c. String str = integer(number); d. String str = number.Integer.toString(str); ANS: A 24. Which of the following statements will print the maximum value a double variable may have? a. System.out.println(MAX_VALUE); b. System.out.println(double.MAX_VALUE); c. System.out.println(Double.MAX_VALUE); d. System.out.println(DOUBLE.MAX_VALUE); ANS: C 25. You must call a method to get the value of a wrapper class object. a. True b. False ANS: B 26. Although it is not normally useful to create objects from the wrapper classes, programmers might use wrapper classes because a. They create immutable classes b. They are easy to use

c. d.

They provide static methods that are very useful (b) and (c)

ANS: C 27. Use the following import statement when using the character wrapper class a. import java.Char b. import java.lang.Char c. import java.String d. No import statement is needed ANS: D 28. What will be printed after the following code is executed? String str = abc456; int i = 0; while ( i < 6 ) { if (!Character.isLetter(str.charAt(i)) System.out.println(Character.toUpperCase(charAt(i))); i++; } a. 456 b. ABC456 c. ABC d. abc456 ANS: A 29. If String str = ABCDEFGHI, what will be returned from Character.toLowerCase(str.charAt(5))? a. e b. E c. f d. F ANS: C 30. If a non-letter argument is passed to the toLowerCase or toUpperCase method, a boolean false is returned. a. b. True False

ANS: B 31. What will be the value of matches after the following code has been executed? boolean matches;

String str1 = The cow jumped over the moon.; String str2 = moon; matches = str1.endsWith(str2); a. True b. False c. moon d. The cow ANS: B 32. What will be the value of loc after the following code is executed? int loc; String str = The cow jumped over the moon. loc = str.lastIndexOf(ov, 14); a. 15 b. 16 c. 0 d. 1 ANS: D 33. What is the value of str after the following code has been executed? String str; String sourceStr = Hey diddle, diddle, the cat and the fiddle; str = sourceStr.substring(12,17); a. diddle b. diddl c. , didd d. Iddle ANS: B 34. Two ways of concatenating two Strings are a. Use the concat() method or use the + between the two Strings b. Use the concatenate() method or use the + between the two Strings c. Use the contenate() method or the concat() method d. Use the concat() method or the trim() method ANS: A 35. The valueOf() method accepts a value of any primitive data type as an argument and returns a string representation of the value. a. b. True False

ANS: A

36. StringBuffer objects are not immutable. a. b. True False

ANS: A 37. The parameterless constructor for a StringBuffer object gives the object enough storage space to hold ___ a. 0 characters b. 8 characters c. 16 characters d. 32 characters ANS: C 38. What would be the results of executing the following code? StringBuffer strbuff = new StringBuffer(12); strbuff.append(The cow); strbuff.append(jumped over the); strbuff.append(moon.); a. The program would crash b. strbuff would equal The cow jump c. strbuff would equal The cow jumped over the d. strbuff would equal The cow jumped over the moon. ANS: D 39. Given the following statement, which of the following is not true? str.insert(8, 32); a. str is a StringBuffer type object b. The insert will start at position 32 c. The starting position for the insert is 8 d. The constant 32 will be inserted ANS: B 40. What will be the value of strbuff after the following statements are executed? StringBuffer strbuff = new StringBuffer(We have lived in Chicago, Trenton, and Atlanta. strbuff.replace(26, 33, Tampa); a. We have lived in Tampa, Trenton, and Atlanta. b. We have lived in Chicago, Tampa, and Atlanta. c. We have lived in Chicago, Trenton, and Tampa. d. We have lived in Chicago, Tampaon, and Atlanta. ANS: B 41. In a string that contains a series of words or other items of data separated

by spaces or other characters, the programming term for the data items is a. Token b. Delimiter c. Buffer d. Separator ANS: A 42. If you do not specify delimiters in the StringToken constructor, which of the following cannot be a delimiter? a. Space b. Tab c. Semicolon d. Newline ANS: C 43. What will be the tokens in the following statement? StringTokenizer strToken = new StringTokenizer(January 1, 2004, , , true); a. January, 1, 2004 b. comma and space c. January, 1, 2004, space d. January, 1, 2004, space, comma ANS: D 44. For the following code, how many times would the while loop execute? StringTokenizer strToken = new StringTokenizer(Ben and Jerrys ice cream is great.); while (strToken.hasMoreTokens) { System.out.println(strToken.nextToken()); } a. 1 b. 3 c. 5 d. 7 ANS: D 45. If a string has more than one character used as a delimiter, we must write a loop to determine the tokens, one for each delimiter character. a. True b. False ANS: B 46. What does the following statement do? Float number = new Float(8.8);

a. b. c. d.

It creates a Float object It initializes that object to 8.8 It assigns the objects address to the number variable All of the above

ANS: D 47. To convert the string, str = 285 to an integer and store it in the variable x, use the following statement a. integer x = str; b. integer x = Integer.parseInt(str); c. integer x = Integer.integer(str); d. integer x = str,Integer.parseInteger; ANS: B 48. To convert the double variable, d = 543.98, to a string, use the following statement. a. String str = Double.toString(d); b. String str = double.toString(d); c. String str = double(d); d. String str = d.Double.toString(str); ANS: A 49. Which of the following statements will print the maximum value an integer variable may have? a. System.out.println(MAX_VALUE); b. System.out.println(integer.MAX_VALUE); c. System.out.println(Integer.MAX_VALUE); d. System.out.println(INTEGER.MAX_VALUE); ANS: C 50. You cannot assign a value to a wrapper class object. a. b. True False

ANS: B

Das könnte Ihnen auch gefallen