Table Of Content7-3709-AP-CompScience-TP 5/4/07 10:51 AM Page 1
BARRON’S
AP
®
COMPUTER
SCIENCE
Levels A and AB
4TH EDITION
Roselyn Teukolsky, M.S.
Ithaca High School
Ithaca, New York
® AP is a registered trademark of the College Entrance Examination Board, which was not involved in the production of, and does not endorse, this book.
c Copyright2007byBarron’sEducationalSeries,Inc.
(cid:13)
Previouseditions c Copyright2003underthetitle
(cid:13)
HowtoPreparefortheAPComputerScienceAdvanced
PlacementExamination,JAVAVersion,and2001
underthetitleHowtoPreparefortheAPComputer
ScienceAdvancedPlacementExamination
byBarron’sEducationalSeries,Inc.
Allrightsreserved.
Nopartofthisbookmaybereproducedin
anyform,byphotostat,micro(cid:12)lm,xerography,
oranyothermeans,orincorporatedintoany
informationretrievalsystem,electronicor
mechanical,withoutthewrittenpermission
ofthecopyrightowner.
Allinquiriesshouldbeaddressedto:
Barron’sEducationalSeries,Inc.
250WirelessBoulevard
Hauppauge,NewYork11788
http://www.barronseduc.com
ISBN-13:978-0-7641-3709-9
ISBN-10:0-7641-3709-3
ISBN-13(withCD-ROM):978-0-7641-9350-7
ISBN-10(withCD-ROM):0-7641-9350-3
LibraryofCongressControlNumber: 2006102317
LibraryofCongressCataloging-in-PublicationData
Teukolsky,Roselyn.
Barron’sAPcomputerscience: LevelsAandAB/RoselynTeukolsky.
(cid:150)4thed.
p.cm.
Includesindex.
ISBN-13:978-0-7641-3709-9
ISBN-10:0-7641-3709-3
1. Computerscience(cid:150)Examinations,questions,etc. I.Title:
QA76.28.T482007
004’.076(cid:150)dc22
2006102317
PRINTEDINTHEUNITEDSTATESOFAMERICA
987654321
Contents
Preface xi
Introduction xiii
General Information About the Exam . . . . . . . . . . . . . . . . . . . . . . . xiii
Hintsfor Taking the Exam. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
TheMultiple-ChoiceSection . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
TheFree-ResponseSection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Practice Exam One / Level A Diagnostic Test 1
Computer Science A Section I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Computer Science A Section II . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Answer Key (Section I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Diagnostic Chart for Level A Exam . . . . . . . . . . . . . . . . . . . . . . . . . 38
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Practice Exam Two / Level AB Diagnostic Test 51
Computer Science AB Section I. . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Computer Science AB Section II . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Answer Key (Section I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Diagnostic Chart for Level AB Exam . . . . . . . . . . . . . . . . . . . . . . . . 101
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Chapter 1. Introductory Java Language Features 115
Packages and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Types and Identi(cid:12)ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Identi(cid:12)ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Built-inTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
StorageofNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
HexadecimalNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
FinalVariables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
ArithmeticOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
RelationalOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
LogicalOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
AssignmentOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
IncrementandDecrementOperators . . . . . . . . . . . . . . . . . . . . . . . 124
OperatorPrecedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
EscapeSequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
iii
iv Contents
Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Decision-MakingControlStructures . . . . . . . . . . . . . . . . . . . . . . . 126
Iteration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Multiple-ChoiceQuestions on Introductory Java Language Concepts . . 134
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Chapter 2. Classes and Objects 149
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Public, Private, and Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
TypesofMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
MethodOverloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
ThethisKeyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Referencevs.PrimitiveDataTypes . . . . . . . . . . . . . . . . . . . . . . . . 156
TheNullReference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
MethodParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Multiple-ChoiceQuestions on Classes and Objects. . . . . . . . . . . . . . . 166
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Chapter 3. Inheritance and Polymorphism 186
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
SuperclassandSubclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
InheritanceHierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
ImplementingSubclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
DeclaringSubclassObjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
DynamicBinding(LateBinding) . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Type Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Downcasting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
TheClassCastException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
AbstractClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
TheabstractKeyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
De(cid:12)ninganInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
TheimplementsKeyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
TheComparableInterface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Multiple-ChoiceQuestions on Inheritance and Polymorphism. . . . . . . 203
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Contents v
Chapter 4. Some Standard Classes 225
The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
TheUniversalSuperclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
MethodsinObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
StringObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
ConstructingStringObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
TheConcatenationOperator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
ComparisonofStringObjects . . . . . . . . . . . . . . . . . . . . . . . . . . 229
OtherStringMethods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
TheIntegerClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
TheDoubleClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
The Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
RandomNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Multiple-ChoiceQuestions on Standard Classes. . . . . . . . . . . . . . . . . 238
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Chapter 5. Program Design and Analysis 260
The Software Development Life Cycle . . . . . . . . . . . . . . . . . . . . . . . 260
TheWaterfallModel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
ProgramSpeci(cid:12)cation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
ProgramDesign. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
ProgramImplementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
TestingandDebugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
ProgramMaintenance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Object-Oriented Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . 263
IdentifyingClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
IdentifyingBehaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
DeterminingRelationshipsBetweenClasses . . . . . . . . . . . . . . . . . . 263
UMLDiagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
ImplementingClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
ImplementingMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
VocabularySummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Program Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
ProgramCorrectness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Ef(cid:12)ciency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Big-ONotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Multiple-ChoiceQuestions on Program Design and Analysis. . . . . . . . 274
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Chapter 6. Arrays and Array Lists 288
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
LengthofArray. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
TraversinganArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
vi Contents
ArraysasParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
ArrayVariablesinaClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
ArrayofClassObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
AnalyzingArrayAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Array Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
TheArrayListClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
TheMethodsofArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Auto-Boxingand-Unboxing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
UsingArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Two-DimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
ProcessingaTwo-DimensionalArray. . . . . . . . . . . . . . . . . . . . . . . 300
Two-DimensionalArrayasParameter . . . . . . . . . . . . . . . . . . . . . . 301
Multiple-ChoiceQuestions on Arrays and Array Lists . . . . . . . . . . . . 303
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Chapter 7. Recursion 338
Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
General Form of Simple Recursive Methods . . . . . . . . . . . . . . . . . . . 339
Writing Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Analysisof Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
SortingAlgorithms That Use Recursion. . . . . . . . . . . . . . . . . . . . . . 343
Recursive Helper Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Recursion in Two-Dimensional Grids . . . . . . . . . . . . . . . . . . . . . . . . 346
Multiple-ChoiceQuestions on Recursion . . . . . . . . . . . . . . . . . . . . . 349
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Chapter 8. Linked Lists 363
Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Linear Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
FeaturesofaLinkedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
TheListNodeClass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
ALinearLinkedListClass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Circular Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
ImplementingaCircularLinkedList . . . . . . . . . . . . . . . . . . . . . . . 371
DoublyLinked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
WhyDoublyLinkedLists? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
HeaderandTrailerNodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
ImplementingDoublyLinkedLists . . . . . . . . . . . . . . . . . . . . . . . . 374
Run Timeof Linked List vs. ArrayAlgorithms . . . . . . . . . . . . . . . . . 379
Multiple-ChoiceQuestions on Linked Lists . . . . . . . . . . . . . . . . . . . . 380
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Contents vii
Chapter 9. Stacks and Queues 403
Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
WhatIsaStack?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
TheStack<E>Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
WhentoUseaStack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
WhatIsaQueue? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
TheQueue<E>Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
QueueImplementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
WhentoUseaQueue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
WhatIsaPriorityQueue? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
ThePriorityQueue<E>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
ImplementationofaPriorityQueue . . . . . . . . . . . . . . . . . . . . . . . 409
WhentoUseaPriorityQueue . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Run Timeof Stack, Queue, and Priority Queue Operations . . . . . . . . 412
Multiple-ChoiceQuestions on Stacks and Queues . . . . . . . . . . . . . . . 413
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Chapter 10. Trees 432
Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
De(cid:12)nitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
ImplementationofBinaryTrees . . . . . . . . . . . . . . . . . . . . . . . . . . 433
TheTreeNodeClass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
ABinaryTreeClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Binary Search Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
ABinarySearchTreeClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
InsertinganElementintoaBinarySearchTree . . . . . . . . . . . . . . . . 437
FindingaTargetElementinaBinarySearchTree . . . . . . . . . . . . . . . 438
CreatingaBinarySearchTree. . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
TreeTraversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
ThreeMethodsofTraversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
ImplementingtheTraversalAlgorithms . . . . . . . . . . . . . . . . . . . . . 441
Recursive Tree Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
RecursionThatAlterstheTreeStructure . . . . . . . . . . . . . . . . . . . . 444
Binary Expression Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
In(cid:12)x,Post(cid:12)x,andPre(cid:12)xExpressions . . . . . . . . . . . . . . . . . . . . . . . 446
BinaryExpressionTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
EvaluatingaBinaryExpressionTree . . . . . . . . . . . . . . . . . . . . . . . 448
ABinaryExpressionTreeProgram . . . . . . . . . . . . . . . . . . . . . . . . 450
Run Timeof Binary Search Tree(BST) Algorithms . . . . . . . . . . . . . . 453
Multiple-ChoiceQuestions on Trees . . . . . . . . . . . . . . . . . . . . . . . . 454
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
viii Contents
Chapter 11. Collections 472
Collectionsin Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
WhatIsaCollection? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
TheCollectionsAPI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
The CollectionsHierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Collectionsand Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Collectionsand Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
De(cid:12)nitionofanIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
TheIterable<E>Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
TheIterator<E>Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
UsingaGenericIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
TheListIterator<E>Interface . . . . . . . . . . . . . . . . . . . . . . . . . 477
UsingtheListIterator<E>Interface. . . . . . . . . . . . . . . . . . . . . . 478
The List<E> Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
TheMethodsofList<E> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
TheArrayList<E>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
UsingArrayList<E> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
TheLinkedList<E>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
UsingLinkedList<E> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
WritingGeneralCode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
ArrayList vs. LinkedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
The Set<E> Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
TheMethodsofSet<E> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
TheHashSet<E>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
TheTreeSet<E>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
ExampleswithHashSet<E>andTreeSet<E> . . . . . . . . . . . . . . . . . 488
The Map<K,V> Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
TheMethodsofMap<K,V>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
TheHashMap<K,V>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
TheTreeMap<K,V>Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
IteratingoverMaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
ExampleswithHashMap<K,V>andTreeMap<K,V>. . . . . . . . . . . . . . 495
Run Timeof Set and Map Operations . . . . . . . . . . . . . . . . . . . . . . . 498
Multiple-ChoiceQuestions on Collections . . . . . . . . . . . . . . . . . . . . 500
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Chapter 12. Sorting and Searching 524
O(n2) Sorts: Selection and Insertion Sorts . . . . . . . . . . . . . . . . . . . . 524
SelectionSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
InsertionSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Recursive Sorts: Mergesort and Quicksort . . . . . . . . . . . . . . . . . . . . 526
Mergesort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
A Binary Tree Sort: Heapsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
SortingAlgorithms in Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Binary Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Hash Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Contents ix
ResolvingCollisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Note About Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Run Timeof Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Run Timeof Searching Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 539
Multiple-ChoiceQuestions on Sorting and Searching . . . . . . . . . . . . . 541
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Chapter 13. The GridWorld Case Study 564
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
The Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
The Actors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
The Location Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
The Actor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
The Rock and Flower Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
TheRockClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
TheFlowerClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
The Bug Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
The BoxBug Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
The Critter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
The ChameleonCritter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
The Grid<E> Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
The AbstractGrid<E> Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
The BoundedGrid<E> and UnboundedGrid<E> Classes. . . . . . . . . . . . . 578
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Run-TimeAnalysisof Grid Methods . . . . . . . . . . . . . . . . . . . . . . . . 582
BoundedGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
UnboundedGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Big-OSummary of Grid Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The Case Study and the AP Exam . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Multiple-ChoiceQuestions on the Case Study . . . . . . . . . . . . . . . . . . 585
Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
x Contents
Practice Exam Three 607
Computer Science A Section I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Computer Science A Section II . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Answer Key (Section I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Practice Exam Four 653
Computer Science AB Section I. . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Computer Science AB Section II . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Answer Key (Section I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Appendix A. Glossary of Useful Computer Terms 705
Appendix B. Supplementary Code for Evaluating a Binary
Expression Tree 708
Index 711
Description:Placement Examination, JAVA Version, and 2001 under the title How to Prepare for the AP Computer. Science Advanced Placement Examination.