AP Computer Science Principles - Semester 1 Final Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

B

Three words are stored in the variables word1, word2, and word3. The values of the variables are to be updated as shown in the following table. VariableValue BeforeUpdatingValue AfterUpdatingword1"xylophone""zebra"word2"yarn""yarn"word3"zebra""xylophone" Which of the following code segments can be used to update the values of the variables as shown in the table? A temp ← word1 word3 ← word1 word1 ← temp B temp ← word1 word1 ← word3 word3 ← temp C temp ← word1 word1 ← word2 word2 ← word3 word3 ← temp D temp ← word3 word3 ← word2 word2 ← word1 word1 ← temp

B

To attend a particular camp, a student must be either at least 13 years old or in grade 9 or higher, but must not yet be 18 years old. Let age represent a student's age and let grade represent the student's grade level. Which of the following expressions evaluates to true if the student is eligible to attend the camp and evaluates to false otherwise? A ((age ≥ 13) OR (grade ≥ 9)) AND (age ≤ 18) B ((age ≥ 13) OR (grade ≥ 9)) AND (age < 18) C ((age ≥ 13) OR (grade ≥ 9)) OR (age ≤ 18) D ((age ≥ 13) OR (grade ≥ 9)) OR (age < 18)

D

hich of the following code segments can be used to interchange the values of the variables num1 and num2 ? A num1 <- num2 num2 <- num1 B temp <- num1 num1 <- temp num2 <- num1 C temp <- num1 num2 <- num1 num1 <- temp D temp <- num1 num1 <- num2 num2 <- temp

C

To qualify for a particular scholarship, a student must have an overall grade point average of 3.0 or above and must have a science grade point average of over 3.2. Let overallGPA represent a student's overall grade point average and let scienceGPA represent the student's science grade point average. Which of the following expressions evaluates to true if the student is eligible for the scholarship and evaluates to false otherwise? A (overallGPA > 3.0) AND (scienceGPA > 3.2) B (overallGPA > 3.0) AND (scienceGPA ≥ 3.2) C (overallGPA ≥ 3.0) AND (scienceGPA > 3.2) D (overallGPA ≥ 3.0) AND (scienceGPA ≥ 3.2)

B

Consider the following code segment with an integer variable num. IF(num > 0) { DISPLAY("positive") } IF(num < 0) { DISPLAY("negative") } IF(num = 0) { DISPLAY("zero") } Which of the following code segments is equivalent to the code segment above? A IF(num < 0) { DISPLAY("negative") } ELSE { DISPLAY("positive") } IF(num = 0) { DISPLAY("zero") } B IF(num < 0) { DISPLAY("negative") } ELSE { IF(num = 0) { DISPLAY("zero") } ELSE { DISPLAY("positive") } } C IF(num ≤ 0) { DISPLAY("negative") } ELSE { IF(num = 0) { DISPLAY("zero") } ELSE { DISPLAY("positive") } } D IF(num ≤ 0) { DISPLAY("negative") } IF(num = 0) { DISPLAY("zero") } ELSE { DISPLAY("positive") }

C

Consider the following code segment with integer variables x and y. IF x > 10 IF y < 10 DISPLAY "ONE" ELSE DISPLAY "TWO" ELSE IF y > 3 DISPLAY "THREE" ELSE DISPLAY "FOUR" If x has a value of 7 and y has a value of 20, what is displayed as a result of executing the code segment? A ONE B TWO C THREE D FOUR

A

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? A The value of first is true, and the value of second is true. B The value of first is true, and the value of second is false. C The value of first is false, and the value of second is true. D The value of first is false, and the value of second is false.

B

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? A [] B ["guitar", "drums", "bass"] C ["flute", "violin"] D ["flute", "violin", "guitar", "drums", "bass"]

B

Consider the following code segment. p <- 10 q <- 20 r <- 30 s <- 40 p <- q q <- r s <- q r <- p What is the value of r as a result of executing the code segment? A 10 B 20 C 30 D 40

D

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? A val1 = true, val2 = true B val1 = true, val2 = false C val1 = false, val2 = true D val1 = false, val2 = false

D

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? A REPEAT 1 TIMES B REPEAT 2 TIMES C REPEAT 3 TIMES D REPEAT 4 TIMES

C

The following procedure is intended to return true if the list of numbers myList contains only positive numbers and is intended to return false otherwise. The procedure does not work as intended. PROCEDURE allPositive(myList) { index ← 1 len ← LENGTH(myList) REPEAT len TIMES { IF(myList[index] > 0) { RETURN(true) } index ← index + 1 } RETURN(false) } For which of the following contents of myList does the procedure NOT return the intended result? A [-3, -2, -1] B [-2, -1, 0] C [-1, 0, 1] D [1, 2, 3]

