AP Computer Science A, 7th Edition - Moore Public Schools

Transcription

AP COMPUTER SCIENCE AFor 70 years, BARRON’S experts have been helping studentssurpass their expectations on standardized tests. This is the onlybook you will need to get the score you want on the AP ComputerScience A test.4P roven test-taking strategies that make the differencebetween a good score and a great score4O ne full-length diagnostic test to help you pinpoint yourstrengths and weaknesses4T wo additional full-length practice tests to boost yourconfidence4T he most up-to-date subject review that covers the specificmaterial you need to know for the testONLINESee inside front cover for details.4A ccess to a full-length sample AP Computer Science A examwith answer explanations and automatic scoringBARRON’S GIVES YOU THE ANSWERS!Every question in this book comes with a comprehensive answer explanation.AP*COMPUTERSCIENCE AMOST UP-TO-DATE REVIEWAND PRACTICE TESTSCURRENTLY AVAILABLE7TH EDITIONRoselyn Teukolsky, M.S.n  4 full-length practice tests with explainedanswers, including one onlinen  Expert advice on the best way to approachthe testn  Review covers all relevant topics, including thenew AP Computer Science A labs and changes tothe Java subsetBOOKEAN 18.99Canada 21.99w w w.b a r r o nseduc.co mPRINTEDIN THEUSA7TH EDITIONSTUDYGREEN.Printed on partially recycled paperFully recyclable4-0594-AP-ComputerScienceA-fbs.indd All PagesThe Leader in Test PreparationISBN: 978-1-4380-0594-2ONLINENew chapter ontheComputerSTUDYGREEN.Science A labsBARRON’S*BOOKBARRON’Swith BARRON’STeukolskySCORE HIGHEREverything you needto get a 55ONLINE*AP and Advanced Placement Program are registered trademarks of the College Board, which was notinvolved in the production of, and does not endorse, this product.11/7/14 12:41 PM

Now Available!BARRON’SOnlineAP ExamsGo tobarronsbooks.com/AP/compsci/to take a free sampleAP Computer Science A test,complete withanswer explanations andautomated scoring.*This online test was created for devices that support Adobe Flash Player. To access the test on anApple iPad or iPhone you will need to install a web browser that supports Flash (check the iTunesApp Store for free options).4-0594-AP-ComputerScienceA-IFC.indd All Pages11/5/14 12:54 PM

BARRON’SAPCOMPUTER*SCIENCE A7TH EDITIONRoselyn Teukolsky, M.S.Formerly, Ithaca High SchoolIthaca, New York*AP and Advanced Placement Program are registered trademarks of the College Board, which was not involved in the production of, and does not endorse, this book.4-0594-APComputerScienceA-tp.indd 110/7/14 9:09 AM

“ap” — 2014/11/4 — 11:10 — page ii — #2 About the Author:Roselyn Teukolsky has an M.S. degree from Cornell University, and has been teaching programming and computer science since 1980. She has published articles in The Mathematics Teacher and in the National Councilof Teachers of Mathematics Yearbook. She is the author of Barron’s ACT Math and Science Workbook and coauthor of Barron’s SAT 2400: Aiming for the Perfect Score. She has received the Edyth May Sliffe Award forDistinguished Mathematics Teaching and the Alfred Kalfus Distinguished Coach Award from the New YorkState Math League (NYSML).c Copyright 2015, 2013, 2010 by Barron’s Educational Series, Inc.Previous editions c copyright 2007 under the titleAP Computer Science Levels A and AB, 2003 under the titleHow to Prepare for the AP Computer Science AdvancedPlacement Examination, JAVA Version, and 2001under the title How to Prepare for the AP ComputerScience Advanced Placement Examinationby Barron’s Educational Series, Inc.All rights reserved.No part of this publication may be reproduced ordistributed in any form or by any means without the writtenpermission of the copyright owner.All inquiries should be addressed to:Barron’s Educational Series, Inc.250 Wireless BoulevardHauppauge, New York 11788www.barronseduc.comISBN: 978-1-4380-0594-2ISBN (with CD-ROM): 978-1-4380-7553-2ISSN (Print): 2169-5571ISSN (Print with CD-ROM): 2169-558XPRINTED IN THE UNITED STATES OF AMERICA987654321

