





























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























































1 / 69
understand programs with procedures, since procedures give names to complex pieces of code.
The procedure calcSwimYards returns the number of yards swum for a given number of laps in a pool of a given length. PROCEDURE calcSwimYards(poolLength, numLaps) { lapLength â˘poolLength * 2 RETURN lapLength * numLaps } Aarush then runs this line of code: yardsSwum â˘calcSwimYards(25, 10) What value is stored in yardsSwum?: 500
IF (quantity 150) {
2 / 69 discount ⢠} ELSE { IF (quantity 75) { discount ⢠} ELSE { IF (quantity 10) { discount ⢠} } } Which operator could replace so that the code snippet works as expect-ed?: e
diagnosis â˘"anemic" } ELSE { diagnosis â˘"normal"
4 / 69 Program 2: totalCalories ⢠loggedMeals â˘[700, 800, 600, 300] FOR EACH loggedMeal IN loggedMeals { totalCalories â˘totalCalories + loggedMeal IF (totalCalories > 2000) { excessCalories â˘totalCalories - 2000 } } DISPLAY(excessCalories): Program 1 and Program 2 display the same output, but Program 2 requires more computations.
There are many ways for him to reach the cheese. Of the options below, which will require the most repetitions of the loop?: IF (facingWall()) { turnRight() } ELSE {
5 / 69 walkForward(2) }
â˘"" word â˘"Hello" firstLetter1 â˘SUBSTRING(word1, 1, 1) otherLetters1 â˘SUBSTRING(word1, 2, LENGTH(word1) - 1) pigLatin â˘CONCAT(otherLetters1, firstLetter1, "ay") sentence â˘CONCAT(sentence, pigLatin1, " ") word2 â˘"Mister" firstLetter2 â˘SUBSTRING(word2, 1, 1) otherLetters2 â˘SUBSTRING(word2, 2, LENGTH(word2) - 1) pigLatin â˘CONCAT(otherLetters2, firstLetter2, "ay") sentence â˘CONCAT(sentence, pigLatin2, " ") word3 â˘"Rogers" firstLetter3 â˘SUBSTRING(word3, 1, 1) otherLetters3 â˘SUBSTRING(word3, 2, LENGTH(word3) - 1) pigLatin â˘CONCAT(otherLetters3, firstLetter3, "ay") sentence â˘CONCAT(sentence, pigLatin3, " ") DISPLAY(sentence) Which of these is the best refactor of the code?: words â˘["Hello", "Mister", "Rogers"] sentence â˘"" FOR EACH word IN words
7 / 69 The computer wouldn't execute the code more than 5 times.
leadHorses â˘["Justify", "Bravazo", "Good Magic", "Tenfold", "Lone Sailor", "Sporting Chance", "Diamond King", "Quip"] This code snippet updates the list: tempHorse â˘leadHorses[3] leadHorses[3] â˘leadHorses[4] leadHorses[4] â˘tempHorse What does the leadHorses variable store after that code runs?: "Justify", "Bravazo", "Tenfold", "Good Magic", "Lone Sailor", "Sporting Chance", "Diamond King", "Quip"
colNum â˘rowNum + 4 REPEAT (5 - rowNum) TIMES { fillPixel(rowNum, colNum, "blue") colNum â˘colNum + 1 } rowNum â˘rowNum + 1 }: top right corner
8 / 69 wordScore ⢠FOR EACH word IN words { IF (LEN(word) e5) { wordScore â˘wordScore + 3 } ELSE { IF (LEN(word) e4) { wordScore â˘wordScore + 2 } ELSE { IF (LEN(word) e3) { wordScore â˘wordScore + 1 } } } } DISPLAY(wordScore): 13
moveAmount ⢠REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount â˘moveAmount + 1 }
10 / 69
2: player2Misses ⢠3: REPEAT UNTIL (player1Misses = 5 OR player2Misses = 5) 4: { 5: player1Shot â˘RANDOM(1, 2) 6: player2Shot â˘RANDOM(1, 2) 7: IF (player1Shot = 2) 8: { 9: DISPLAY("Player 1 missed! " 9 ) 10: } 11: IF (player2Shot = 2) 12: { 13: DISPLAY("Player 2 missed! " 9 ) 14: } 15: IF (player1Shot = 1 AND player2Shot = 1) 16: { 17: DISPLAY("No misses! :") 18: } 19: }: 9 and 10 13 and 14
11 / 69 APPEND(localFavs, "The Flying Falafel") APPEND(localFavs, "Rojbas Grill") APPEND(localFavs, "Cha-Ya") APPEND(localFavs, "Platano") APPEND(localFavs, "Cafe Nostos") INSERT(localFavs, 3, "Gaumenkitzel") REMOVE(localFavs, 5): "Udupi", "The Flying Falafel", "Gaumenkitzel", "Rojbas Grill", "Platano", "Cafe Nos-tos"
ult â˘0 i ⢠REPEAT 7 TIMES { result â˘result + i i â˘i - 1 }: This program sums up the integers from 2 to 8 (inclusive).
bigWords â˘[] FOR EACH word IN words { IF (LEN(word) > 5) {
} }: APPEND(bigWords, word)
13 / 69
DISPLAY(playingCards[4]) REMOVE(playingCards, 1) INSERT(playingCards, 4, "8") REMOVE(playingCards, 1) INSERT(playingCards, 1, "3") DISPLAY(playingCards[4]): 7 8
2: REPEAT UNTIL (startPlayer `0) 3: { 4: player1Roll â˘RANDOM(1, 6) 5: player2Roll â˘RANDOM(1, 6) 6: IF (player1Roll > player2Roll) 7: { 8: DISPLAY("Player 1 starts") 9: } 10: ELSE IF (player2Roll > player1Roll) 11: { 12: DISPLAY("Player 2 starts") 13: } 14: ELSE 15: { 16: DISPLAY("Roll again")
14 / 69
Unfortunately, this code is incorrect; the REPEAT UNTIL loop never stops repeating. Where would you add code so that the game starts when expected?: Between line 8 and 9 Between line 12 and 13
APPEND(favMovies, "Toy Story") APPEND(favMovies, "The Matrix") APPEND(favMovies, "Shrek") APPEND(favMovies, "Spider-Man") REMOVE(favMovies, 2) INSERT(favMovies, 3, "Lord of the Rings") What does the favMovies variable store after that code runs?: "The Lion King", "The Matrix", "Lord of the Rings", "Shrek", "Spider-Man"
walking exactly into the squares where they're hiding. Which of these code segments accomplishes that goal?: REPEAT 2 TIMES { walkForward() turnRight() walkForward() turnLeft()
16 / 69
DISPLAY(totalDuration)
moveAmount â˘0. REPEAT UNTIL ( reachedFinish() ) { moveAmount â˘moveAmount * 2 moveForward(moveAmount) } Fox: moveAmount ⢠REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount â˘moveAmount + 2 } Dog: moveAmount ⢠REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) }
17 / 69 Alien: moveAmount ⢠REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount â˘moveAmount / 2 } After 3 repetitions of each loop, which creature will be ahead?: The fox will be ahead.
working on a procedure to calculate acceleration, based on this formula: \text{Acceleration} = \dfrac{\text{Force}}{\text{Mass}}Acceleration= Mass Force A, c, c, e, l, e, r, a, t, i, o, n, equals, start fraction, F, o, r, c, e, divided by, M, a, s, s, end fraction Which of these is the best procedure for calculating and displaying accelera-tion?: PROCEDURE calcAcceleration (force, mass) { DISPLAY (force/mass) }
rollercoaster, based on their height. The rollercoaster has a sign posted that states: "RIDERS MUST BE AT LEAST 48" TALL TO RIDE" The variable riderHeight represents a potential rider's height (in inches), and
19 / 69 The code relies on the drawMountain() procedure, which accepts three para-meters for the mountain's x position, y position, and height. Here's what's drawn from one run of the program: Graphic of landscape with mountain 210 pixels from the left side. Which of these describes mountains that might be drawn from this program?- : A mountain at an x position of 4 and a height of 120 A mountain at an x position of 180 and a height of 110
experiments. solutionType â˘"unknown" IF (phLevel = 7) { solutionType â˘"neutral" } ELSE { IF (phLevel > 7) { solutionType â˘"basic" } ELSE { solutionType â˘"acidic"
20 / 69
Which of these tables shows the expected values of solutionType for the given values of phLevel? Choose 1 answer:: phLevel solutionType -0.4 "acidic" 4.7 "acidic" 6.9 "acidic" 7 "neutral" 7.4 "basic" 14.2 "basic"
Her procedure calcDistance should return the distance between two points, based on the Pythagorean distance formula: d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}d= (x 2 âx 1 ) 2 +(y 2 ây