C

Consider the following code segment, where exam and presentation are integer variables and grade is a string variable. IF((exam > 90) AND (presentation > 80)) { grade ← "A" } IF((exam > 80) OR (presentation > 75)) { grade ← "B" } ELSE { IF((exam > 70) OR (presentation > 60)) { grade ← "C" } ELSE { IF(exam > 60) { grade ← "D" } ELSE { grade ← "F" } } } Under which of the following conditions will the value "C" be assigned to the variable grade ? A When the value of exam is 70 and the value of presentation is 50 B When the value of exam is 70 and the value of presentation is 80 C When the value of exam is 80 and the value of presentation is 60 D When the value of exam is 80 and the value of presentation is 80

C

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? A APPEND(evenList, i) i ← i + 2 B i ← i + 2 APPEND(evenList, i) C APPEND(evenList, 2 * i) i ← i + 1 D i ← i + 1 APPEND(evenList, 2 * i)

D

Consider the following code segment. a <- 10 b <- 20 c <- 30 d <- 40 x <- 20 b <- x + b a <- x + 1 d <- c + d/2 DISPLAY a DISPLAY b DISPLAY c DISPLAY d What is displayed as a result of executing the code segment? A 10 20 30 40 B 21 30 40 50 C 21 40 30 40 D 21 40 30 50

D

Consider the following code segment. 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? A true false false B true false true C true true false D true true true

B

Consider the following code segment. a <- true b <- false c <- true a <- NOT a OR b and c c <- c AND s DISPLAY a DISPLAY b DISPLAY c What is displayed as a result of executing the code segment? A true true true B false false false C true false true D false false true

D

Consider the following code segment. i <- 1 REPEAT UNTIL i > 4 rand <- RANDOM [1, i] DISPLAY [rand] i <- i + 1 Which of the following CANNOT be displayed as a result of executing the code segment? A 1 1 1 1 B 1 2 3 2 C 1 2 3 4 D 1 3 2 4

C

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 <- num2 + num3 What is the value of sum after the code segment is executed? A 12 B 14 C 16 D 18

B

A certain game keeps track of the maximum and minimum scores obtained so far. If num represents the most recent score obtained, which of the following algorithms correctly updates the values of the maximum and the minimum? A If num is greater than the minimum, set the minimum equal to num. Otherwise, if num is greater than the maximum, set the maximum equal to num. B If num is less than the minimum, set the minimum equal to num. Otherwise, if num is greater than the maximum, set the maximum equal to num. C If num is less than the minimum, set the minimum equal to num. Otherwise, if num is less than the maximum, set the maximum equal to num. D If num is greater than the minimum, set the minimum equal to num. Otherwise, if num is less than the maximum, set the maximum equal to num.

C

Consider the 4-bit binary numbers 0011, 0110, and 1111. Which of the following decimal values is NOT equal to one of these binary numbers? A 3 B 6 C 9 D 15

c

Consider the following code segment. numList <- 100, 20, 300, 40, 500, 60 FOR EACH item IN numList IF item >= 90 DISPLAY [item] What is displayed as a result of executing the code segment? A 1 3 5 B 5 3 1 C 100 300 500 D 500 300 100

C

Consider the following code segment. x <- 0 result <- 0 REPEAT UNTIL x > 5 result <- result + x x <- x + 1 What is the value of result after the code segment is executed? A 6 B 10 C 15 D 21

C

Consider the following code segment. x <- 23 z <- x MOD y Which of the following initial values of the variable y would result in the variable z being set to 2 after the code segment is executed? A 1 B 2 C 3 D 4

A

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? A [10, 30, 50, 70] B [20, 40, 60, 80] C [10, 30, 50, 70, 20, 40, 60, 80] D [20, 40, 60, 80, 10, 30, 50, 70]

A

Consider the following code segment. IF x > y DISPLAY x + y ELSE DISPLAY x - y If the value of x is 3 and the value of y is 5, what is displayed as a result of executing the code segment? A -2 B 2 C 8 D Nothing will be displayed.

C

A certain programming language uses 4-bit binary sequences to represent nonnegative integers. For example, the binary sequence 0101 represents the corresponding decimal value 5. Using this programming language, a programmer attempts to add the decimal values 14 and 15 and assign the sum to the variable total. Which of the following best describes the result of this operation? A The correct sum of 29 will be assigned to the variable total. B An overflow error will occur because 4 bits is not large enough to represent either of the values 14 or 15. C An overflow error will occur because 4 bits is not large enough to represent 29, the sum of 14 and 15. D A round-off error will occur because the decimal values 14 and 15 are represented as approximations due to the fixed number of bits used to represent numbers.

A

A company that develops educational software wants to assemble a collaborative team of developers from a variety of professional and cultural backgrounds. Which of the following is NOT considered a benefit of assembling such a team? A Collaboration that includes diverse backgrounds and perspectives can eliminate the need for software testing. B Collaboration that includes diverse backgrounds and perspectives can help the team anticipate the needs of a variety of software users. C Collaboration that includes diverse backgrounds and perspectives can help the team avoid bias. D Collaboration that includes diverse backgrounds and perspectives can reflect the strengths of the individual team members.

D

A company that develops mobile applications wants to involve users in the software development process. Which of the following best explains the benefit in having users participate? A Users can identify and correct errors they encounter when using released versions of the software. B Users can review the algorithms used in the software to help improve their efficiency. C Users can provide documentation for program code at the end of the software development process. D Users can provide feedback that can be used to incorporate a variety of perspectives into the software.

B

A homework assignment consists of 10 questions. The assignment is graded as follows. Number of Correct Answers Grade9-10check plus7-8checkUnder 7check minus Let numCorrect represent the number of correct answers for a particular student. The following code segment is intended to display the appropriate grade based on numCorrect. The code segment does not work as intended in all cases. IF numCorrect > 7 IF numCorrect >= 9 DISPLAY "check plus" ELSE DISPLAY "check minus" ELSE DISPLAY "check" For which of the following values of numCorrect does the code segment NOT display the intended grade? Select two answers. A 9 B 8 C 7 D 6

D

A numeric test score is to be converted to a letter grade of A, B, or C according to the following rules: A score greater than 90 is considered an A; a score between 80 and 90, inclusive, is considered a B; and any other score is considered a C. Which of the following code segments will assign the correct letter grade to grade based on the value of the variable score ? I. grade <- "C" IF score > 90 grade <- "A" ELSE grade <- "B" II. IF score > 90 grade <- "A" ELSE IF score >= 80 grade <- "B" ELSE grade <- "C" III. IF score < 80 grade <- "C" ELSE IF score =< 90 grade <- "B" ELSE grade <- "A"

B

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? A Keeping the numeric values in a list makes it easier to round a number to the nearest integer. B Keeping the numeric values in a list makes it easier to apply the same computation to every data element. C Keeping the numeric values in a list makes it easier to prevent a program from unintentionally changing the value of a variable. D Keeping the numeric values in a list makes it easier to prevent a program from attempting to access an index beyond the length of the list.

D

A programmer is creating an algorithm that will be used to turn on the motor to open the gate in a parking garage. The specifications for the algorithm are as follows. The gate should not open when the time is outside of business hours. The motor should not turn on unless the gate sensor is activated. The motor should not turn on if the gate is already open. Which of the following algorithms can be used to open the gate under the appropriate conditions? A Check if the time is outside of business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is closed. If it is, turn on the motor. B Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is open. If it is, turn on the motor. C Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is not, check if the gate is open. If it is not, turn on the motor. D Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is open. If it is not, turn on the motor.

A

A spinner contains 12 regions of equal size. The regions are numbered 1 to 12. Which of the following code segments can be used to simulate the results of spinning the spinner three times and assigns the sum of the values obtained by the three spins to the variable sum ? A sum ← RANDOM(1, 12) + RANDOM(1, 12) + RANDOM(1, 12) B sum ← RANDOM(1, 36) C sum ← 3 * RANDOM(1, 12) D sum ← 12 * RANDOM(1, 3)

B

A student is creating an algorithm to display the distance between the numbers num1 and num2 on a number line. The following table shows the distance for several different values. Value of num1Value of num2Distance Between num1 and num2523187-347 Which of the following algorithms displays the correct distance for all possible values of num1 and num2 ? A Step 1:Add num1 and num2 and store the result in the variable sum. Step 2: Take the absolute value of sum and display the result. B Step 1:Subtract num1 from num2 and store the result in the variable diff.Step 2:Take the absolute value of diff and display the result. C Step 1:Take the absolute value of num1 and store it in the variable absNum1.Step 2:Take the absolute value of num2 and store it in the variable absNum2.Step 3:Add absNum1 and absNum2 and display the result. D Step 1:Take the absolute value of num1 and store it in the variable absNum1.Step 2:Take the absolute value of num2 and store it in the variable absNum2.Step 3:Subtract absNum1 from absNum2 and display the result.

B

A student is creating an application that allows customers to order food for delivery from a local restaurant. Which of the following is LEAST likely to be an input provided by a customer using the application? A The address where the order should be delivered B The cost of a food item currently available for order C The credit card or payment information for the purchaser D The name of a food item to be included in the delivery

B

