[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-341433-105":59,"doc-detail-341433-en":130},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","be-prepared-for-the-computer-science-exam-in-java-chapter-5-annotated-solutions-to-past-free-response-questions-2026","Be Prepared - for the Computer Science Exam in Java - Chapter 5: Annotated Solutions to Past Free-Response Questions - 2026","","Chapter 5 compiles annotated solutions to past free-response questions for the Computer Science exam in Java. It provides detailed Java code examples alongside notes that explain key assumptions, such as preconditions, boundary cases, and recursion alternatives. The chapter demonstrates how to structure iterative and conditional logic, manipulate strings for usernames, update object state safely, compare records across collections, and compute scoring rules based on grid row colors.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":20,"@type":76,"position":81},"https://docshare.wps.com/document/exam/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/be-prepared-for-the-computer-science-exam-in-java-chapter-5-annotated-solutions-to-past-free-response-questions-2026/341433/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/be-prepared-for-the-computer-science-exam-in-java-chapter-5-annotated-solutions-to-past-free-response-questions-2026/341433.png","ImageObject",300,407,{"name":92,"@type":93},"Jacob","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-10-26","2026-09-22",true,{"@type":102,"interactionType":103,"userInteractionCount":52},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"What does Chapter 5 include for the Java exam?","Question",{"text":112,"@type":113},"It includes annotated solutions to past free-response questions, with Java code and explanatory notes for the required parts.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"How does Question 1 handle the shortened username?",{"text":117,"@type":113},"It iteratively removes the section around the first hyphen using indexOf and substring operations, and it also presents a recursive helper method as an alternative.",{"name":119,"@type":110,"acceptedAnswer":120},"What scoring logic is used in Question 4?",{"text":121,"@type":113},"The solution sums points across a target row and doubles the total when all spaces in the row share the same color.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},341433,1790437691,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":19,"category_name":20,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":52,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":34,"language":139,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":140,"faqs":141,"seo_title":142,"seo_description":67,"update_tm":143,"read_time":144,"preview_page_count":4},962084931830,"https://ap-avatar.wpscdn.com/davatar_a8503ba1806abce46bf441b54a3ca4cd","Ninth Edition  \nBe Prepared  \nfor the  \nComputer Science Exam in Java  \nChapter 5: Annotated Solutions to Past Free-Response Questions  \n2026  \nMaria Litvin  \nPhillips Academy, Andover, Massachusetts  \nGary Litvin  \nSkylight Publishing, Andover, Massachusetts  \nSkylight Publishing  \nAndover, Massachusetts  \nCopyright © 2026 by  \nMaria Litvin, Gary Litvin, and Skylight Publishing  \nAll rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the authors and Skylight Publishing.  \nISBN 978-0-9972528-3-5  \nSkylight Publishing  \n14 Lincoln Street Andover, MA 01810  \nweb: [www.skylit.com](www.skylit.com)  \ne-mail: [sales@skylit.com](sales@skylit.com)  \n[support@skylit.com](support@skylit.com)  \n[www.skylit.com/beprepared/x2026all.zip](www.skylit.com/beprepared/x2026all.zip contains)[ contains](www.skylit.com/beprepared/x2026all.zip contains)[ ](www.skylit.com/beprepared/x2026all.zip contains)complete Java code, including solutions and test programs for runnable projects.  \nThe free-response questions for this exam are posted on AP Central:  \n[https://apcentral.collegeboard.org/courses/ap-computer-science-a/exam/past](https://apcentral.collegeboard.org/courses/ap-computer-science-a/exam/past)exam-questions  \nScoring guidelines for teachers are usually posted over the summer.  \nQuestion 1  \nPart (a)  \npublic Account(String requestedName)  \n{  \nint num = 1;  \nusername = requestedName;  \nwhile (!isAvailable(username))  \n{  \nusername = requestedName + num; 1  \nnum++;  \n}  \n}  \nNotes:  \n1. Not  \nusername += num;  \nPart (b)  \npublic String getShortenedName()  \n{  \nString result = username;  \nint pos = 0;  \nwhile (pos >= 0) 1  \n{  \npos = result .indexOf(\"-\");  \nif (pos >= 0)  \nresult = result .substring(0, pos-1) +  \nresult .substring(pos+1);  \n}  \nreturn result;} 2  \nNotes:  \n1. According to the method’s precondition, pos cannot be 0—only negative or greater than 0.  \n2. A recursive solution:  \n// Helper method:  \nprivate String getShortenedName(String name)  \n{  \nint pos = name .indexOf(\"-\");  \nif (pos \u003C 0)  \nreturn name;  \nname = name .substring(0, pos-1) + name .substring(pos+1); return getShortenedName(name);  \n}  \npublic String getShortenedName()  \n{  \nreturn getShortenedName(username);  \n}  \nQuestion 2  \npublic class Bottle {  \nprivate final double capacity;  \nprivate double amount;  \npublic Bottle(double cap)  \n{  \ncapacity = cap;  \namount = capacity;}  \npublic double updateAmount(double {  \namount - = amtToBeRemoved;  \nif (amount \u003C 0 .25*capacity) amount = capacity;  \nreturn amount;}  \n}  \nNotes:  \n1. Do not recheck the given preconditions.  \namtToBeRemoved)  \n1  \nQuestion 3  \npublic int moreHistoryThanMathAbsences()  \n{  \nint numStudents = 0;  \nfor (CourseRecord histRec : historyList)  \n{  \nfor (CourseRecord mathRec : mathList) 1  \n{  \nif (histRec.getStudentID() .equals(mathRec.getStudentID())  \n&& histRec.getAbsences() > mathRec.getAbsences())  \n{  \nnumStudents++;  \n}  \n}  \n}  \nreturn numStudents;}  \nNotes:  \n1. An ArrayList has the indexOf method. However, to use it here on mathList, we have to be sure that the CourseRecord class has the appropriate equals method defined. This is not guaranteed, so you might lose points if you use mathList’s indexOf. It is safer to use nested loops.  \nQuestion 4  \npublic int getPointsForRow(int targetRow)  \n{  \nboolean sameColor = true;  \nString prevColor = \"\"; 1  \nint points = 0;  \nfor (int col = 0; col \u003C board[targetRow] .length; col++){  \nSpace sp = board[targetRow][col];  \npoints += sp .getPoints();  \nString color = sp .getColor();  \nif (col > 0 && !color .equals(prevColor))  \nsameColor = false;  \nprevColor = color;  \n}  \nif (sameColor)  \npoints *= 2;  \nreturn points;  \n} 2  \nNotes:  \n1. Can be initialized to null or any String value.  \n2. Alternative solution, with a “for-each” loop:  \npublic int getPointsFo","cbCaihangTVIbkJC","https://ap.wps.com/l/cbCaihangTVIbkJC","pdf",177029,"English","# Chapter 5: Annotated Solutions to Past Free-Response Questions\n## Question 1\n### Part (a)\n### Part (b)\n## Question 2\n## Question 3\n## Question 4","[{\"question\":\"What does Chapter 5 include for the Java exam?\",\"answer\":\"It includes annotated solutions to past free-response questions, with Java code and explanatory notes for the required parts.\"},{\"question\":\"How does Question 1 handle the shortened username?\",\"answer\":\"It iteratively removes the section around the first hyphen using indexOf and substring operations, and it also presents a recursive helper method as an alternative.\"},{\"question\":\"What scoring logic is used in Question 4?\",\"answer\":\"The solution sums points across a target row and doubles the total when all spaces in the row share the same color.\"}]","Be Prepared - for the Computer Science Exam in Java - Chapter 5: Annotated Solutions to Past Free-Response Questions - 2026 | PDF",1790041881,18]