AP Computer Science - Unit 5 answers

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Using simulation software can save the company money by helping to compare designs early in the process before prototype cars are built.

A car manufacturer uses simulation software during the design process for a new car. Which of the following is reasons to use simulation software in this context?

(A) REPEAT 2 TIMES DISPLAY ["up"] REPEAT 3 TIMES DISPLAY ["down"]

A code segment is intended to display the following output. up down down down up down down down Which of the following code segments can be used to display the intended output? (A) REPEAT 2 TIMES DISPLAY ["up"] REPEAT 3 TIMES DISPLAY ["down"] (B) REPEAT 3 TIMES DISPLAY ["up"] REPEAT 2 TIMES DISPLAY ["down"] (C) REPEAT 2 TIMES REPEAT 3 TIMES DISPLAY ["up"] DISPLAY ["down"] (D) REPEAT 3 TIMES REPEAT 2 TIMES DISPLAY ["up"] DISPLAY ["down']

len ← LENGTH (utensils) temp ← utensils [len] REMOVE (utensils, len) APPEND (utensils, temp)

A code segment is intended to transform the list utensils so that the last element of the list is moved to the beginning of the list. For example, if utensils initially contains ["fork", "spoon", "tongs", "spatula", "whisk"], it should contain ["whisk", "fork", "spoon", "tongs", "spatula"] after executing the code segment. Which of the following code segments transforms the list as intended?

(A) 5

A flowchart is a way to visually represent an algorithm. The flowchart below uses the following building blocks. What is displayed as a result of executing the algorithm in the flowchart? (A) 5 (B) 15 (C) 1 2 3 4 (D) 1 2 3 4 5

Step 4: Repeat steps 2 and 3 until the value of position is greater than n. Step 5: If count is greater than or equal to 2, display true. Otherwise, display false.

A list of numbers has n elements, indexed from 1 to n. The following algorithm is intended to display true if the value target appears in the list more than once and to display false otherwise. The algorithm uses the variables position and count. Steps 4 and 5 are missing. Step 1: Set count to 0 and position to 1. Step 2: If the value of the element at index position is equal to target, increase the value of count by 1. Step 3: Increase the value of position by 1. Step 4: (missing step) Step 5: (missing step) Which of the following could be used to replace steps 4 and 5 so that the algorithm works as intended?

Keeping the numeric values in a list makes it easier to apply the same computation to every data element.

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?

(D) Inserting index index - 1 between lines 7 and 8

A student wrote the following program to remove all occurrences of the strings "the" and "a" from the list wordList. Line 1: index LENGTH (wordList) Line 2: REPEAT UNTIL (index < 1) Line 3: { Line 4: IF ((wordList[index] = "the") OR (wordList[index] = "a")) Line 5: { Line 6: REMOVE (wordList, index) Line 7: } Line 8: } While debugging the program, the student realizes that the loop never terminates. Which of the following changes can be made so that the program works as intended? (A) Inserting index index + 1 between lines 6 and 7 (B) Inserting index index + 1 between lines 7 and 8 (C) Inserting index index - 1 between lines 6 and 7 (D) Inserting index index - 1 between lines 7 and 8

(A) len <-- LENGTH[scores] - 1 found <-- false index <-- 2 REPEAT len TIMES IF scores[index] = scores[1] found <-- true index <-- index + 1

A teacher stores the most recent quiz scores for her class in the list scores . The first element in the list hold 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 to 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

h

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 ?

Changing line 5 to APPEND (newList, number)

Assume that the list originalList contains integer values and that the list newList is initially empty. The following code segment is intended to copy all even numbers from originalList to newList so that the numbers in newList appear in the same relative order as in originalList. The code segment may or may not work as intended. Line 1: FOR EACH number IN originalList Line 2: { Line 3: IF (number MOD 2 = 0) Line 4: { Line 5: INSERT (newList, 1, number) Line 6: } Line 7: } Which of the following changes, if any, can be made so that the code segment works as intended?

Sum <-- 0 Repeat three times sum<---- sum + random [0,1 ] if { sum=3 } OR { sum=0 } display ["You Win"] Else display ["You Lose"]

Consider a game in which a player flips a fair coin three times. If all three coin flips have the same result (eitherall heads or all tails) the player wins. Otherwise, the player loses.Which of the following code segments best simulates the behavior of the game?

(D)

Consider the code segment below: IF (CAN_MOVE(forward)) { MOVE_FORWARD ( ) } IF (NOT CAN_MOVE (forward)) { ROTATE_RIGHT ( ) ROTATE_RIGHT ( ) } For the following initial situation, what will be the final position and direction of the robot after the code is executed?

(B)

Consider the code segment below: IF (CAN_MOVE(forward)) { MOVE_FORWARD( ) } ROTATE_RIGHT ( ) ROTATE_RIGHT ( ) IF (NOT CAN_MOVE(RIGHT)) { MOVE_FORWARD ( ) ROTATE_RIGHT ( ) ROTATE_RIGHT ( ) } For the following initial situation, what will be the final position and direction of the robot after the code is executed?

(A) The initial condition is not met, so the code in the REPEAT UNTIL loop is not executed.

Consider the code segment below: REPEAT UNTIL (NOT CAN_MOVE(right)) { IF (CAN_MOVE(forward)) { MOVE_FORWARD( ) } IF (NOT CAN_MOVE(forward)) { ROTATE_RIGHT ( ) ROTATE_RIGHT ( ) MOVE_FORWARD ( ) } } For the following initial situation, what will be the final position and direction of the robot after the code is executed? } The code does not run as intended for the robot in the following scenario. Why not? (A) The initial condition is not met, so the code in the REPEAT UNTIL loop is not executed. (B) The robot backs up, then does not move. (C) The program terminates when the robot attempts to move off the grid. (D) There is an infinite loop.

