




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
2020 Exam Sample Questions 1 and 2 with solutions.
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!
®
(Adapted from: AP®^ Computer Science A Course and Exam Description) Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Notes:
Allotted time: 25 minutes (plus 5 minutes to submit) The Gizmo class represents gadgets that people purchase. Some Gizmo objects are electronic and others are not. A partial definition of the Gizmo class is shown below. public class Gizmo { /** Returns the name of the manufacturer of this Gizmo. / public String getMaker() { / implementation not shown / } /* Returns true if this Gizmo is electronic, and false otherwise. / public boolean isElectronic() { / implementation not shown / } /* Returns true if this Gizmo is equivalent to the Gizmo object
(a) Write the countElectronicsByMaker method. The method examines the ArrayList instance variable purchases to determine how many Gizmo objects purchased are electronic and are manufactured by maker. Assume that the OnlinePurchaseManager object opm has been declared and initialized so that the ArrayList purchases contains Gizmo objects as represented in the following table. Index in purchases 0 1 2 3 4 5 Value returned by method call isElectronic() true false true false true (^) false Value returned by method call getMaker() “ABC” “ABC” “XYZ” “lmnop” “ABC” “ABC” The following table shows the value returned by some calls to countElectronicsByMaker. Method Call Return Value opm.countElectronicsByMaker(“ABC”) 2 opm.countElectronicsByMaker(“lmnop”) 0 opm.countElectronicsByMaker(“XYZ”) 1 opm.countElectronicsByMaker(“QRP”) 0 Complete method countElectronicsByMaker below. /** Returns the number of purchased Gizmo objects that are electronic and
(c) A programmer would like to add a method getCheapestGizmoByMaker, which returns the least expensive Gizmo purchased by an individual from a given maker. Write a description of how you would change the Gizmo and OnlinePurchaseManager classes in order to support this modification. Make sure to include the following in your response. ཛ (^) Write the method header for the getCheapestGizmoByMaker method. ཛ (^) Identify any new or modified variables, constructors, or methods aside from the getCheapestGizmoByMaker method. Do not write the program code for this change. ཛ (^) Describe, for each new or revised variable, constructor, or method, how it would change or be implemented, including visibility and type. You do not need to describe the getCheapestGizmoByMaker method. Do not write the program code for this change.
(a) Write the isValid method. The method returns true if its parameter numWithCheckDigit, which represents a number containing a check digit, is valid, and false otherwise. The check digit is always the rightmost digit of numWithCheckDigit. The following table shows some examples of the use of isValid. Method Call Return Value Explanation getCheck(159) 2 The check digit for 159 is 2. isValid(1592) true The number 1592 is a valid combination of a number ( 159 ) and its check digit ( 2 ). isValid(1593) false The number 1593 is not a valid combination of a number ( 159 ) and its check digit ( 3 ) because 2 is the check digit for 159. Complete method isValid below. You must use getCheck appropriately to receive full credit. /** Returns true if numWithCheckDigit is valid, or false
A single paragraph (2 – 4 sentences) may be sufficient for answering these questions. The provided bullets in the question are meant to guide your single paragraph response; it is not necessary to provide a separate response for each bullet. The following verbs are in addition to the Task Verbs in Free-Response Questions on page 190 of the AP Computer Science A Course and Exam Description :