AP Computer Science Prinicples Lists & Reviews
Which of the following are benefits of using well-named variables in a computer program? Select two answers.
- the program will be easier for people to read - the program will be easier to modify in the future
Consider the following code segment, which uses the variables r, s, and t. r ← 1 s ← 2 t ← 3 r ← s s ← t DISPLAY (r) DISPLAY (s) What is displayed as a result of running the code segment?
2 3
Assume that both lists and strings are indexed starting with index 1. The list wordList has the following contents. ["abc", "def", "ghi", "jkl"] Let myWord be the element at index 3 of wordList. Let myChar be the character at index 2 of myWord. What is the value of myChar ?
"h"
A teacher is writing a code segment that will use variables to represent a student's name and whether or not the student is currently absent. Which of the following variables are most appropriate for the code segment?
A string variable named studentName and a Boolean variable named isAbsent
The variable isOpen is to be used to indicate whether or not a store is currently open. Which of the following is the most appropriate data type for isOpen ?
Boolean
The list listOne is a sorted list of numbers that contains 700 elements. The list listTwo is a sorted list of numbers that contains 900 elements. Let x represent the maximum number of list elements that will need to be examined when performing a binary search for a value in listOne, and let y represent the maximum number of list elements that will need to be examined when performing a binary search for a value in listTwo. Which of the following statements about x and y is true?
the value of x is approximately equal to the value of y
The following code segment is used to determine whether a customer is eligible for a discount on a movie ticket. val1 ← (NOT (category = "new")) OR (age ≥ 65) val2 ← (category = "new") AND (age < 12) If category is "new" and age is 20, what are the values of val1 and val2 as a result of executing the code segment?
val1 = false, val2 = false
Which of the following Boolean expressions are equivalent to the expression num >= 15 ? Select two answers.
- (num > 15) OR (num = 15) - NOT (num < 15)
Consider the following code segment. num1 <--- 6 num2 <--- 4 num3 <--- 10 IF num1 < num2 num1 <--- num2 ELSE num3 <--- num2 IF num2 >= num3 num1 <--- num2 + num3 sum <--- num1 + num2 + num3 What is the value of sum after the code segment is executed?
16
Consider the following code segment, which is intended to store ten consecutive even integers, beginning with 2, in the list evenList. Assume that evenList is initially empty. i ← 1 REPEAT 10 TIMES { <MISSING CODE> } Which of the following can be used to replace <MISSING CODE> so that the code segment works as intended?
APPEND(evenList, 2 * i) i <--- i + 1
A teacher stores the most recent quiz scores for her class in the list scores. The first element in the list holds the maximum possible number of points that can be awarded on the quiz, and each remaining element holds one student's quiz score. Assume that scores contains at least two elements. Which of the following code segments will set the variable found to true if at least one student scored the maximum possible number of points on the quiz and will set found to false otherwise?
len <--- LENGTH scores - 1 found <--- false index <--- 2 REPEAT len TIMES IF scoresindex = scores1 found <--- true index <--- index + 1
Two lists, list1 and list2, contain the names of books found in two different collections. A librarian wants to create newList, which will contain the names of all books found in either list, in alphabetical order, with duplicate entries removed. For example, if list1 contains ["Macbeth", "Frankenstein", "Jane Eyre"] and list2 contains ["Frankenstein", "Dracula", "Macbeth", "Hamlet"], then newList will contain ["Dracula", "Frankenstein", "Hamlet", "Jane Eyre", "Macbeth"]. The following procedures are available to create newList. - sort (list) = sort list in alphabetical order and returns the resulting list - combine (list1, list2) = creates a new list consisitng of the entries from list1 followed by the entries from list2. The resulting list is returned -removeDuplicates (list) = iterates through list. If any two or more entries have the same value, the duplicate entries are removed so that any entry appears at most once. The resulting list is returned. Which of the following code segments will correclty create newList?
newList ← Combine (list1, list2) newList ← Sort (newList) newList ← RemoveDuplicates (newList)
Consider the following procedures for string manipulation. concat(str1, str2) = Returns a single string consisting of str1 followed by str2. For example, concat("key", "board") returns "keyboard" reverse(str) = Returns the reverse of the string str. For example, reverse("abcd") returns "dcba". Which of the following code segments can be used to store "noon" in the string variable word ?
word ← "on" word←concat(reverse(word), word)
For which of the following lists can a binary search be used to search for an item in the list? 1. ["blue", "green", "jade", "mauve", "pink"] 2. [5, 5, 5, 5, 6, 7, 8, 8, 8] 3. [10, 5, 3, 2, -4, -8, -9, -12]
1, 2, 3 (in order for a binary search on a list to work, the list must be sorted; these are are sorted alphabetically, numerically, reverse order, etc)
The cost of a customer's electricity bill is based on the number of units of electricity the customer uses. - For the first 25 units of electricity, the cost is $5 per unit. - For units of electricity after the first 25, the cost is $7 per unit. Which of the following code segments correctly sets the value of the variable cost to the cost, in dollars, of using numUnits units of electricity?
IF numUnits <= 25 cost <--- numUnits * 5 ELSE cost <--- 25 * 5 + (numUnits -25) * 7
Which of the following is a benefit of using a list as a data abstraction in a program?
Lists often allow their size to be easily updated to hold as many data values as needed.
The code segment below uses the procedure IsFound (list, item), which returns true if item appears in list and returns false otherwise. The list resultList is initially empty. For EACH item IN inputList1 ( IF (IsFound (inputList2, item) ( APPEND (resultList, item) ) Which of the following best describes the contents of resultList after the code segment is executed?
Only elements that appear in both inputList1 and inputList2
Consider the following program, which is intended to display the number of times a number target appears in a list. count <--- 0 FOR EACH n IN list IF n = target count <--- count + 1 DISPLAY count Which of the following best describes the behavior of the program?
The program correctly displays the number of times target appears in the list.
Consider the following code segment. firstList ← ["guitar", "drums", "bass"] secondList ← ["flute", "violin"] thirdList ← [] thirdList ← firstList firstList ← secondList secondList ← thirdList What are the contents of secondList after the code segment is executed?
["guitar", "drums", "bass"]
Consider the following code segment. yourList <--- 20, 40, 60, 80 myList <--- 10, 30, 50, 70 yourList <--- myList What are the contents of yourList after the code segment is executed?
[10, 30, 50, 70]
Consider the following code segment first <--- true second <--- false second <--- first first <--- second What are the values of first and second as a result of executing the code segment?
first = true second = true
The variable age is to be used to represent a person's age, in years. Which of the following is the most appropriate data type for age ?
number
A summer camp offers a morning session and an afternoon session. The list morningList contains the names of all children attending the morning session, and the list afternoonList contains the names of all children attending the afternoon session. Only children who attend both sessions eat lunch at the camp. The camp director wants to create lunchList, which will contain the names of children attending both sessions. The following code segment is intended to create lunchList, which is initially empty. It uses the procedure IsFound (list, name), which returns true if name is found in list and returns false otherwise. FOR EACH child IN morningList ( <MISSING CODE> ) Which of the following could replace <MISSING CODE> so that the code segment works as intended?
IF (IsFound (afternoonList, child)) ( APPEND (luncList, child) )
A student is writing a program to model different real-world events using simulations. Which of the following simulations will generate a result that would best be stored using a Boolean variable?
a simulation of flipping a fair coin
Consider the following code segment. x ← 25 y ← 50 z ← 75 x ← y y ← z z ← x Which of the variables have the value 50 after executing the code segment?
x and z only