AP Computer Science Principles 1st Semester Finals Part Three
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"
x <- 0 result <- 0 repeat until x > 5 result <- result + x x <- x + 1 What is the value of the result after the code segment is executed?
15
The list wordList contains a list of 10 string values. Which of the following is a valid index for the list? -1 "hello" 2.5 4
4
In which of the following scenarios would a simulation be the LEAST beneficial?
A retail company wants to determine the most popular item that was sold on the company's Web site last month.
Which of the following statements describes a limitation of using a computer simulation to model a real-world object or system?
Computer simulations usually make some simplifying assumptions about the real-world object or system being modeled.
A car manufacturer uses simulation software during the design process for a new car. Which of the following are reasons to use simulation software in this context? I. Using simulation software can save the company money by helping to compare designs early in the process, before prototype cars are built. II. Using simulation software can help to identify safety issues by providing data about how different mechanical components will interact in a wide variety of situations. III. The manufacturer can present simulation software to customers to demonstrate different design possibilities.
I, II, and III
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. A four-line code segment reads as follows. Line 1: FOR EACH child IN morning List. Line 2: open brace. Line 3: open angle bracket, MISSING CODE, end angle bracket. Line 4: close brace. Which of the following could replace <MISSING CODE> so that the code segment works as intended?
IF (IsFound (afternoonList, child) ) { APPEND (lunchlist, child) }
The following grid contains a robot represented as a triangle, which is initially facing right. The following code segment is intended to move the robot to the gray square. <MISSING STATEMENT> { REPEAT 4 TIMES { MOVE_FORWARD( ) ROTATE_RIGHT( ) } ROTATE_LEFT( ) MOVE_FORWARD( ) ROTATE_RIGHT( ) } Which of the following can be used as a replacement for <MISSING STATEMENT> so that the code segment works as intended?
REPEAT 2 TIMES
theList ← [-2, -1, 0, 1, 2] count1 ← 0 count2 ← 0 FOR EACH value IN theList { IF(value > 0) { count1 ← count1 + 1 } ELSE { count2 ← count2 + 1 } } What are the values of count1 and count2 as a result of executing the code segment?
count1 = 2, count2 = 3
n the program below, y is a positive integer (e.g., 1, 2, 3, ...). result <- 0 repeat 3 times repeat y times result <- result + 1 What is the value of result after running the program?
y^3
The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the bottom-left square of the grid and facing toward the top of the grid. Code for the procedure Mystery is shown below. Assume that the parameter p has been assigned a positive integer value (e.g., 1, 2, 3, ...).
Bottom row, third from the right
Suppose a large group of people in a room were all born in the same year. Consider the following three algorithms, which are each intended to identify the people in the room who have the earliest birthday based on just the month and day. For example, a person born on February 10 is considered to have an earlier birthday than a person born on March 5. Which of the three algorithms will identify the correct people? II. All the people in the room stand up. All standing people form pairs with another standing person that they have not previously been paired with where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people in the pair remain standing. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing or all persons standing have the same birthday. Anyone still standing has the earliest birthday.
II only
In which of the following scenarios is using a simulation more beneficial than performing a calculation? Select two answers.
Investigating ways to reduce the amount of trash in the ocean Studying the effect of a genetic change in a population
A programmer has a need to round many numeric values to the nearest integer. Which of the following best explains the benefit of using a list as a data abstraction in this situation?
Keeping the numeric values in a list makes it easier to apply the same computation to every data element.
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]
a <- true b <- false c <- true repeat until a and b c <- not c b <- c display a display b display c What is displayed as a result of executing the code segment?
true true true
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)
Consider the following procedure. drawCircle(xPos, yPos, rad) <- Draws a circle on a coordinate grid with center (xPos, yPos) and radius rad The drawCircle procedure is to be used to draw the following figure on a coordinate grid. Which of the following code segments can be used to draw the figure? Select two answers.
x ← 4 y ← 1 r ← 0 REPEAT 3 TIMES { r ← r + 1 y ← y + 1 drawCircle(x, y, r) } x ← 4 y ← 4 r ← 3 REPEAT 3 TIMES { drawCircle(x, y, r) y ← y - 1 r ← r - 1 }
numList <- 100, 20, 300, 40, 500, 60 FOR EACH item IN numList IF (item >= 90) display item
5 3 1
For which of the following problems is using a simulation LEAST likely to be beneficial?
Determining the longest word in a textbook
The transportation department plans to build a new high-speed train route between two cities. The transportation department wants to implement a simulation of the train before any construction begins on this project. Which of the following statements is true about the use of a simulation for this project?
Using a simulation may expose potential safety issues that can be corrected before construction begins.
Consider the following code segment. j<- 1 repeat until <- <missing condition> j <- j + 2 Which of the following replacements for <MISSING CONDITION> will result in an infinite loop?
j = 6
The following grid contains a robot represented as a triangle. The robot is initially facing right. Which of the following code segments can be used to move the robot to the gray square along the path indicated by the arrows?
n <- 1 REPEAT 3 TIMES REPEAT n TIMES MOVE_FORWARD ROTATE_LEFT REPEAT n TIMES MOVE_FORWARD ROTATE_RIGHT n <- n + 1
Consider the following code segment. Assume that index1 is a number between 1 and LENGTH(theList), inclusive, and index2 is a number between 2 and LENGTH(theList) - 1, inclusive. theList ← [9, -1, 5, 2, 4, 8] x ← theList[index1] + theList[index2] What is the largest possible value that the variable x can have after the code segment executes?
14
A flowchart is a way to visually represent an algorithm. The flowchart below uses the following building blocks. Start -> count <- 1 -> count < 5 -> display count
5
An algorithm will be used to identify the maximum value in a list of one or more integers. Consider the two versions of the algorithm below. Algorithm I : Set the value of a variable max to − 1. Iterate through the list of integer values. If a data value is greater than the value of the variable max, set max to the data value. Algorithm II : Set the value of a variable max to the first data value. Iterate through the remaining values in the list of integers. If a data value is greater than the value of the variable max, set max to the data value. Which of the following statements best describes the behavior of the two algorithms?
Algorithm II always works correctly, but Algorithm I only works correctly when the maximum value is greater than or equal to − 1.
Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ? Select two answers.
-Step 1: Set x to 0. Step 2: Increment x by 1. Step 3: If x is not divisible by 3, then display x. Step 4: Repeat steps 2 and 3 until x is 20. -Step 1: Set x to 1. Step 2: If x is divisible by 3, then do nothing; otherwise display x. Step 3: Increment x by 1. Step 4: Repeat steps 2 and 3 until x is 20.
Which of the following is a benefit of using a simulation instead of an experiment to make a prediction? Select two answers.
A simulation allows investigation of a phenomenon without the real-world limitations on time, safety, or budget. A simulation can be used to model real-world events that are impractical for experiments.
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
There are 32 students standing in a classroom. Two different algorithms are given for finding the average height of the students. Algorithm A Step 1: All students stand. Step 2: A randomly selected student writes his or her height on a card and is seated. Step 3: A randomly selected standing student adds his or her height to the value on the card, records the new value on the card, and is seated. The previous value on the card is erased. Step 4: Repeat step 3 until no students remain standing. Step 5: The sum on the card is divided by 32. The result is given to the teacher. Algorithm B Step 1: All students stand. Step 2: Each student is given a card. Each student writes his or her height on the card. Step 3: Standing students form random pairs at the same time. Each pair adds the numbers written on their cards and writes the result on one student's card; the other student is seated. The previous value on the card is erased. Step 4: Repeat step 3 until one student remains standing. Step 5: The sum on the last student's card is divided by 32. The result is given to the teacher. Which of the following statements is true?
Both Algorithm A and Algorithm B always calculate the correct average.
Shoppers at a mall were asked whether they preferred wearing gloves or mittens in cold weather. Shoppers' preferences were stored in the list voteList as strings, with the string "Gloves" representing a preference for gloves and the string "Mittens" representing a preference for mittens. The following code segment is intended to traverse the list and display the number of shoppers who chose gloves and the number of shoppers who chose mittens. numGlovesVotes ← 0 numMittensVotes ← 0 <MISSING CODE> { IF(vote = "Gloves") { numGlovesVotes ← numGlovesVotes + 1 } ELSE { numMittensVotes ← numMittensVotes + 1 } } DISPLAY(numGlovesVotes) DISPLAY(" shoppers chose gloves and") DISPLAY(numMittensVotes) DISPLAY(" shoppers chose mittens.") Which of the following should replace <MISSING CODE> so that the code segment works as intended?
FOR EACH vote IN voteList
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 following procedure is intended to return the number of times the value val appears in the list myList. The procedure does not work as intended. Line 01: PROCEDURE countNumOccurences(myList, val) Line 02: { Line 03: FOR EACH item IN myList Line 04: { Line 05: count ← 0 Line 06: IF(item = val) Line 07: { Line 08: count ← count + 1 Line 09: } Line 10: } Line 11: RETURN(count) Line 12: } Which of the following changes can be made so that the procedure will work as intended?
Moving the statement in line 5 so that it appears between lines 2 and 3
i <- 0 sum <- 0 Repeat until i=4 i <- 1 sum <- sum + 1 i <- i + 1 display sum Which of the following best describes the result of running the program code?
Nothing is displayed; the program results in an infinite loop.
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. 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
The following grid contains a robot represented as a triangle, which is initially facing toward the top of the grid. The robot can move into a white or gray square but cannot move into a black region. Which of the following code segments can be used to move the robot to the gray square?
REPEAT 3 TIMES { MOVE_FORWARD( ) } ROTATE_LEFT( ) REPEAT 2 TIMES { MOVE_FORWARD( ) } ROTATE_RIGHT( ) REPEAT 3 TIMES { MOVE_FORWARD( ) }
A list of numbers has n elements, indexed from 1 to n. The following algorithm is intended to display the number of elements in the list that have a value greater than 100. The algorithm uses the variables count and position. Steps 3 and 4 are missing. Step 1 Set count to 0 and position to 1. Step 2 If the value of the element at index position is greater than 100, increase the value of count by 1. Step 3 (missing step) Step 4 (missing step) Step 5 Display the value of count. Which of the following could be used to replace steps 3 and 4 so that the algorithm works as intended?
Step 3 Increase the value of position by 1. Step 4 Repeat steps 2 and 3 until the value of position is greater than n.
A city planner is using simulation software to study crowd flow out of a large arena after an event has ended. The arena is located in an urban city. Which of the following best describes a limitation of using a simulation for this purpose?
The model used by the simulation software often omits details so that it is easier to implement.
A new bank plans to make customer convenience a priority by minimizing the amount of time a customer waits in line. The bank is considering two options: a single line where the customer at the front waits for the next available teller, or separate lines for each teller. The bank decides to use a computer simulation of these two options to determine the average wait time for customers. Which of the following is NOT true about the bank's plan?
The simulation will not produce usable results because actual customer data are not available.
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"]
The algorithm below is used to simulate the results of flipping a coin 4 times. Consider the goal of determining whether the simulation resulted in an equal number of heads and tails. Step 1: Initialize the variables heads_counter and flip_counter to 0. Step 2: A variable coin_flip is randomly assigned a value of either 0 or 1. If coin_flip has the value 0, the coin flip result is heads, so heads_counter is incremented by 1. Step 3: Increment the value of flip_counter by 1. Step 4: Repeat steps 2 and 3 until flip_counter equals 4. Following the execution of the algorithm, which of the following expressions indicates that the simulation resulted in an equal number of heads and tails?
heads_counter = 2
concat(str1, str2) <- Returns a single string consisting of str1 followed by str2. For example, concat("key", "board") returns "keyboard". prefix(str, length) <- Returns the first length characters of str or str if length is greater than the number of characters in str. For example, prefix("delivery", 3) returns "del" and prefix("delivery", 100) returns "delivery". The variable initials is to be assigned a string consisting of the first letter of the string firstName followed by the first letter of the string lastName. Which of the following assigns the correct string to initials?
initials ← concat(prefix(firstName, 1), prefix(lastName, 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
concat(str1, str2) <- Returns a single string consisting of str1 followed by str2. For example, concat("key", "board") returns "keyboard". substring(str, start, length) <- Returns a substring of consecutive characters from str, starting with the character at position start and containing length characters. The first character of str is located at position 1. For example, substring("delivery", 3, 4) returns "live". len(str) <- Returns the number of characters in str. For example, len("pizza") returns 5. Assume that the string oldString contains at least 4 characters. A programmer is writing a code segment that is intended to remove the first two characters and the last two characters from oldString and assign the result to newString. For example, if oldString contains "student", then newString should contain "ude". Which of the following code segments can be used to assign the intended string to newString ? Select two answers.
newString ←← substring(oldString, 3, len(oldString) - 2) tempString ←← substring(oldString, 3, len(oldString) - 2) newString ←← substring(tempString, 1, len(tempString) - 2)