System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə76/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   72   73   74   75   76   77   78   79   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

 Chapter 16 Self Test
1.
In general, AWT components are heavyweight and Swing components are
____________.
2.
Can the look and feel of a Swing component be changed? If so, what feature enables
this?
3.
What is the most commonly used top­level container for an application?
4.
Top­level containers have several panes. To what pane are components added?
5.
Show how to construct a label that contains the message "Select an entry from the
list".
6.
All interaction with GUI components must take place on what thread?


© 2018 
Safari

Terms of Service

Privacy Policy
7.
What is the default action command associated with a JButton? How can the action
command be changed?
8.
What event is generated when a push button is pressed?
9.
Show how to create a text field that has 32 columns.
10.
Can a JTextField have its action command set? If so, how?
11.
What Swing component creates a check box? What event is generated when a check
box is selected or deselected?
12.
JList displays a list of items from which the user can select. True or False?
13.
What event is generated when the user selects or deselects an item in a JList?
14.
What method sets the selection mode of a JList? What method obtains the index of
the first selected item?
15.
Add a check box to the file comparer developed in 
Try This 15­1
that has the
following text: Show position of mismatch. When this box is checked, have the program
display the location of the first point in the files at which a mismatch occurs.
16.
Change the ListDemo program so that it allows multiple items in the list to be
selected.
17. Bonus challenge: Convert the Help class developed in 
Try This 4­1
into a Swing­
based GUI program. Display the keywords (for, while, switch, and so on) in a JList.
When the user selects one, display the keyword’s syntax. To display multiple lines of
text within a label, you can use HTML. When doing so, you must begin the text with the
sequence . When this is done, the text is automatically formatted as described
by the markup. In addition to other benefits, using HTML enables you to create labels
that span two or more lines. For example, this creates a label that displays two lines of
text, with the string "Top" over the string "Bottom".
No answer is shown for this exercise. You have reached the point where you are ready
to apply your Java skills on your own!


Appendix A
Answers to Self Tests
CHAPTER 1: JAVA FUNDAMENTALS
1.
What is bytecode and why is it important to Java’s use for Internet programming?
Bytecode is a highly optimized set of instructions that is executed by the Java Virtual
Machine.
Bytecode helps Java achieve both portability and security.
2.
What are the three main principles of object­oriented programming?
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out


Encapsulation, polymorphism, and inheritance.
3.
Where do Java programs begin execution?
Java programs begin execution at main( ).
4.
What is a variable?
A variable is a named memory location. The contents of a variable can be changed
during the execution of a program.
5.
Which of the following variable names is invalid?
The invalid variable is D. Variable names cannot begin with a digit.
6.
How do you create a single­line comment? How do you create a multiline comment?
A single­line comment begins with // and ends at the end of the line. A multiline
comment begins with /* and ends with */.
7.
Show the general form of the if statement. Show the general form of the for loop.
The general form of the if:
if(condition) statement;
The general form of the for:
for(initialization; condition; iteration) statement;
8.
How do you create a block of code?
A block of code is started with a { and ended with a }.
9.
The moon’s gravity is about 17 percent that of the earth’s. Write a program that
computes your effective weight on the moon.


10.
Adapt 
Try This 1­2
so that it prints a conversion table of inches to meters. Display
12 feet of conversions, inch by inch. Output a blank line every 12 inches. (One meter
equals approximately 39.37 inches.)


11.
If you make a typing mistake when entering your program, what sort of error will
result?
A syntax error.
12.
Does it matter where on a line you put a statement?
No, Java is a free­form language.
CHAPTER 2: INTRODUCING DATA TYPES AND
OPERATORS
1.
Why does Java strictly specify the range and behavior of its primitive types?
Java strictly specifies the range and behavior of its primitive types to ensure portability


across platforms.
2.
What is Java’s character type, and how does it differ from the character type used by
some other programming languages?
Java’s character type is char. Java characters are Unicode rather than ASCII, which is
used by some other computer languages.
3.
A boolean value can have any value you like because any non­zero value is true.
True or False?
False. A boolean value must be either true or false.
4.
Given this output,
use a single string to show the println( ) statement that produced it.
5.
What is wrong with this fragment?
There are two fundamental flaws in the fragment. First, sum is created each time the
block defined by the for loop is entered and destroyed on exit. Thus, it will not hold its
value between iterations. Attempting to use sum to hold a running sum of the
iterations is pointless. Second, sum will not be known outside of the block in which it is
declared. Thus, the reference to it in the println( ) statement is invalid.
6.
Explain the difference between the prefix and postfix forms of the increment
operator.
When the increment operator precedes its operand, Java will perform the increment
prior to obtaining the operand’s value for use by the rest of the expression. If the


operator follows its operand, then Java will obtain the operand’s value before
incrementing.
7.
Show how a short­circuit AND can be used to prevent a divide­by­zero error.
8.
In an expression, what type are byte and short promoted to?
In an expression, byte and short are promoted to int.
9.
In general, when is a cast needed?
A cast is needed when converting between incompatible types or when a narrowing
conversion is occurring.
10.
Write a program that finds all of the prime numbers between 2 and 100.
11.
Does the use of redundant parentheses affect program performance?
No.
12.
Does a block define a scope?


Yes.
CHAPTER 3: PROGRAM CONTROL STATEMENTS
1.
Write a program that reads characters from the keyboard until a period is received.
Have the program count the number of spaces. Report the total at the end of the
program.
2.
Show the general form of the if­else­if ladder.
if(condition)

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   72   73   74   75   76   77   78   79   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə