Docsity

AP Computer Science Principles Programming (Khan Academy), Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
1 / 69
AP Computer Science Principles Programming (Khan Academy)
1. Which of the following is a benefit of procedures for programmers?:
Program-
mers can more easily
understand programs with procedures, since procedures give names to complex pieces of
code.
2.
Aarush is writing a program to help him calculate how much exercise he does at the gym.
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
3. At that distance, the code should give the player a 30% chance of making the
goal.:
RANDOM(1, 100)
<= 30
RANDOM(1,
10)
<=
3
4.
discount •0
IF (quantity <?> 150)
{
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45

Partial preview of the text

Download AP Computer Science Principles Programming (Khan Academy) and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

1 / 69

AP Computer Science Principles Programming (Khan Academy)

1. Which of the following is a benefit of procedures for programmers?: Program-mers can more easily

understand programs with procedures, since procedures give names to complex pieces of code.

2. Aarush is writing a program to help him calculate how much exercise he does at the gym.

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

3. At that distance, the code should give the player a 30% chance of making the goal.: RANDOM(1, 100)

RANDOM(1, 10) <= 3

4. discount •

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

5. IF (ironLevel < 10)

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.

9. REPEAT UNTIL ( canTakeCheese() )

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

10. sentence

•"" 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.

12. This list represents the horses leading in a race:

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"

13. rowNum •

REPEAT 4 TIMES

colNum •rowNum + 4 REPEAT (5 - rowNum) TIMES { fillPixel(rowNum, colNum, "blue") colNum •colNum + 1 } rowNum •rowNum + 1 }: top right corner

14. words •["cab", "lab", "cable", "cables", "bales", "bale"]

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

15. Mr. Pink:

moveAmount • REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount •moveAmount + 1 }

10 / 69

16. 1: player1Misses •

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

17. APPEND(localFavs, "Udupi")

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"

18. res

ult •0 i • REPEAT 7 TIMES { result •result + i i •i - 1 }: This program sums up the integers from 2 to 8 (inclusive).

19. words •["See", "Jane", "run", "swiftly", "towards", "home"]

bigWords •[] FOR EACH word IN words { IF (LEN(word) > 5) {

} }: APPEND(bigWords, word)

13 / 69

22. playingCards •["3", "5", "6", "7", "9", "J", "A"]

DISPLAY(playingCards[4]) REMOVE(playingCards, 1) INSERT(playingCards, 4, "8") REMOVE(playingCards, 1) INSERT(playingCards, 1, "3") DISPLAY(playingCards[4]): 7 8

23. 1: startPlayer •

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

24. APPEND(favMovies, "The Lion King")

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"

25. We want to program KittyBot so that she pounces on both of the Mousey-Bots,

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)

27. Each of the creatures is controlled by a different program. Frog:

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.

28. Kash is writing code to calculate formulas from his physics class. He's currently

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

29. Emanuel is writing a program to decide which fairgoers can ride on the

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

31. A chemistry student is writing a program to help classify the results of

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"

32. Lillie is writing a program that calculates geometry formulas.

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