Sie sind auf Seite 1von 16

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. b. ANS: B True False

11. StringBuffer objects are immutable. a. b. 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 True False

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. 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 True False

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. They provide static methods that are very useful d. (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. 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 True False

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. ANS: A 36. StringBuffer objects are not immutable. a. b. 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); True False True False

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. It creates a Float 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 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. b. c. d. ANS: C

System.out.println(MAX_VALUE); System.out.println(integer.MAX_VALUE); System.out.println(Integer.MAX_VALUE); System.out.println(INTEGER.MAX_VALUE);

50. You cannot assign a value to a wrapper class object. a. b. ANS: B True False

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 14 Applets and More Multiple Choice
1. Applets are important because the can be used to ____. a. Extend the capabilities of a Web page. b. Process large data files c. Run other programs on the users system d. Keep the doctor away ANS: A 2. Which of the following is not a restriction placed on applets? a. Applets cannot create files on the users system b. Applets cannot execute operating system procedures on the users system c. Applets cannot display a window d. Applets cannot retrieve the users identity ANS: C 3. In an HTML document, the tags a. Instruct the Web browser how to format the text b. Instruct the Web browser where to place images c. Instruct the Web browser what to do when the user clicks on a link d. All of the above ANS: D 4. The browser creates an instance of the applet class automatically. a. True b. False

ANS: A 5. Which of the following will load the applet, TestApplet? a. APPLET CODETestApplet.class WIDTH200 HEIGHT50/APPLET b. APPLET CODETestApplet.class WIDTH200 HEIGHT50/APPLET c. LOAD CODETestApplet.class WIDTH200 HEIGHT50/LOAD d. LOAD CODETestApplet.class WIDTH200 HEIGHT50/LOAD ANS: A 6. An applet using a Swing GUI extends which class? a. Applet b. JApplet c. JSwing d. JFrame ANS: B 7. In the following code, which line has an error? 1. public class TestApplet extends JApplet 2. { 3. public void init() 4. { 5. super.JApplet(); 6. JLabel label new JLabel(Test label); 7. Container contentPane getContentPane(); 8. contentPane.setLayout(new FlowLayout()); 9. contentPane.add(label); 10. } 11. } a. 1 b. 3 c. 5 d. There are no errors ANS: C 8. When the applet viewer opens an HTML document with more than one APPLET tag, a. It terminates immediately b. It displays the first applet, and then terminates c. It displays each applet in a separate window d. It displays each applet in order in the same window ANS: C 9. If you want to make sure that an applet is compatible with all Java-enabled browsers, use AWT components instead of Swing. a. True b. False ANS: A

10. Which of the following is not a valid AWT class? a. Panel b. Applet c. TextField d. Jmenu ANS: D 11. If a frame component is 200 pixels wide and 300 pixels high, the lower, left-hand corner has (X,Y) coordinates of a. (0, 299) b. (0, 300) c. (200, 0) d. (0, 0) ANS: A 12. For all Swing components except JApplet and JFrame, you should override the paintComponent method instead of the paint method. a. True b. False ANS: A 13. A mouse motion listener class cannot respond to: a. The mouse button is clicked on b. The mouse cursor exits a components screen space c. The mouse moved d. The mouse button is released ANS: C 14. When you extend an interface, you must implement all the methods defined in the interface. If you are interested in only one or two mouse events, you can extend the ____ class which implements the MouseListener interface, but does not require you to write all the functions in the interface. a. MouseAdapter b. MouseExtender c. MouseMotionAdapter d. MouseMotionExtender ANS: A 15. The delay parameter in the Timer constructor is the amount of time between action events, measured in milliseconds. a. True b. False ANS: A 16. Given the following code, how many times per second will the TimerListener event be generated?

Timer timer new Timer(100, new TimerListener()); a. 1 b. 10 c. 100 d. 1000 ANS: B 17. Which of the following sound file formats does Java not support? a. .wav b. .au c. .mid d. Java supports all of the above ANS: D 18. The play method will load and play a sound file once; while the getAudioClip method will load the sound file and may assign the location of the sound file to a variable so it may be invoked multiple times. a. True b. False ANS: A 19. ____ are Java programs that are usually part of a Web site. a. Applications b. Applets c. Classes d. Web services ANS: B 20. Certain restrictions are placed on applets. If an applet attempts to violate one of these restrictions, a. The program has a fatal error b. It will display a window stating that it has violated the restriction c. An exception is thrown d. It automatically notifies the Web server of the violation ANS: C 21. Hypertext can contain a. A Java program b. Exception methods c. Executable code d. A link to another Web page ANS: D 22. The applet class requires that you write a constructor to create an object of the class. a. True b. False

ANS: B 23. One way of viewing the results of the applet, TestApplet, is to enter the following at the command prompt a. appletviewer TestApplet.html b. appleviewer TestApplet c. viewapplet TestApplet.html d. view TestApplet ANS: A 24. An applet using a Swing GUI is derived from the ____ class. a. JFrame b. JLabel c. JSwing d. JApplet ANS: D 25. In the following code that uses a swing GUI, which line has an error? 1. public class TestApplet extends Applet 2. { 3. public void init() 4. { 5. JLabel label new JLabel(Test label); 6. Container contentPane getContentPane(); 7. contentPane.setLayout(new FlowLayout()); 8. contentPane.add(label); 9. } 10. } a. 1 b. 3 c. 5 d. There are no errors ANS: A 26. In an applet, events are handled a. With special applet event listeners b. Differently, depending upon which computer is running the applet c. With event listeners exactly as they are in GUI applications d. By always terminating the applet ANS: C 27. Some browsers, such as Microsoft Internet Explorer, do not directly support the Swing classes of applets. a. True b. False ANS: A

28. When using AWT Applet objects, add components to a. The content pane b. The Applet object directly c. The window d. The panel ANS: B 29. If a frame component is 300 pixels wide and 200 pixels high, the upper, right-hand corner has (X,Y) coordinates of a. (0, 0) b. (300, 0) c. (200, 300) d. (299, 0) ANS: D 30. Whenever you need a component to be painted, call the paint method. a. True b. False ANS: B 31. Which of the following can a mouse listener respond to? a. The mouse button is pressed b. The mouse button is clicked c. The mouse cursor enters a components screen space d. All of the above ANS: D 32. When you extend an interface, you must implement all the methods defined in the interface. If you are interested in when the mouse moves, you can extend the ____ class which implements the MouseMotionListener interface, but does not require you to write all the functions in the interface. a. MouseAdapter b. MotionExtender c. MouseMotionAdapter d. MouseMotionExtender ANS: C 33. The delay parameter in the Timer constructor is the amount of time between action events, measured in microseconds. a. True b. False ANS: B 34. Given the following code, how many times per second will the TimerListener event be generated? Timer timer new Timer(10, new TimerListener());

a. b. c. d. ANS: C

1 10 100 10,000

35. In the following code, what does getDocumentBase() return? play(getDocumentBase(), mysound.wav); a. A URL object containing the location of the applets .class file b. A URL object containing the location of the HTML file that invoked the applet c. The HTML location mysound.wav d. The sound file itself ANS: D 36. The following getAudioClip() method returns an object that will load the sound file and assign the location of that file to clip which can then be used to call methods that may play the sound file one or more times: Audioclip clip getAudioClip(getDocumentBase(), mysound.wav); a. True b. False ANS: A

Das könnte Ihnen auch gefallen