Test BigIdea 3.1-3.8 - AP CSP

Ace your homework & exams now with Quizwiz!

The code fragment below is intended to display "odd" if the positive number num is odd. IF <MISSING CONDITION> Display "odd" Which of the following can be used to replace <MISSING CONDITION> so that the code fragment will work as intended?

(num MOD 2) = 1

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?

21 40 30 50

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?

3

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

Consider the code segment below. IF onTime DISPLAY "Hello" ELSE IF absent DISPLAY "Is anyone there?" ELSE DISPLAY "Better late than never." If the variables onTime and absent both have the value false, what is displayed as a result of running the code segment?

Better late than never

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 ? (options - summarized) I. grade is equal to C, if A, else B II. if A, else if B, else C III. if C, else if B, else A

II and III only

Which of the following are benefits of using well-named variables in a computer program? Select two answers.

The people will be easier for people to read. The program will be easier to modify in the future.

Consider the following code segment. a = true b = false c = true a = NOT a OR b AND c c = c AND a DISPLAY a DISPLAY b DISPLAY c What is displayed as a result of executing the code segment?

false false false

Which of the following code segments can be used to interchange the values of the variables num1 and num2 ?

temp = num1 num1 = num2 num2=temp

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?

true

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"

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?

((age ≥ 13) OR (grade ≥ 9)) AND (age < 18)

Which of the following Boolean expressions are equivalent to the expression num>=15? Select two answers.

(num>15) OR (num=15) NOT (num<15)

In 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?

3y

A flowchart is a way to visually represent an algorithm. The flowchart below uses the following building blocks. (table) (flowchart) What is displayed as a result of executing the algorithm in the flowchart?

5

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?

5

The following question 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 right. (grid) Consider the following code segment, which moves the robot in the grid. n=3 REPEAT 3 TIMES REPEAT n TIMES MOVE_FORWARD ROTATE LEFT n=n-1 Which of the following shows the location of the robot after running the code segment?

A (arrow in 3rd column, 3rd row, facing down)

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.

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.

Consider the following program code. i=0 sum=0 REPEAT UNTIL i=4 i=1 sum=sum+i 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 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. (grid) 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() }

An algorithm has been developed to compute the sum of all the elements in a list of integers. Which of the following programming structures must be added to the existing algorithm so that the new algorithm computes the sum of only the even integers in the list?

Selection

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?

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.

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 num1 - 5, 1, -3 Value of num2 - 2, 8, 4 Distance Between num1 and num - 3, 7, 7 Which of the following algorithms displays the correct distance for all possible values of num1 and num2 ?

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.

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?

THREE

A school library allows students to borrow laptops. A computer program is used to count the number of times a particular laptop has been borrowed from the library (borrows) and the number of times the same laptop has been returned to the library (returns). Which of the following indicate that a particular laptop is not currently borrowed? Select two answers.

The difference between borrows and returns is 0. The sum of borrows and returns is a positive even number.

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. 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. (grid) 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 procedures for string manipulation. (table) 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) - 4) tempString ← substring(oldString, 3, len(oldString) - 2) newString ← substring(tempString, 1, len(tempString) - 2)

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. (table) Which of the following code segments can be used to update the values of the variables as shown in the table?

temp ← word1 word1 ← word3 word3 ← temp

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?

true true true

The following code segment is intended to set max equal to the maximum value among the integer variables x, y, and z. The code segment does not work as intended in all cases. IF x>y AND x>z max = x IF y>x AND y>z max=y ELSE max=z Which of the following initial values for x, y, and z can be used to show that the code segment does not work as intended?

x = 3, y = 2, z = 1

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

Consider the following procedure. (table) The drawCircle procedure is to be used to draw the following figure on a coordinate grid. (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 }

An office building has two floors. A computer program is used to control an elevator that travels between the two floors. Physical sensors are used to set the following Boolean variables. (table) The elevator moves when the door is closed and the elevator is called to the floor that it is not currently on. Which of the following Boolean expressions can be used in a selection statement to cause the elevator to move?

(onFloor1 AND callTo2) OR (onFloor2 AND callTo1)

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

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. (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, ...). PROCEDURE MYSTERY p REPEAT p TIMES MOVE_FORWARD MOVE_FORWARD ROTATE_RIGHT Which of the following shows a possible result of calling the procedure?

A (arrow in 3rd column, last row, racing left)

A programmer completes the user manual for a video game she has developed and realizes she has reversed the roles of goats and sheep throughout the text. Consider the programmer's goal of changing all occurrences of "goats" to "sheep" and all occurrences of "sheep" to "goats." The programmer will use the fact that the word "foxes" does not appear anywhere in the original text. Which of the following algorithms can be used to accomplish the programmer's goal?

First, change all occurrences of "goats" to "foxes." Then, change all occurrences of "sheep" to "goats." Last, change all occurrences of "foxes" to "sheep."

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?

IF scoreA>scoreB 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."

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.

IF(score > 100) { bonus ←← score * 10 } ELSE { IF(score ≥ 50) { bonus ←← score } ELSE { bonus ←← 0 } } IF(score < 50) { bonus ←← 0 } ELSE { IF(score > 100) { bonus ←← score * 10 } ELSE { bonus ←← score } }

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? I. 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. 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. III. 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.

II only

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


Related study sets

Statistics: Chap. 1.5 - Bias in Sampling

View Set

CHAPTER 5 - CONTRACT LAW: ESSENTIALS OF A VALID CONTRACT

View Set

CIS 3100 Chapter 5 Review Questions

View Set

Фізіологія дихання ( 1 )

View Set

PUB301: Study designs, Bias and Confounding

View Set