AP Computer Science A, 7Th Edition
Total Page:16
File Type:pdf, Size:1020Kb
BARRON’S SCORE HIGHER Teukolsky BARRON’S with BARRON’S The Leader in Test Preparation AP For 70 years, BARRON’S experts have been helping students * surpass their expectations on standardized tests. This is the only book you will need to get the score you want on the AP Computer Science A test. * 4 Proven test-taking strategies that make the difference BOOK COMPUTER SCIENCE A between a good score and a great score 4 One full-length diagnostic test to help you pinpoint your AP strengths and weaknesses COMPUTER 4 Two additional full-length practice tests to boost your confidence SCIENCE A 4 The most up-to-date subject review that covers the specific material you need to know for the test MOST UP-TO-DATE REVIEW AND PRACTICE TESTS ONLINE 4 Access to a full-length sample AP Computer Science A exam CURRENTLY AVAILABLE with answer explanations and automatic scoring See inside front cover for details. 7TH EDITION Roselyn Teukolsky, M.S. n 4 full-length practice tests with explained answers, including one online n Expert advice on the best way to approach BARRON’S GIVES YOU THE ANSWERS! the test Every question in this book comes with a comprehensive answer explanation. n Review covers all relevant topics, including the new AP Computer Science A labs and changes to the Java subset ISBN: 978-1-4380-0594-2 BOOK New chapter on 7TH EDITION EAN Everything you need theSTUDY ComputerGREEN. ONLINE Science A labs to get a 5 ONLINE STUDYGREEN. 5 PRINTED Printed on partially recycled paper $18.99 Canada $21.99 IN THE Fully recyclable www.barronseduc.com USA *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 product. 4-0594-AP-ComputerScienceA-fbs.indd All Pages 11/7/14 12:41 PM STUDYGREEN. STUDYGREEN. G R Y E D E U N T . S Now Available! Go to BARRON’S barronsbooks.com/AP/compsci/ to take a free sample Online AP Computer Science A test, complete with AP Exams answer explanations and automated scoring. *This online test was created for devices that support Adobe Flash Player. To access the test on an Apple iPad or iPhone you will need to install a web browser that supports Flash (check the iTunes App Store for free options). 4-0594-AP-ComputerScienceA-IFC.indd All Pages 11/5/14 12:54 PM BARRON’S AP* COMPUTER SCIENCE A 7TH EDITION Roselyn Teukolsky, M.S. Formerly, Ithaca High School Ithaca, 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 1 10/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 com- puter science since 1980. She has published articles in The Mathematics Teacher and in the National Council of Teachers of Mathematics Yearbook. She is the author of Barron’s ACT Math and Science Workbook and co- author of Barron’s SAT 2400: Aiming for the Perfect Score. She has received the Edyth May Sliffe Award for Distinguished Mathematics Teaching and the Alfred Kalfus Distinguished Coach Award from the New York State Math League (NYSML). c Copyright 2015, 2013, 2010 by Barron’s Educational Series, Inc. Previous editions c copyright 2007 under the title AP Computer Science Levels A and AB, 2003 under the title How to Prepare for the AP Computer Science Advanced Placement Examination, JAVA Version, and 2001 under the title How to Prepare for the AP Computer Science Advanced Placement Examination by Barron’s Educational Series, Inc. All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means without the written permission of the copyright owner. All inquiries should be addressed to: Barron’s Educational Series, Inc. 250 Wireless Boulevard Hauppauge, New York 11788 www.barronseduc.com ISBN: 978-1-4380-0594-2 ISBN (with CD-ROM): 978-1-4380-7553-2 ISSN (Print): 2169-5571 ISSN (Print with CD-ROM): 2169-558X PRINTED IN THE UNITED STATES OF AMERICA 9 8 7 6 5 4 3 2 1 ✐ ✐ ✐ ✐ ✐ ✐ “ap” — 2014/11/4 — 11:10 — page iii — #3 ✐ ✐ Contents Preface ix Introduction xi GeneralInformationAbouttheExam ....................... xi HintsforTakingtheExam............................... xii TheMultiple-ChoiceSection ............................ xii TheFree-ResponseSection.............................. xii HowtoUseThisBook ................................. xiii Practice Exam One / Diagnostic Test 1 ComputerScienceSectionI .............................. 5 ComputerScienceSectionII.............................. 33 AnswerKey(SectionI) ................................. 45 DiagnosticChartforPracticeExam......................... 45 AnswersExplained .................................... 47 Chapter 1. Introductory Java Language Features 57 PackagesandClasses ................................... 57 JavadocComments .................................. 59 TypesandIdentifiers ................................... 60 Identifiers......................................... 60 Built-inTypes ...................................... 60 StorageofNumbers .................................. 61 HexadecimalandOctalNumbers ... ... ... ... .... ... ... ... 62 FinalVariables...................................... 63 Operators .......................................... 63 ArithmeticOperators................................. 63 RelationalOperators ................................. 64 LogicalOperators ................................... 65 AssignmentOperators ................................ 66 IncrementandDecrementOperators. 66 OperatorPrecedence ................................. 67 Input/Output ....................................... 67 Input............................................ 67 Output .......................................... 67 EscapeSequences.................................... 68 ControlStructures .................................... 69 Decision-MakingControlStructures . 69 Iteration.......................................... 71 ErrorsandExceptions .................................. 74 Multiple-Choice Questions on Introductory Java Language Concepts . 77 AnswerKey......................................... 88 AnswersExplained .................................... 88 iii ✐ ✐ ✐ ✐ ✐ ✐ “ap” — 2014/11/4 — 11:10 — page iv — #4 ✐ ✐ iv Contents Chapter 2. Classes and Objects 92 Objects............................................ 92 Classes ............................................ 93 Public,Private,andStatic ............................... 94 Methods ........................................... 95 Headers.......................................... 95 TypesofMethods ................................... 95 MethodOverloading ................................. 99 Scope ............................................. 100 The this Keyword ..................................100 References.......................................... 101 Referencevs.PrimitiveDataTypes . 101 TheNullReference ..................................103 MethodParameters ..................................103 Multiple-ChoiceQuestionsonClassesand Objects. 111 AnswerKey......................................... 126 AnswersExplained .................................... 126 Chapter 3. Inheritance and Polymorphism 131 Inheritance ......................................... 131 SuperclassandSubclass ................................131 InheritanceHierarchy.................................131 ImplementingSubclasses ...............................132 DeclaringSubclassObjects.. ... ... ... ... .... ... ... ... ...137 Polymorphism ....................................... 138 DynamicBinding(LateBinding). 138 Using super inaSubclass ..............................139 TypeCompatibility.................................... 140 Downcasting.......................................140 The ClassCastException .............................142 AbstractClasses ...................................... 142 AbstractClass......................................142 The abstract Keyword...............................142 Interfaces .......................................... 144 Interface..........................................144 DefininganInterface .................................145 The implements Keyword .............................145 The Comparable Interface..............................145 Multiple-Choice Questions on Inheritance and Polymorphism . 150 AnswerKey......................................... 169 AnswersExplained .................................... 169 Chapter 4. Some Standard Classes 174 The Object Class ..................................... 174 TheUniversalSuperclass...............................174 Methods in Object ..................................174 The String Class ..................................... 177 String Objects.....................................177 Constructing String Objects ...........................177 TheConcatenationOperator ... ... ... ... .... ... ... ... ...178 ✐ ✐ ✐ ✐ ✐ ✐ “ap” — 2014/11/4 — 11:10 — page v — #5 ✐ ✐ Contents v Comparison of String Objects ..........................178 Other String Methods................................179 WrapperClasses ...................................... 180 The Integer Class ..................................181 The Double Class ...................................182 The Math Class....................................... 183 RandomNumbers ...................................184 Multiple-ChoiceQuestionsonStandardClasses. 187 AnswerKey......................................... 200