A student wrote the following code segment, which displays true if the list myList contains any duplicate values and displays false otherwise. containsDuplicates <- false j <- 1 REPEAT UNTIL j > LENGTH [myList] -1 k <- j + 1 REPEAT UNTIL k > LENGTH [myList] IF myList j = myList k containsDuplicates <- true k <- k + 1 j <- j + 1 DISPLAY containsDuplicates The code segment compares pairs of list elements, setting containsDuplicates to true if any two elements are found to be equal in value. Which of the following best describes the behavior of how pairs of elements are compared? A The code segment iterates through myList, comparing each element to all other elements in the list. B The code segment iterates through myList, comparing each element to all subsequent elements in the list. C The code segment iterates through myList, comparing each element to the element that immediately follows it in the list. D The code segment iterates through myList, comparing each element to the element that immediately precedes it in the list.

D

A teacher has a goal of displaying the names of 2 students selected at random from a group of 30 students in a classroom. Any possible pair of students should be equally likely to be selected. Which of the following algorithms can be used to accomplish the teacher's goal? A Step 1:Assign each student a unique integer from 1 to 30.Step 2:Generate a random integer n from 1 to 15.Step 3:Select the student who is currently assigned integer n and display the student's name.Step 4:Generate a new random integer n from 16 to 30.Step 5:Select the student who is currently assigned integer n and display the student's name. B Step 1:Assign each student a unique integer from 1 to 30.Step 2:Generate a random integer n from 1 to 30.Step 3:Select the student who is currently assigned integer n and display the student's name.Step 4:Generate a new random integer n from 1 to 30.Step 5:Select the student who is currently assigned integer n and display the student's name. C Step 1:Assign each student a unique integer from 1 to 30.Step 2:Generate a random odd integer n from 1 to 29.Step 3:Select the student who is currently assigned integer n and display the student's name.Step 4:Generate a new random even integer n from 2 to 30.Step 5:Select the student who is currently assigned integer n and display the student's name. D Step 1:Assign each student a unique integer from 1 to 30.Step 2:Generate a random integer n from 1 to 30.Step 3:Select the student who is currently assigned integer n and display the student's name.Step 4:The student who was selected in the previous step is assigned 0. All other students are reassigned a unique integer from 1 to 29.Step 5:Generate a new random integer n from 1 to 29. Step 6:Select the student who is currently assigned integer n and display the student's name.

C

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 A string variable named s and a Boolean variable named a B A string variable named s and a numeric variable named n C A string variable named studentName and a Boolean variable named isAbsent D A string variable named studentName and a numeric variable named numAbsences

A

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? A len <- LENGTH [scores]-1 found <- false index <- 2 REPEAT len TIMES IF scores [index] = scores [1] found <- true index <- index + 1 B len <- LENGTH [scores] found <- false index <- 1 REPEAT len TIMES IF scores [index] = scores [1] found <- true index <- index + 1 C len <- LENGTH [scores] found <- false index <- 2 REPEAT UNTIL index >= len IF scores [index] = scores [1] found <- true index <- index + 1 D found <- false FOR EACH value IN scores IF value = scores[1] found <- true

D

A video game character can face toward one of four directions: north, south, east, and west. Each direction is stored in memory as a sequence of four bits. A new version of the game is created in which the character can face toward one of eight directions, adding northwest, northeast, southwest, and southeast to the original four possibilities. Which of the following statements is true about how the eight directions must be stored in memory? A Four bits are not enough to store the eight directions. Five bits are needed for the new version of the game. B Four bits are not enough to store the eight directions. Eight bits are needed for the new version of the game. C Four bits are not enough to store the eight directions. Sixteen bits are needed for the new version of the game. D Four bits are enough to store the eight directions.

C

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 ? A "e" B "f" C "h" D "i"

B

Consider a game where a player spins a wheel divided into four identical sections, labeled A, B, C, and D. If the player spins A, the score is 10. If the player spins B, the score is 5. If the player spins C or D, the score is -1. The following code segment is intended to implement the game. <MISSING STATEMENT> IF spin = 1 score <- 10 ELSE IF spin = 2 score <- 5 ELSE score <- -1 Which of the following could be used as a replacement for <MISSING STATEMENT> so the code segment works as intended? A spin <- RANDOM [1, 3] B spin <- RANDOM [1, 4] C spin <- RANDOM [1, 6] D spin <- RANDOM [1, 8]

D

Consider the following code segment. result <- 2 REAPEAT 3 TIMES result <- result * 5 DISPLAY [result] Which of the following best describes the behavior of the code segment? A The code segment displays the value of 2(5×3) by initializing result to 2 and then multiplying result by 3 a total of five times. B The code segment displays the value of 2(5×3) by initializing result to 2 and then multiplying result by 5 a total of three times. C The code segment displays the value of 2(53) by initializing result to 2 and then multiplying result by 3 a total of five times. D The code segment displays the value of 2(53) by initializing result to 2 and then multiplying result by 5 a total of three times.

