apcomp unit 3

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

Consider the following code segment. IF x>y, DISPLAY x+y, ELSE DISPLAY x minus 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) negative 2 (b) 2 (c) 8 (d) Nothing will be displayed

Answer: A

Consider the following code segment.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. © 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.

Answer: A

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 <MISSINGSTATEMENT>{REPEAT4TIMES{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

Answer: B

Consider the following code segment result ← IF(score1 > 500 { result ← result + 1 IF(score2 > 500) { result ← result + 1 } ELSE { result ← result + 2 ELS { result ← result + 5 IF(score2 > 500) { result ← result + 1 } ELSE { result ← result minus 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)

Answer: 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) FOU

Answer: C

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

Answer: 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()}

Answer: 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 macPS > 120, maPS < 30 (b) IF time > 120, maxPS < 30, maxPS < 50(c) IF time > 120, maxPS < 150, ELSE maxPS < 30 (d) IF time > 120, maxPS < 30, ELSE maxPS < 50

Answer: 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

Answer: 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 © string (d) list

Answer: 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 scoreA > scoreB IF scoreA > scoreC, DISPLAY ""Team A wins.", ELSE DISPLAY "Team C wins.", ELSE I scoreB > scoreC, DISPLAY "Team B wins."", ELSE DISPLAY "Team C wins." (b) IF scoreA > scoreB, IF scoreB > scoreC, 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 > scpreB, DISPSLAY "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."

Answer: 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. , © 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

Answer: 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.

Answer: A

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}}

Answer: A & D

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

Answer: 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.

Answer: 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") } } (b) IF(num ≤ 0) { DISPLAY("negative") } ELSE { IF(num = 0) { DISPLAY("zero") } ELSE {DISPLAY("positive") }} (c) IF(num ≤ 0){DISPLAY("negative")}IF(num = 0){DISPLAY("zero")}ELSE{DISPLAY("positive")}

Answer: B

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? (a) true true true (b) false false false (c) true false true (d) false false true

Answer: B

Consider the following code segment. What is the value of r as a result of executing the code segment? (a) 10 (b) 20 © 30 (d) 40

Answer: 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 } }

Answer: 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

Answer: 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))

Answer: 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? 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. (a) I only (b) II only (c) I and II (d) II and II

Answer: 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? (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 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.

Answer: 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 © string (d) List

Answer: 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)

Answer: B

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 © A string variable named studentName and a Boolean variable named isAbsent (d) A string variable named studentName and a numeric variable named numAbsences

Answer: 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"

Answer: 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

Answer: C

Consider the following code segment. Num1 < 6, Num2 < 4, Num3 < 10, IF num1 < num 2, num 1 < 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? (a) 12 (b) 14 (c) 16 (d) 1

Answer: 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

Answer: C

Consider the following code segment. numList < 100, 20, 300, 40, 500, 60; FOR EACH tem 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

Answer: 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)2

Answer: C

In the following expression, the variable truckWeight has the value 70000 and the variable weightLimit has the value 80000. truckWeight < weightLimi What value does the expression evaluate to? (a) 70000 (b) 80000 (c) true (d) false

Answer: 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 numUnites< 25, cost <numnits * 5 ELSE cost < numUnits * 7 (b) IF numUnits </ 25, cost < numUnits * 5 ELSE cost < (numUnits minus 25) * 7 © IF numUnits </ 25 cost <numUnits * 5 ELSE cost < 25 * 5 + (numUnits minus 25) * 7 (d) IF numUnits </ 25, cost < numUnits * 5, ELSE cost < 25 * 7 + (numUnits minus 25) * 5

Answer: 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)

Answer: C

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" (a) II only (b) I and II only (c) I and III only (d) II and III only

Answer: 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

Answer: 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

Answer: D

Consider the following code segment. a< true, b< fase, 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

Answer: 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. / REPEAT 2 TIMES: MOVE_FORWARD; ROTATE_RIGHT; REPEAT 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 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.

Answer: 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.

Answer: D


Ensembles d'études connexes

STAT Final Exam- Test questions Review

View Set

Letter 'U' : Prefixes, Combining Forms, and Suffixes

View Set

Adding and Subtracting Fractions with Like Denominators

View Set

Module 12: Culture, Gender, and Other Environmental Influences

View Set

Lord Voldemort (i recommend studying with test)

View Set

Chapter 1, Chapter 2, Chapter 4, Chapter 3

View Set