CPSC 220 Chapter 2 Answers
From MWCSWiki
Class Notes
- Chapter2 Questions
- Course Home page
- 1. Consider the following statement:
- String greeting = 13;
- Answer = B) The statement yields a compile-time error
- 2. Which of the following statements is correct?
- Answer = A) Identifiers can be made up of letters, digits, and the underscore (_) character
- 3. it is an error to use the value of a variable that has never had a value assigned to it.
- Answer = A) True
- 4. Which of the following code fragments will cause an error?
- Answer = D (int luckyNumber;
- System.out.println(luckyNumber);
- 5. The ____________ of a class specifies what you can do with its objects.
- Answer = Public Interface
- 6. Which of the following statements is correct in the Java language?
- Answer = C) Every object belongs to a class
- 7. Which of the folowing counts the number of characters in a string?
- String greeting = "Hello, World!";
- Answer = A) int n = greetng.length();
- 8. A method name is _______ if a class has more than one method with that name (but different parameter types).
- Answer = overloaded
- 9. Object variables store ______.
- Answer = A) references
- 10. Based on the followng statement, which of the following statements sets the title of the frame?.
- JFrame frame = new JFrame();
- Answer = D) frame.setTitle("An Empty Frame");
- 11. Use a _______ to recover the Graphics2D object from the Graphics parameter of the paintComponent method?
- Answer = Cast
- 12. Which of the following statements would complete the code below?
- public class RectangleComponent extends JComponent
- {
- public void paintComponent (Graphics g)
- {
- // Recover Graphics2D
- Graphics2D g2 = (Graphics2D) g;
- //Draw a square
- ____________
- g2.draw(box);
- }
- }
- [Answer = A)Rectangle box = new Rectangle(5, 10, 20, 20);
- 13.Consider the following code fragment:
- public class RedFlagComponent
- {
- public void paintComponent(Graphics g)
- {
- Graphics2D g2 = (Graphics2D) g;
- Rectangle flag = new Rectangle(100, 100, 200, 100);
- g2.draw(flag);
- }
- }
- [Answer = D) It is impossible to add a RedFlagComponent object to a frame because the class does not extend JComponent.
All answers
- B
- A
- True
- D
- Public Interface
- C
- A
- Overloaded
- A
- D
- Cast
- A
- D