(D) There is an infinite loop.

Consider the code segment below: REPEAT UNTIL (NOT CAN_MOVE(right)) { IF(CAN_MOVE (forward)) { MOVE_FORWARD( ) } IF(NOT CAN_MOVE (forward)) { ROTATE_RIGHT ( ) ROTATE_RIGHT ( ) MOVE_FORWARD( ) } } For the following initial situation, what will be the final position and direction of the robot after the code is executed? (A) (B) (C) The program terminates when the robot attempts to move off the grid. (D) There is an infinite loop.

(C) APPEND(evenList, 2 * i) i <-- i + 1

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)

(A) 3

Consider the following code segment. integerList <-- 4, 2, 5, 4, 2, 3, 1 result <-- 0 FOR EACH item IN integerList result <-- result + (item MOD 2 ) DISPLAY [result] What value is displayed as a result of executing the code segment? (A) 3 (B) 4 (C) 9 (D) 12

(C) 100 300 500

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

(A) [10, 30, 50, 70]

Consider the following code segment. yourList <-- [20, 40, 60, 80] myList <-- [10, 30, 50, 70] yourList <-- myList What are he 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]

(B) 14

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

(B) 14

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

(C) The robot will be in a position in which it can move forward. and (D) The robot will get stuck in an infinite loop.

Consider the following code segment: REPEAT UNTIL (CAN_MOVE (forward)) { ROTATE_RIGHT ( ) } Which of the following are possible outcomes of the above code? Select two answers. (A) The robot will move forward. (B) The robot will return to its original position. (C) The robot will be in a position in which it can move forward. (D) The robot will get stuck in an infinite loop.

(D)

Consider the following incorrect code segment, meant to guide a robot through the maze below: REPEAT UNTIL (NOT (CAN_MOVE (right) OR CAN_MOVE (left) OR CAN_MOVE (forward) OR CAN_MOVE (backward))) { IF (CAN_MOVE(forward)) { MOVE_FORWARD ( ) } ELSE { ROTATE_RIGHT ( ) } } In order to find out why the code is not working, the programmer views the robot at different points in the code. Immediately after which point will the robot not move as expected?

(C)

Consider the following scenario: What will be the final location and direction of the robot after the following code segment is run? REPEAT UNTIL (NOT CAN_MOVE (forward)) { IF (CAN_MOVE (right)) { ROTATE_RIGHT ( ) MOVE_FORWARD ( ) } ELSE { MOVE_FORWARD ( ) } }

(A) Program A and program B display identical values in the same order.