“ap” — 2014/11/4 — 11:10 — page iii — #3 ContentsPrefaceixIntroductionGeneral Information About the Exam . .Hints for Taking the Exam . . . . . . . . . .The Multiple-Choice Section . . . . . .The Free-Response Section . . . . . . . .How to Use This Book . . . . . . . . . . . .Practice Exam One / Diagnostic TestComputer Science Section I . . . . . .Computer Science Section II . . . . . .Answer Key (Section I) . . . . . . . . .Diagnostic Chart for Practice Exam .Answers Explained . . . . . . . . . . . .Chapter 1. Introductory Java Language FeaturesPackages and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Javadoc Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Types and Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Built-in Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Storage of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hexadecimal and Octal Numbers . . . . . . . . . . . . . . . . . . . . . . . . .Final Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . .Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Decision-Making Control Structures . . . . . . . . . . . . . . . . . . . . . . .Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiple-Choice Questions on Introductory Java Language Concepts . .Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64656666676767676869697174778888iii

“ap” — 2014/11/4 — 11:10 — page iv — #4 iv ContentsChapter 2. Classes and ObjectsObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Public, Private, and Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Types of Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Reference vs. Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . .The Null Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Method Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiple-Choice Questions on Classes and Objects . . . . . . . . . . . . . . .Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . r 3. Inheritance and Polymorphism131Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Superclass and Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Inheritance Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Implementing Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132Declaring Subclass Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Dynamic Binding (Late Binding) . . . . . . . . . . . . . . . . . . . . . . . . . . 138Using super in a Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Type Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140Downcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140The ClassCastException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Abstract Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142The abstract Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145The implements Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145The Comparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Multiple-Choice Questions on Inheritance and Polymorphism . . . . . . . 150Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Chapter 4. Some Standard ClassesThe Object Class . . . . . . . . . . . . . . . .The Universal Superclass . . . . . . . . .Methods in Object . . . . . . . . . . . .The String Class . . . . . . . . . . . . . . . .String Objects . . . . . . . . . . . . . . .Constructing String Objects . . . . .The Concatenation Operator . . . . . .174174174174177177177178

“ap” — 2014/11/4 — 11:10 — page v — #5 Contents vComparison of String Objects . . . . . . . . .Other String Methods . . . . . . . . . . . . . . .Wrapper Classes . . . . . . . . . . . . . . . . . . . . . .The Integer Class . . . . . . . . . . . . . . . . .The Double Class . . . . . . . . . . . . . . . . . .The Math Class . . . . . . . . . . . . . . . . . . . . . . .Random Numbers . . . . . . . . . . . . . . . . . .Multiple-Choice Questions on Standard Classes .Answer Key . . . . . . . . . . . . . . . . . . . . . . . . .Answers Explained . . . . . . . . . . . . . . . . . . . .178179180181182183184187200200Chapter 5. Program Design and Analysis207The Software Development Life Cycle . . . . . . . . . . . . . . . . . . . . . . . 207The Waterfall Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Program Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Program Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Testing and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Program Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210Object-Oriented Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . 210Identifying Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210Identifying Behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Determining Relationships Between Classes . . . . . . . . . . . . . . . . . . 211UML Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Implementing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Implementing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Vocabulary Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Program Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Program Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Multiple-Choice Questions on Program Design and Analysis . . . . . . . . 221Answer Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Answers Explained . . . . . . . . . . . . . . . . . . . . . . . . .

COMPUTER SCIENCE A * AP STUDYGREEN. STUDYGREEN. STUDYGREEN. STUDYGREEN. S T U D Y G R E E N. Printed on partially recycled paper Fully recyclable COMPUTER SCIENCE A * BARRON’S GIVES YOU THE ANSWERS! Every question in this book comes with a comprehensive answer explanation. New chapter on the Computer Science A labs BOOK ONLINE ONLINE 7TH .