Name: 
 

Chapter7



True/False
Indicate whether the sentence or statement is true or false.
 

 1. 

Consider the following code:

String doItAgain = “y”;
while (doItAgain.equals(“y”) ){
      System.out.print(“\nEnter degrees Fahrenheit: “);
      thermo.setFahrenheit(reader.nextDouble());
      // Consume the trailing newline
      reader.nextLine();
      System.out.println(“The equivalent in Celsius is “ +
            thermo.getCelsius());
      System.out.print(“\nDo it again (y/n)? “);
      doItAgain = reader.nextLine();
}

If the user enters an uppercase “Y” when prompted, the loop will repeat.
 

 2. 

In a menu-driven program in which the menu is repeatedly displayed, the menu should contain an option that allows the user to quit the program.
 

 3. 

One advantage of a menu-driven program is that you do not need to error-check the user’s menu choice.
 

 4. 

When printing the value of a double variable, the methods print and println always display all of the decimal digits.
 

 5. 

Consider the following code:

      double  dollars  =  25;
      double  tax  =  dollars  *  0.125;
      System.out.println(“Income:  $”  +  dollars);
      System.out.println(“Tax  owed:  $”  +  tax);

The values printed by the output statements would appear in a standard monetary format (two-decimal places of precision).
 

 6. 

A format string is a combination of literal string information and formatting information.
 

 7. 

In a format specifier code, a letter is used to specify the format type.
 

 8. 

In a printf statement, a format specifier in the format string and the corresponding expression must match in position and type.
 

 9. 

When creating a table, it is not important for all of the values in a column to be justified in the same manner.
 

 10. 

In a table, a data value is right-justified when its display begins in the leftmost column of its field.
 

 11. 

The format specifier %6.2f says to use a precision of 6 digits and a field of 2 columns when displaying the floating-point number.
 

 12. 

The String class’s format method takes the same arguments as the printf method.
 

 13. 

In a try-catch statement, all of the code within the try block will always be executed.
 

 14. 

Consider the following code segment:

    Scanner reader = new Scanner(System.in);
    Thermometer thermo = new Thermometer();
    while (true)
        try{
            System.out.print(“\nEnter degrees Fahrenheit: “);
            thermo.setFahrenheit(reader.nextDouble());
            break;
        }catch(Exception e){
            System.out.println(“Error in number format!”);
            reader.nextLine();
        }

The try-catch block in this code segment is equipped to catch any type of exception that might occur in the try block.
 

 15. 

A program that uses dialog box I/O forces the user to respond to a rigid sequence of pop-up prompts for data.
 

Multiple Choice
Identify the letter of the choice that best completes the statement or answers the question.
 

 16. 

Which of the following terms is not associated with handling repeating sets of input?
a.
count-controlled
c.
query-controlled
b.
input-controlled
d.
sentinel-controlled
 

 17. 

Java 5.0 includes the method ____ for formatting output.
a.
print
c.
printf
b.
println
d.
printFormatted
 

 18. 

Format specifier codes begin with the ____ character.
a.
%
c.
$
b.
#
d.
&
 

 19. 

The ____ format type code in a format specifier would denote a decimal integer value.
a.
d
c.
f
b.
e
d.
i
 

 20. 

The ____ format type code in a format specifier would denote a fixed floating-point value.
a.
d
c.
f
b.
e
d.
x
 

 21. 

The ____ format type code in a format specifier would denote an exponential floating-point value.
a.
d
c.
f
b.
e
d.
x
 

 22. 

The ____ format type code in a format specifier would denote a string value.
a.
c
c.
t
b.
s
d.
x
 

 23. 

Consider the following code:

   int idNum = 758;
    double wage = 10.5;
    printf(“The wage for employee %d is $%.2f per hour.”,
        idNum, wage);

The output of the printf statement would be ____.
a.
The wage for employee 10.50 is $758 per hour.
b.
The wage for employee 758 is $10.50 per hour.
c.
The wage for employee 758 is $10.5 per hour.
d.
The wage for employee 758.00 is $10 per hour.
 

 24. 