Consider the two programs below. Which of the following best compares the values displayed by programs A and B? (A) Program A and program B display identical values in the same order. (B) Program A and program B display the same values in different orders. (C) Program A and program B display the same number of values, but the values differ. (D) Program B displays one more value than program A.

(C) Flight simulation software provides a more realistic experience for pilots than actual training flights.

Flight simulation software, which imitates the experience of flying, is often used to train airline pilots. Which of the following is LEAST likely to be an advantage of using flight simulation software for this purpose? (A) Flight simulation software allows pilots to practice landing in a variety of different terrains and weather conditions without having to physically travel. (B) Flight simulation software could save money due to the cost of maintenance and fuel for actual training flights. (C) Flight simulation software provides a more realistic experience for pilots than actual training flights. (D) Flight simulation software allows for the testing of emergency air situations without serious consequences.

3y

In the program below, y is a positive integer (e.g., 1, 2, 3...) [See image] What is the value of result after running the program?

FOR EACH vote IN voteList

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?

(C) ( ( end - start ) / 5) + 1

The code segment below is intended to display all multiples of 5 between the values start and end , inclusive. For example, if start has the value 35 and end has the value 50 , the code segment should display the values 35, 40, 45, and 50. Assume that start and end are multiples of 5 and that start is less than end. Line 1: i <-- start Line 2: REPEAT <MISSING EXPRESSION> TIMES Line 3: { Line 4: DISPLAY ( i ) Line 5: i <-- i + 5 Line 6: } Which of the following could replace <MISSING EXPRESSION> in line 2 so that the code segment works as intended? (A) end - start +1 (B) end - start + 6 (C) ( ( end - start ) / 5) + 1 (D) 5 * (end - start) + 1

(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 more one square forward. Step 3: Repeat steps 1 and 2 three more times.

The figure below shows four girds, 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? (A) 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 more 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.

(B) REPEAT 2 TIMES

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

(D) REPEAT 3 TIMES { MOVE_FORWARD ( ) } ROTATE_LEFT ( ) REPEAT 2 TIMES { MOVE_FORWARD ( ) } ROTATE_RIGHT( ) REPEAT 3 TIMES { MOVE_FORWARD ( ) }

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) No change is needed; the algorithm is correct as is.

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 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. REPEAT 2 TIMES MOVE_FORWARD ROTATE_RIGHT REPEAT 4 TIMES MOVE_FORWARD ROTATE_LEFT REPEAT 2 TIMES MOVE_FORWARD 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 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.

(D) n <-- 1 REPEAT 3 TIMES REPEAT n TIMES MOVE_FORWARD ROTATE_LEFT REPEAT n TIMES MOVE_FORWARD ROTATE_RIGHT n <-- n + 1

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? (A) n <-- 1 REPEAT 3 TIMES n <-- n + 1 REPEAT n TIMES MOVE_FORWARD ROTATE_LEFT MOVE_FORWARD ROTATE_RIGHT (B) n <-- 1 REPEAT 3 TIMES REPEAT n TIMES MOVE_FORWARD ROTATE_LEFT MOVE_FORWARD ROTATE_RIGHT n <-- n + 1 (C) n <-- 1 REPEAT 3 TIMES n <-- n + 1 REPEAT n TIMES MOVE_FORWARD ROTATE_LEFT REPEAT n times MOVE_FORWARD ROTATE_RIGHT (D) n <-- 1 REPEAT 3 TIMES REPEAT n TIMES MOVE_FORWARD ROTATE_LEFT REPEAT n TIMES MOVE_FORWARD ROTATE_RIGHT n <-- n + 1

(A) Inserting the statement count <-- count + 1 between line 6 and line 7

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

(A)

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

(B) REPEAT UNTIL (CAN_MOVE (forward) = false) { MOVE_FORWARD ( ) } ROTATE_RIGHT ( )