C

Consider the following code segment. result ← 1 IF(score1 > 500) { result ← result + 1 IF(score2 > 500) { result ← result + 1 } ELSE { result ← result + 2 } } ELSE { result ← result + 5 IF(score2 > 500) { result ← result + 1 } ELSE { result ← result - 1 } } If the value of score1 is 350 and the value of score2 is 210, what will be the value of result after the code segment is executed? A 3 B 4 C 5 D 7

B

Consider the following code segment. 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? A count1 = 2, count2 = 2 B count1 = 2, count2 = 3 C count1 = 3, count2 = 2 D count1 = 5, count2 = 0

C

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? A x only B y only C x and z only D x, y, and z

B

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? A 17 B 14 C 11 D 4

A

Consider the following numeric values. Binary 1011 Binary 1101 Decimal 5 Decimal 12 Which of the following lists the values in order from least to greatest? A Decimal 5, binary 1011, decimal 12, binary 1101 B Decimal 5, decimal 12, binary 1011, binary 1101 C Decimal 5, binary 1011, binary 1101, decimal 12 D Binary 1011, binary 1101, decimal 5, decimal 12

A

Consider the following procedures for string manipulation. Procedure CallExplanationconcat(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 ? A initials ← concat(prefix(firstName, 1), prefix(lastName, 1)) B initials ← concat(prefix(firstName, 2), prefix(lastName, 2)) C initials ← prefix(concat(firstName, lastName), 1) D initials ← prefix(concat(firstName, lastName), 2)

C

Consider the following procedures for string manipulation. Procedure CallExplanationconcat(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 ? A word ← "no" word ← concat(reverse(word), word) B word ← "no" word ← concat(reverse(word), reverse(word)) C word ← "on" word ← concat(reverse(word), word) D word ← "on" word ← concat(reverse(word), reverse(word))

A

Consider the following procedures for string manipulation. Procedure CallExplanationconcat(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. A newString ← substring(oldString, 3, len(oldString) - 4) B newString ← substring(oldString, 3, len(oldString) - 2) C tempString ← substring(oldString, 3, len(oldString) - 2) newString ← substring(tempString, 1, len(tempString) - 2) D tempString1 ← substring(oldString, 1, 2) tempString2 ← substring(oldString, len(oldString) - 2, 2) newString ← concat(tempString1, tempString2)

B

Consider the following spinner, which is used to determine how pieces are to be moved on a game board. Each region is of equal size. Which of the following code segments can be used to simulate the behavior of the spinner? A B C D *Look on AP classroom

A

DineOutHelper is a mobile application that people can use to select a restaurant for a group meal. Each user creates a profile with a unique username and a list of food allergies or dietary restrictions. Each user can then build a contact list of other users of the app. A user who is organizing a meal with a group selects all the members of the group from the user's contact list. The application then recommends one or more nearby restaurants based on whether the restaurant can accommodate all of the group members' allergies and dietary restrictions. Suppose that Alejandra is using DineOutHelper to organize a meal with Brandon and Cynthia. Which of the following data are needed for DineOutHelper to recommend a restaurant for the group? I. Each group member's list of food allergies or dietary restrictions II. Alejandra's geographic location III. The usernames of the people on Brandon and Cynthia's contact lists A I and II only B I and III only C II and III only D I, II, and III

B

DineOutHelper is a mobile application that people can use to select a restaurant for a group meal. Each user creates a profile with a unique username and a list of food allergies or dietary restrictions. Each user can then build a contact list of other users of the app. A user who is organizing a meal with a group selects all the members of the group from the user's contact list. The application then recommends one or more nearby restaurants based on whether the restaurant can accommodate all of the group members' allergies and dietary restrictions. Suppose that Alejandra is using DineOutHelper to organize a meal with Brandon and Cynthia. Which of the following data is not provided by Alejandra but is necessary for DineOutHelper to recommend a restaurant for the group? I. Brandon's contact list II. Information about which restaurants Brandon and Cynthia have visited in the past III. Information about which food allergies and dietary restrictions can be accommodated at different restaurants near Alejandra A II only B III only C II and III only D I, II, and III

C

In a certain game, a player may have the opportunity to attempt a bonus round to earn extra points. In a typical game, a player is given 1 to 4 bonus round attempts. For each attempt, the player typically earns the extra points 70% of the time and does not earn the extra points 30% of the time. The following code segment can be used to simulate the bonus round. success <- 0 attempts <- RANDOM [ 1, 4] REPEAT attempts TIMES IF RANDOM [ 1, 10] <= 7 success <- success + 1 DISPLAY "The player had" DISPLAY attemps DISPLAY "bonus round attempts and" DISPLAY "success" DISPLAY "of them earned extra points." Which of the following is NOT a possible output of this simulation? A The player had 1 bonus round attempts and 1 of them earned extra points. B The player had 2 bonus round attempts and 0 of them earned extra points. C The player had 3 bonus round attempts and 7 of them earned extra points. D The player had 4 bonus round attempts and 3 of them earned extra points.

A,D

In a certain game, the integer variable bonus is assigned a value based on the value of the integer variable score. If score is greater than 100, bonus is assigned a value that is 10 times score. If score is between 50 and 100 inclusive, bonus is assigned the value of score. If score is less than 50, bonus is assigned a value of 0. Which of the following code segments assigns bonus correctly for all possible integer values of score ? Select two answers. A IF(score > 100) { bonus ← score * 10 } ELSE { IF(score ≥ 50) { bonus ← score } ELSE { bonus ← 0 } } B IF(score ≥ 50) { IF(score > 100) { bonus ← score * 10 } ELSE { bonus ← 0 } } ELSE { bonus ← score } C IF(score < 50) { bonus ← 0 } ELSE { IF(score ≥ 50) { bonus ← score } ELSE { bonus ← score * 10 } } D IF(score < 50) { bonus ← 0 } ELSE { IF(score > 100) { bonus ← score * 10 } ELSE { bonus ← score } }

D

In a certain video game, the variable maxPS represents the maximum possible score a player can earn. The maximum possible score depends on the time it takes the player to complete the game. The value of maxPS should be 30 if time is greater than 120 and 50 otherwise. Which of the following code segments correctly sets the value of maxPS based on the value of time ? Select two answers. A maxPS <- 50 IF time > 120 maxPS <- 30 B IF time > 120 maxPS <- 30 maxPS <- 50 C IF time > 120 maxPS <- 50 ELSE maxPS <- 30 D IF time > 120 maxPS <- 30 ELSE maxPS <- 50

B

In the following code segment, assume that x and y have been assigned integer values. sum ← 0 REPEAT x TIMES { REPEAT y TIMES { sum ← sum + 1 } } At the end of which of the following code segments is the value of sum the same as the value of sum at the end of the preceding code segment? Select two answers. A sum ← 0 z ← x + y REPEAT z TIMES { sum ← sum + 1 } B sum ← 0 z ← x * y REPEAT z TIMES { sum ← sum + 1 } C sum ← 0 REPEAT x TIMES { sum ← sum + 1 } REPEAT y TIMES { sum ← sum + 1 } D sum ← 0 REPEAT y TIMES { REPEAT x TIMES { sum ← sum + 1 }

D

In the following code segment, score and penalty are initially positive integers. The code segment is intended to reduce the value of score by penalty. However, if doing so would cause score to be negative, score should be assigned the value 0. For example, if score is 20 and penalty is 5, the code segment should set score to 15.If score is 20 and penalty is 30, score should be set to 0. The code segment does not work as intended. Line 1: IF(score - penalty < 0) Line 2: { Line 3: score ← score - penalty Line 4: } Line 5: ELSE Line 6: { Line 7: score ← 0 Line 8: } Which of the following changes can be made so that the code segment works as intended? A Changing line 1 to IF(score < 0) B Changing line 1 to IF(score + penalty < 0) C Changing line 7 to score ← score + penalty D Interchanging lines 3 and 7

C

In the following expression, the variable truckWeight has the value 70000 and the variable weightLimit has the value 80000. truckWeight < weightLimit What value does the expression evaluate to? A 70000 B 80000 C true D false

B

In the following procedure, the parameter max is a positive integer. PROCEDURE printNums(max) { count ← 1 REPEAT UNTIL(count > max) { DISPLAY(count) count ← count + 2 } } Which of the following is the most appropriate documentation to appear with the printNums procedure? A Prints all positive even integers that are less than or equal to max. B Prints all positive odd integers that are less than or equal to max. C Prints all positive even integers that are greater than max. D Prints all positive odd integers that are greater than max.

B

In the following procedure, the parameter numList is a list of numbers and the parameters j and k are integers. PROCEDURE swapListElements(numList, j, k) { newList ← numList newList[j] ← numList[k] newList[k] ← numList[j] RETURN(newList) } Which of the following is the most appropriate documentation to appear with the swapListElements procedure? A Returns a copy of numList with the elements at indices j and k interchanged.The value of j must be between 0 and the value of k, inclusive. B Returns a copy of numList with the elements at indices j and k interchanged.The values of j and k must both be between 1 and LENGTH(numList), inclusive. C Interchanges the values of the parameters j and k.The value of j must be between 0 and the value of k, inclusive. D Interchanges the values of the parameters j and k.The values of j and k must both be between 1 and LENGTH(numList), inclusive.

B

Let n be an integer value. Which of the following expressions evaluates to true if and only if n is a two-digit integer (i.e., in the range from 10 to 99, inclusive)? A n = (n MOD 100) B (n ≥ 10) AND (n < 100) C (n < 10) AND (n ≥ 100) D (n > 10) AND (n < 99)

C

PROCEDURE calculate [x, y] result <- x + y result <- result/x DISPLAY [result] Which of the following is the most appropriate documentation to appear with the calculate procedure? A Displays the value of x + (y / x).The value of the parameter x must not be 0. B Displays the value of x + (y / x).The value of the parameter y must not be 0. C Displays the value of (x + y) / x.The value of the parameter x must not be 0. D Displays the value of (x + y) / x.The sum of the parameters x and y must not be 0.

B

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? A IF(vote ≤ LENGTH(voteList)) B FOR EACH vote IN voteList C REPEAT LENGTH(voteList) TIMES D REPEAT UNTIL(vote > LENGTH(voteList))

D

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? A REPEAT 3 TIMES { MOVE_FORWARD() } REPEAT 2 TIMES { MOVE_FORWARD() } REPEAT 3 TIMES { MOVE_FORWARD() } B REPEAT 8 TIMES { MOVE_FORWARD() } C REPEAT 3 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 2 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 3 TIMES { MOVE_FORWARD() } D REPEAT 3 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 2 TIMES { MOVE_FORWARD() } ROTATE_RIGHT() REPEAT 3 TIMES { MOVE_FORWARD()

D

The following grid contains a robot represented as a triangle, which is initially in the bottom-left square of the grid and facing the top of the grid. The robot can move into a white or a gray square but cannot move into a black region. The following code segment implements an algorithm that moves the robot from its initial position to the gray square and facing the top of the grid. When the robot reaches the gray square, it turns around and faces the bottom of the grid. Which of the following changes, if any, should be made to the code segment to move the robot back to its original position in the bottom-left square of the grid and facing toward the bottom of the grid? A Interchange the ROTATE_RIGHT and the ROTATE_LEFT blocks. B Replace ROTATE_RIGHT with ROTATE_LEFT. C Replace ROTATE_LEFT with ROTATE_RIGHT. D No change is needed; the algorithm is correct as is.

B

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? All the people in the room stand up. All standing people form pairs 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 sit down. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing. That person has the earliest birthday. 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. Beginning with the number 1, ask if anyone was born on that day of any month. Continue with the numbers 2, 3, and so on until a positive response is received. If only one person responds, that person has the earliest birthday. If more than one person responds, determine which person was born in the earliest month, and that person or those persons have the earliest birthday. A I only B II only C I and

c

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? A IF numUnits <= 25 cost <- numUnits * 5 ElSE cost <- numUnites * 7 B IF numUnits <= 25 cost <- numUnits * 5 ELSE cosr <- (numUnits -25) * 7 C IF (numUnits <= 25) cost <- numUnits * 5 ELSE cost <-25 * 5 + (numUnits - 25) * 7 D IF numUnits <= 25 cost <- numUnits *5 ELSE cosr <- 25*7 +(numUnits -25) *5

B

The figure below shows four grids, each containing a robot represented as a triangle. The robot cannot move to a black square or move beyond the edge of the grid. Which of the following algorithms will allow the robot to make a single circuit around the rectangular region of black squares, finishing in the exact location and direction that it started in each of the four grids? Step 1:Keep moving forward, one square at a time, until the square to the right of the robot is black.Step 2:Turn right and move one square forward.Step 3:Repeat steps 1 and 2 three more times. B Step 1:Keep moving forward, one square at a time, until the square to the right of the robot is no longer black.Step 2:Turn right and move one square forward. Step 3: Repeat steps 1 and 2 three more times. C Step 1:Move forward three squares.Step 2:Turn right and move one square forward.Step 3: If the square to the right of the robot is black, repeat steps 1 and 2. D Step 1:Move forward three squares.Step 2:Turn right and move one square forward.Step 3:If the square to the right of the robot is not black, repeat steps 1 and 2.

A

The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially facing toward the top of the grid. The following code segment moves the robot around the grid. Assume that n is a positive integer. Line 1: count ← 0 Line 2: REPEAT n TIMES Line 3: { Line 4: REPEAT 2 TIMES Line 5: { Line 6: MOVE_FORWARD() Line 7: } Line 8: ROTATE_RIGHT() Line 9: } Consider the goal of modifying the code segment to count the number of squares the robot visits before execution terminates. Which of the following modifications can be made to the code segment to correctly count the number of squares the robot moves to? A Inserting the statement count ← count + 1 between line 6 and line 7 B Inserting the statement count ← count + 2 between line 6 and line 7 C Inserting the statement count ← count + 1 between line 8 and line 9 D Inserting the statement count ← count + n between line 8 and line 9

D

The list wordList contains a list of 10 string values. Which of the following is a valid index for the list? A -1 B "hello" C 2.5 D 4

D

The position of a runner in a race is a type of analog data. The runner's position is tracked using sensors. Which of the following best describes how the position of the runner is represented digitally? A The position of the runner is determined by calculating the time difference between the start and the end of the race and making an estimation based on the runner's average speed. B The position of the runner is measured and rounded to either 0 or 1 depending on whether the runner is closer to the starting line or closer to the finish line. C The position of the runner is predicted using a model based on performance data captured from previous races. D The position of the runner is sampled at regular intervals to approximate the real-word position, and a sequence of bits is used to represent each sample.

B

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 ? A Boolean B number C string D list

A

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 ? A Boolean B number C string D list

D

Three different numbers need to be placed in order from least to greatest. For example, if the numbers are ordered 9, 16, 4, they should be reordered as 4, 9, 16. Which of the following algorithms can be used to place any three numbers in the correct order? A If the first number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them. B If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. C If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. Then, if the first number is greater than the last number, swap them. D If the first number is greater than the middle number, swap them. Then, if the middle number is greater than the last number, swap them. Then, if the first number is greater than the middle number, swap them.

C

Three students in different locations are collaborating on the development of an application. Which of the following strategies is LEAST likely to facilitate collaboration among the students? A Having all three students participate in frequent video chat sessions to discuss ideas about the project and to provide feedback on work done so far B Having all three students use an online shared folder to contribute and discuss components to be considered for use in the application C Having all three students write code independently and then having one student combine the code into a program D Having all three students work in a shared document that each can edit to provide comments on the work in progress

A

Three teams (Team A, Team B, and Team C) are participating in a trivia contest. Let scoreA represent the number of correct questions for Team A, scoreB represent the number of correct questions for Team B, and scoreC represent the number of correct questions for Team C. Assuming no two teams get the same number of correct questions, which of the following code segments correctly displays the team with the highest number of correct questions? A. IF score > score B IF scoreA > scoreC DISPLAY "Team A wins." ELSE DISPLAY "Team C wins." ELSE IF scoreB > scoreC DISPLAY "Team B wins." ELSE DISPLAY "Team C wins." B. IF scoreA > scoreB IF score B > score C DISPLAY "Team A wins." ELSE DISPLAY "Team C wins." ELSE IF scoreB > scoreC DISPLAY "Team B wins." ELSE DISPLAY "Team C wins." C. IF scoreA > scoreB IF score C > scoreA DISPLAY "Team C wins." ELSE DISPLAY "Team A wins." ELSE IF scoreB > scoreA DISPLAY "Team B wins." ELSE DISPLAY "Team C wins." D. IF scoreA > scoreB IF scoreC > scoreA DISPLAY "Team C wins." ELSE DISPLAY "Team A wins." ELSE IF scoreC > scoreA DISPLAY "Team C wins." ELSE DISPLAY "Team B wins."

A

Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ? Select two answers. A 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. B Step 1:Set x to 0.Step 2: If x is divisible by 3, then display x.Step 3:Increment x by 1. Step 4: Repeat steps 2 and 3 until x is greater than 20. C 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. D 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 greater than 20.

D

Which of the following are true statements about the data that can be represented using binary sequences? Binary sequences can be used to represent strings of characters. Binary sequences can be used to represent colors. Binary sequences can be used to represent audio recordings. A I only B I and II only C II and III only D I, II, and III

A

Which of the following is a benefit of using a list as a data abstraction in a program? A Lists often allow their size to be easily updated to hold as many data values as needed. B Lists convert all elements to strings so that they can be inspected character-by-character. C Lists prevent duplicate data values from appearing in the list. D Lists are used to store all input data so that there is a running record of all user input.

D

consider the following code segment. ans <- RANDOM [1, 3] + RANDOM [2, 5] ans <- ans + RANDOM [4, 8] Which of the following describes the possible values of ans as a result of executing the code segment? A Any integer value from 1 to 8, inclusive B Any integer value from 1 to 16, inclusive C Any integer value from 4 to 8, inclusive D Any integer value from 7 to 16, inclusive


Ensembles d'études connexes

Chapter 3: Settling the Northern Colonies 1619-1700

View Set

CHAPTER 29: integumentary disorders

View Set