The ____ format flag is used for left justification.
a.
+
c.
*
b.
%l
d.
-
 

 25. 

The ____ format flag converts uppercase to lowercase.
a.
%
c.
-
b.
^
d.
@
 

 26. 

Which of the following represents the proper syntax for a try-catch statement?
a.
try{
   <statements that might throw exceptions>
}catch{
   <code to recover from an exception if it's thrown>
}
b.
try{
   <statements that might throw exceptions>
}(Exception e){
   <code to recover from an exception if it's thrown>
}
c.
try{
   <statements that might throw exceptions>
}catch(Exception e){
   <code to recover from an exception if it's thrown>
}
d.
try{
   <statements that might throw exceptions>
   <code to recover from an exception if it's thrown>
}
 

 27. 

Consider the following code fragment:

    double temp;
    Scanner reader = new Scanner(System.in);
    try{
1.     System.out.print(“\nEnter  degrees  Fahrenheit:  “);
2.     temp = reader.nextDouble());
3.     System.out.println(“\nYou entered “ + temp);
    }catch(Exception  e){
4.     System.out.println(“Error  in  number  format!”);
    }

Assuming that the user enters a value of “200e” when prompted, which of the numbered statements would be executed?
a.
1, 2, 3, 4
c.
1, 2, 4
b.
1, 2, 3
d.
1, 2
 

 28. 

____ classes should represent the data for a program.
a.
View
c.
Controller
b.
Model
d.
Menu
 

 29. 

The default layout of a JFrame is a ____.
a.
GridLayout
c.
FlowLayout
b.
BorderLayout
d.
No layout manager
 

 30. 

The JTextField class’s ____ method can be used to set a text field to be read-only.
a.
setEditable
c.
setReadOnly
b.
readOnly
d.
editable
 

Completion
Complete each sentence or statement.
 

 31. 

In ____________________ input, before each set of inputs after the first, the program asks the user whether there are more inputs.
 

 

 32. 

____________________ programs begin by displaying a list of options from which the user selects one.
 

 

 33. 

The ____________________ of a floating-point number is the number of digits to the right of the decimal point supported by the programming language.
 

 

 34. 

The parameters of the method printf consist of a(n) ____________________ and one or more data values.
 

 

 35. 

The formatting information in a format string consists of one or more ____________________.
 

 

 36. 

The symbol ____________________ can be used to embed an end-of-line character in a format string.
 

 

 37. 

When creating tabular output, a(n) ____________________ consists of a fixed number of columns within which the characters of a data value can be placed.
 

 

 38. 

The method printf includes several ____________________ that support the justification of text as well as other format styles.
 

 

 39. 

In general, when creating tabular output with the printf method, when the width of the field is less than or equal to the string representation of the number, the string is ____________________-justified.
 

 

 40. 

The String class’s ____________________ method can be used to build formatted strings.
 

 

 41. 

In order to detect and respond to an exception under program control so that the exception does not halt the program, you should use a(n) ____________________ statement.
 

 

 42. 

The Scanner methods nextInt and nextDouble will throw a(n) ____________________ exception if they attempt to read and parse a non-numerical value.
 

 

 43. 

When using the JTextField method called ____________________, the field is read-only by the user if the Boolean parameter b is false.
 

 

 44. 

____________________ classes should represent the graphical display for a GUI program.
 

 

 45. 

____________________ classes are listeners that listen for events generated by the components contained in the view classes.
 

 

 46. 

The JTextField class’s ____________________ method returns the string currently occupying the text field.
 

 

 47. 

Unlike programs based on terminal I/O or even dialog box I/O, real GUI programs are ____________________.
 

 

 48. 

The class for a button widget that can be placed on a UI is the ____________________ class.
 

 

 49. 

The class for a text label widget that can be placed on a UI is the ____________________ class.
 

 

 50. 

In order to register a button widget with a specific listener class (attach a listener to the button), you should use the ____________________ method.
 

 



 
Check Your Work     Reset Help