The grid below contains a robot represented as a triangle, initially facing right. The robot can move into a white or gray square but cannot move into a black region. The code segment below uses the procedure GoalReached , which evaluates to true if the robot is in the gray square and evaluates to false otherwise. REPEAT UNTIL (GoalReached ( )) { <MISSING CODE> } Which of the following replacements for <MISSING CODE> can be used to move the robot to the gray square? (A) REPEAT UNTIL (CAN_MOVE (forward) = false) { ROTATE_RIGHT ( ) } MOVE_FORWARD ( ) (B) REPEAT UNTIL (CAN_MOVE (forward) = false) { MOVE_FORWARD ( ) } ROTATE_RIGHT ( ) (C) REPEAT UNTIL (CAN_MOVE (right)) { ROTATE_RIGHT ( ) } MOVE_FORWARD ( ) (D) REPEAT UNTIL (CAN_MOVE (right)) { MOVE_FORWARD ( ) } ROTATE_RIGHT ( )

(A) IF (CAN_MOVE (right) ) { ROTATE_RIGHT ( ) } MOVE_FORWARD ( )

The grid below contains a robot represented as a triangle, initially facing up. The robot can move into a white or gray square, but cannot move into a black region. The code segment below uses the procedure GoalReached , which evaluates to true if the robot is in the gray square and evaluates to false otherwise. REPEAT UNTIL (GoalReached ( ) ) { <MISSING CODE> } Which of the following replacements for <MISSING CODE> can be used to move the robot to the gray square? (A) IF (CAN_MOVE (right) ) { ROTATE_RIGHT ( ) } MOVE_FORWARD ( ) (B) IF (CAN_MOVE (right) ) { ROTATE_RIGHT ( ) MOVE_FORWARD ( ) } (C) IF (CAN_MOVE (forward) ) { MOVE_FORWARD ( ) } ROTATE_RIGHT ( ) (D) IF (CAN_MOVE (forward) ) { ROTATE_RIGHT ( ) MOVE_FORWARD ( ) }

(D)

The program segment below is intended to move a robot in a grid to a gray square. The program segment uses the procedure GoalReached , which evaluates to true if the robot is in the gray square and evaluates to false otherwise. The robot in each grid is represented as a triangle and is initially facing left. The robot can move into a white or gray square but cannot move into a black region. REPEAT UNTIL (GoalReached ( ) ) { IF (CAN_MOVE (forward) ) { MOVE_FORWARD ( ) } IF (CAN_MOVE (right) ) { ROTATE_RIGHT ( ) } IF (CAN_MOVE (left) ) { ROTATE_LEFT ( ) } } For which of the following grids does the program NOT correctly move the robot to the gray square?

(C) Both program I and program II correctly move the robot to the gray square.

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 gird and facing right. The following programs are each intended to move the robot to the gray square. Program II uses the procedure GoalReached, which returns true if the robot is in the gray square and returns false otherwise. Which of the following statements best describes the correctness of the programs? (A) Program I correctly moves the robot to the gray square, but program II does not. (B) Program II correctly moves the robot to the gray square, but Program I does not. (C) Both program I and program II correctly move the robot to the gray square. (D) Neither program I nor program II correctly moves the robot to the gray square.

(C) Both program I and program II correctly move the robot to the gray square.

The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the bottom right square of the grid and facing toward the top of the grid. The following programs are each intended to move the robot to the gray square. Program II uses the procedure GoalReached , which returns true if the robot is in the gray square and returns false otherwise. Which of the following statements is true? (A) Program I correctly moves the robot to the gray square, but program II does not. (B) Program II correctly moves the robot to the gray square, but Program I does not. (C) Both program I and program II correctly move the robot to the gray square. (D) Neither program I nor program II correctly moves the robot to the gray square.

