AP Review Set 7

pdf

School

Rumson Fair Haven Reg H *

*We aren’t endorsed by this school

Course

301

Subject

Computer Science

Date

Nov 24, 2024

Type

pdf

Pages

13

Uploaded by CoachRiverTiger30

Report
Computer Science A m 17. A bear is an animal and a zoo contains many animals, including bears. Three classes Animal, Bear, and Zoo are declared to represent animal, bear, and zoo objects. Which of the following is the most appropriate set of declarations? (A) public class Animal extends Bear { } public class Zoo { private Animal[] myAnimals; } (B) public class Bear extends Animal { } public class Zoo { private Animal([] myAnimals; } (C) public class Animal extends Zoo [ private Bear myBear; } (D) public class Bear extends Animal, Zoo { } (E) public class Bear extends Animal implements Zoo { } 14. Consider the following declarations. int valueOne, valueTwo; Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value? (A) valueOne.equals ( (Object) valueTwo) (B) valueOne == valueTwo (C) valueOne.compareTo ( (Object) valueTwo) == 0 (D) valueOne.compareTo (valueTwo) == 0 (E) valueOne.equals (valueTwo) Unauthorized copying or reuse of| any part of this page is illegal. 21 GO ON TO THE NEXT PAGE.
Computer Science A 19. Consider the following two static methods, where £2 is intended to be the iterative version of £1. public static int fl(int n) { if (n < 0) { return 0; b else { return (fl(n - 1) + n * 10); } } public static int £2(int n) { int answer = 0; while (n > 0) { answer = answer + n * 10; n--; } return answer; } The method £2 will always produce the same results as £1 under which of the following conditions? ILn<O ILn=0 ILn>0 (A) Tonly (B) I only (C) 1T only (D) II and 11T only (B) L 1L, and 11T Unauthorized copying or reuse of any part of this page is illegal. 23 GO ON TO THE NEXT PAGE.
Computer Science A 27. Assume that class Vehicle contains the following method. public void setPrice(double price) { /* implementation not shown */ } Also assume that class Car extends Vehicle and contains the following method. public void setPrice(double price) { /* implementation not shown */ } Assume Vehicle v is initialized as follows. Vehicle v = new Car(); v.setPrice(1000.0); Which of the following is true? (A) The code above will cause a compile-time error, because a subclass cannot have a method with the same name and the same signature as its superclass. (B) The code above will cause a run-time error, because a subclass cannot have a method with the same name and the same signature as its superclass. (C) The code above will cause a compile-time error because of type mismatch. (D) The code v.setPrice(1000.0); will cause the setPrice method of the Car class to be called. (E) Thecode v.setPrice(1000.0); willcausethe setPrice method of the Vehicle class to be called. Unauthorized copying or reuse of any part of this page Is lllegal. 30 GO ON TO THE NEXT PAGE.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
: vComputer Science A m Questions 30-31 are based on the following incomplete declaration of the class BoundedIntArray and its constructor definitions. A BoundedIntArray represents an indexed list of integers. Ina BoundedIntArray the user can specify a size, in which case the indices range from 0 to size - 1. The user can also specify the lowest index, low, in which case the indices can range from low to low + size - 1. public class BoundedIntArray { private int[] myItems; // storage for the list private int myLowIndex; // lowest index public BoundedIntArray(int size) { myItems = new int[size]; myLowIndex = 0; } public BoundedIntArray(int size, int low) { myItems = new int[size]; myLowIndex = low; } // other methods not shown 30. Which of the following is the best reason for declaring the data fields myItems and myLowIndex to be private rather than public? (A) This permits BoundedIntArray objects to be initialized and modified. (B) This permits BoundedIntArray methods to be written and tested before code that uses a BoundedIntArray is written. (C) This helps to prevent clients of the BoundedIntArray class from writing code that would need to be modified if the implementation of BoundedIntArray were changed. (D) This prevents compile-time errors whenever public methods are called that access the private data fields. (E) This prevents run-time errors whenever public methods are called that access the private data fields. Unauthorized copying or reuse of any part of this page Is illegal. 33 GO ON TO THE NEXT PAGE.
Computer Science A Questions 38-39 refer to the following information. Consider the following data field and method. The method removeDups is intended to remove all adjacent duplicate numbers from myData, but does not work as intended. private ArrayList myData; public void removeDups () { int k = 1; while (k < myData.size()) { if (myData.get(k).equals(myData.get(k - 1))) { myData.remove (k) ; } k++; } } For example, if myData hasthe values 3 3 4 4 4 8 7 7 7, aftercalling removeDups, myData should have the values 3 4 8 7. 39. Which of the following best describes how to fix the error so that removeDups works as intended? (A) k should be initialized to 0 at the beginning of the method. (B) The while condition should be (k < myData.size() - 1). (C) The if testshould be (myData.get (k).equals(myData.get(k + 1))). (D) The body of the if statement should be: myData.remove(k - 1); (E) There should be an else before the statement k++; Unauthorized copying or reuse of ¢ [any part of this page Is illeg: 41 GO ON TO THE NEXT PAGE.
2008 AP° COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS COMPUTER SCIENCE A SECTION II Time—1 hour and 45 minutes Number of questions—4 Percent of total grade—50 Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Notes: ° 1. Assume that the classes listed in the Quick Reference found in the Appendix have been imported where appropriate. Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods may not receive full credit. A travel agency maintains a list of information about airline flights. Flight information includes a departure time and an arrival time. You may assume that the two times occur on the same day. These times are represented by objects of the Time class. The declaration for the Time class is shown below. It includes a method minutesUntil that returns the difference (in minutes) between the current Time object and another Time object. public class Time { /** @return difference, in minutes, between this time and other; * difference is negative if other is earlier than this time i/ public int minutesUntil (Time other) { /* implementation not shown */ '} // There may be instance variables, constructors, and methods that are not shown. } For example, assume that t1 and t2 are Time objects where t1 represents 1:00 P.M. and t2 represents 2:15 P.M. The call t1.minutesUntil (t2) willreturn 75 and the call t2.minutesUntil(tl) will return -75. © 2008 The College Board. All rights reserved. Visit apcentral collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. .2-
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2008 AP° COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS The declaration for the Flight class is shown below. It has methods to access the departure time and the arrival time of a flight. You may assume that the departure time of a flight is earlier than its arrival time. public class Flight & /** @return time at which the flight departs % public Time getDepartureTime () { /* implementation not shown */ } /** @return time at which the flight arrives *p public Time getArrivalTime () { /* implementation not shown */ } // There may be instance variables, constructors, and methods that are not shown. } ©2008 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and wiww.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. _3-
2008 AP® COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS A trip consists of a sequence of flights and is represented by the Trip class. The Trip class contains an ArrayList of Flight objects that are stored in chronological order. You may assume that for each flight after the first flight in the list, the departure time of the flight is later than the arrival time of the preceding flight in the list. A partial declaration of the Trip class is shown below. You will write two methods for the Trip class. public class Trip { private ArrayList<Flight> flights; // stores the flights (if any) in chronological order /** @return the number of minutes from the departure of the first flight to the arrival * of the last flight if there are one or more flights in the trip; * 0, if there are no flights in the trip */ public int getDuration() { /* tobeimplemented in part (a) */ } /** Precondition: the departure time for each flight is later than the arrival time of its * preceding flight * @return the smallest number of minutes between the arrival of a flight and the departure * of the flight immediately after it, if there are two or more flights in the trip; * -1, if there are fewer than two flights in the trip */ public int getShortestLayover () { /* tobe implemented in part (b) */ } // There may be instance variables, constructors, and methods that are not shown. } © 2008 The College Board. Al rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. 4-
2008 AP° COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS (a) Complete method getDuration below. /** @return the number of minutes from the departure of the first flight to the arrival 3 of the last flight if there are one or more flights in the trip; # 0, if there are no flights in the trip /4 public int getDuration() (b) Write the Trip method getShortestLayover. A layover is the number of minutes from the arrival of one flight in a trip to the departure of the flight immediately after it. If there are two or more flights in the trip, the method should return the shortest layover of the trip; otherwise, it should return -1. For example, assume that the instance variable flights ofa Trip object vacation contains the following flight information. Departure Arrival Time Time Flight 0 11:30 am, | 12:15 p.m. Flight 1 L15pm. | 3:45p.m. Flight 2 4:00 p.m. 6:45 p.m. Flight 3 10:15 p.m. | 11:00 p.m. Layover (minutes) } 60 1oIs 1210 The call vacation.getShortestLayover () should return 15. Complete method getShortestLayover below. /** Precondition: the departure time for each flight is later than the arrival time of its # preceding flight * @return the smallest number of minutes between the arrival of a flight and the departure * of the flight immediately after it, if there are two or more flights in the trip; * -1, if there are fewer than two flights in the trip L public int getShortestLayover () © 2008 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for students and parents). _5- GO ON TO THE NEXT PAGE.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2 2008 AP®° COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS Consider a method of encoding and decoding words that is based on a master string. This master string will contain all the letters of the alphabet, some possibly more than once. An example of a master string is "sixtyzipperswerequicklypickedfromthewovenjutebag”. This string and its indexes are shown below. 9eeia0 [ 11 32 43 [14505: 16 17 18 19 20 21 22 23 0 1 2 3 4 5 i [sTifxTe[v[zlifplplelr|sJwlelr]elaluli[clk[i]v]p 24 25 26 27 28 29 30 31 32 33 34 35 36[37 38 39]40 41 42 43 44 45 | 46 47 i [c[xfeTdle[r{ofm [t h[e[wlo[v]e[n]jJultlelblalg An encoded string is defined by a list of string parts. A string part is defined by its starting index in the master string and its length. For example, the string "overeager" is encoded as the list of string parts [ (37,3), (14, 2), (46,2),(9,2) ] denoting the substrings "ove", "re", "ag",and "er". String parts will be represented by the StringPart class shown below. public class StringPart t /** @param start the starting position of the substring in a master string * @param length the length of the substring in a master string i | public StringPart(int start, int length) { /* implementation not shown */ } /** @return the starting position of the substring in a master string 2 public int getStart() { /* implementation not shown */ } /** @return the length of the substring in a master string it} public int getLength() { /* implementation not shown */ } // There may be instance variables, constructors, and methods that are not shown. © 2008 The College Board. Al rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and wiww.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. -6-
2008 AP® COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS The class StringCoder provides methods to encode and decode words using a given master string. When encoding, there may be multiple matching string parts of the master string. The helper method findpPart is provided to choose a string part within the master string that matches the beginning of a given string. public class StringCoder { private String masterString; /** @param master the master string for the StringCoder * Precondition: the master string contains all the letters of the alphabet */ public StringCoder (String master) { masterString = master; } /** @param parts an ArrayList of string parts that are valid in the master string # Precondition: parts.size() >0 * @return the string obtained by concatenating the parts of the master string */ public String decodeString (ArrayList<StringPart> parts) { /* tobeimplemented in part (a) */ } /** @param str the string to encode using the master string * Precondition: all of the characters in stxr appear in the master string; * str.length() >0 * @return astring part in the master string that matches the beginning of str. ¥ The returned string part has length at least 1. 4 private StringPart findPart (String str) { /* implementation not shown */ } /** @param word the string to be encoded ¥ Precondition: all of the characters in word appear in the master string; L] word.length() >0 * @return an ArrayList of string parts of the master string that can be combined ] to create word %, public ArrayList<StringPart> encodeString(String word) { /* tobeimplemented in part (b) */ } // There may be instance variables, constructors, and methods that are not shown. © 2008 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and wwiw.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. 7
2008 AP° COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS (a) Write the StringCoder method decodeString. This method retrieves the substrings in the master string represented by each of the StringPart objectsin parts, concatenates them in the order in which they appear in parts, and returns the result. Complete method decodeString below. /** @param parts an ArrayList of string parts that are valid in the master string * Precondition: parts.size() >0 * @return the string obtained by concatenating the parts of the master string #/ public String decodeString (ArrayList<StringPart> parts) © 2008 The College Board, Al rights reserved. Visit apeentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. Ba
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2008 AP® COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS (b) Write the StringCoder method encodeString. A string is encoded by determining the substrings in the master string that can be combined to generate the given string. The encoding starts with a string part that matches the beginning of the word, followed by a string part that matches the beginning of the rest of the word, and so on. The string parts are returned in an array list in the order in which they appear in word. The helper method £indPart must be used to choose matching string parts in the master string. Complete method encodeString below. /** @param word the string to be encoded * Precondition: all of the characters in word appear in the master string; = word.length() >0 * @return an ArrayList of string parts of the master string that can be combined # to create word i | public ArrayList<StringPart> encodeString(String word) © 2008 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for students and parents). GO ON TO THE NEXT PAGE. -9-