(A) REPEAT 4 TIMES { MOVE_FORWARD ( ) ROTATE_LEFT ( ) MOVE_FORWARD ( ) ROTATE_RIGHT ( ) } and (C) REPEAT 2 TIMES { REPEAT 4 TIMES { MOVE_FORWARD ( ) } ROTATE_LEFT ( )

The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the bottom-right square of the grid and facing toward the top of the grid. Which of the following code segments can be used to move the robot to the gray square? Select two answers. (A) REPEAT 4 TIMES { MOVE_FORWARD ( ) ROTATE_LEFT ( ) MOVE_FORWARD ( ) ROTATE_RIGHT ( ) } (B) REPEAT 4 TIMES { ROTATE_LEFT ( ) MOVE_FORWARD ( ) MOVE_FORWARD ( ) ROTATE_RIGHT ( ) } (C) REPEAT 2 TIMES { REPEAT 4 TIMES { MOVE_FORWARD ( ) } ROTATE_LEFT ( ) (D) REPEAT 2 TIMES { REPEAT 2 TIMES { MOVE_FORWARD ( ) MOVE_FORWARD ( ) ROTATE_LEFT ( ) } }

(A) count <-- 0 REPEAT 4 TIMES { count count + 1 REPEAT count TIMES { MOVE_FORWARD ( ) { ROTATE_LEFT ( ) }

The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the center square and facing toward the top of the grid. The following code segment is used to move the robot in the grid. count <-- 1 REPEAT 4 TIMES { REPEAT count TIMES { MOVE_FORWARD ( ) { ROTATE_LEFT ( ) count count + 1 } Which of the following code segments will move the robot from the center square along the same path as the code segment above? (A) count <-- 0 REPEAT 4 TIMES { count count + 1 REPEAT count TIMES { MOVE_FORWARD ( ) { ROTATE_LEFT ( ) } (B) count <-- 0 REPEAT 4 TIMES { count count + 1 ROTATE_LEFT ( ) REPEAT count TIMES { MOVE_FORWARD ( ) } } (C) count <-- 0 REPEAT 4 TIMES { REPEAT count TIMES { ROTATE_LEFT ( ) { MOVE_FORWARD ( ) count count + 1 } (D) count <-- 0 REPEAT 4 TIMES { ROTATE_LEFT ( ) REPEAT count TIMES { MOVE_FORWARD ( ) { count count + 1 }

(B)

The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the center square of the grid and facing toward the top of the grid. The following code segment is used to move the robot within the grid. x <-- RANDOM (1, 3) REPEAT x TIMES { ROTATE_RIGHT ( ) } y <-- RANDOM (1, 2) REPEAT y TIMES { MOVE_FORWARD ( ) } A gray square represents a possible final location of the robot after the code segment is executed. Which of the following represents all possible final location for the robot?

(A) MOVE_FORWARD ( ) MOVE_FORWARD ( ) ROTATE_LEFT ( ) ROTATE_LEFT ( ) ROTATE_LEFT ( ) MOVE_FORWARD ( )

The robot below has an additional method, ROTATE_LEFT 0 , in which the robot rotates 90 degrees counter-clockwise (i.e., makes an in-place left turn). Consider a robot who would like to move in an "L" shape. Which of the following code segment will allow to the robot to move in an L shape, assuming the path is clear: (A) MOVE_FORWARD ( ) MOVE_FORWARD ( ) ROTATE_LEFT ( ) ROTATE_LEFT ( ) ROTATE_LEFT ( ) MOVE_FORWARD ( ) (B) MOVE_FORWARD ( ) ROTATE_RIGHT ( ) MOVE_FORWARD ( ) MOVE_FORWARD ( ) (C) MOVE_FORWARD ( ) MOVE_FORWARD ( ) ROTATE_LEFT ( ) ROTATE_LEFT ( ) MOVE_FORWARD ( ) (D) MOVE_FORWARD ( ) MOVE_FORWARD ( ) ROTATE_LEFT ( ) ROTATE_LEFT ( ) MOVE_FORWARD ( )

(C) Both code segments display the correct average, but code segment I requires more arithmetic operations than code segment II.

The two code segments below are each intended to display the average of the numbers in the list . Assume that contains more than one value. Which of the following best describes the two code segments? (A) Code segment I displays the correct average, but code segment II does not. (B) Code segment II displays the correct average, but code segment I does not. (C) Both code segments display the correct average, but code segment I requires more arithmetic operations than code segment II. (D) Both code segments display

(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. and (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.

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.

Lists often allow their size to be easily updated to hold as many data values as needed.

Which of the following is a benefit of using a list as a data abstraction in a program?


Kaugnay na mga set ng pag-aaral

A Christmas Carol Vocabulary Staves 1-2

View Set

Biology Chemistry of Life Test Review

View Set

Monopolistically Competitive Market

View Set

Intro to Financial Accounting Exam 2

View Set

Ch. 61 Management of Patients with Nuero dysfunctions

View Set

Atmosphere and Air Pressure | Period 2

View Set