AP COMPUTER SCIENCE PRINCIPLES PROGRAMMING (KHAN ACADEMY) FREQUENTLY TESTED QUESTIONS AND ANSWERS ;SOLVED AND PASSED 100%
AP COMPUTER SCIENCE PRINCIPLES PROGRAMMING (KHAN ACADEMY) FREQUENTLY TESTED QUESTIONS AND ANSWERS ;SOLVED AND PASSED 100% Which of the following is a benefit of procedures for programmers? Ans- Programmers can more easily understand programs with procedures, since procedures give names to complex pieces of code. 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? Ans- 500 At that distance, the code should give the player a 30% chance of making the goal. Ans- RANDOM(1, 100) <= 30 RANDOM(1, 10) <= 3 discount ← 0 IF (quantity <?> 150) { discount ← 10 } ELSE { IF (quantity <?> 75) { discount ← 7 } ELSE { IF (quantity <?> 10) { discount ← 5 } } } Which operator could replace <?> so that the code snippet works as expected? Ans- ≥ IF (ironLevel < 10) { diagnosis ← "anemic" } ELSE { diagnosis ← "normal" } Which of these tables shows the expected values of diagnosis for the given values of ironLevel? Choose 1 answer: Ans- 4.5 "anemic" 8.2 "anemic" 9.9 "anemic" 10.0 "normal" 22.5 "normal" NOT ( calendar = "user" AND rsvp = "yes" ) Ans- calendar ≠ "user" OR rsvp ≠ "yes" This graph contains a line with unknown slope, going through the points [2, 1][2,1]open bracket, 2, comma, 1, close bracket and [4, 3][4,3]open bracket, 4, comma, 3, close bracket: Ans- (2, 1, 4, 3) Program 1: totalCalories ← 0 loggedMeals ← [700, 800, 600, 300] FOR EACH loggedMeal IN loggedMeals { totalCalories ← totalCalories + loggedMeal } IF (totalCalories > 2000) { excessCalories ← totalCalories - 2000 DISPLAY(excessCalories) } Program 2: totalCalories ← 0 loggedMeals ← [700, 800, 600, 300] FOR EACH loggedMeal IN loggedMeals { totalCalories ← totalCalories + loggedMeal IF (totalCalories > 2000) { excessCalories ← totalCalories - 2000 } } DISPLAY(excessCalories) Ans- Program 1 and Program 2 display the same output, but Program 2 requires more computations. REPEAT UNTIL ( canTakeCheese() ) { <MISSING CODE> } There are many ways for him to reach the cheese. Of the options below, which will require the most repetitions of the loop? Ans- IF (facingWall()) { turnRight() } ELSE { walkForward(2) } sentence ← "" word1 ← "Hello" firstLetter1 ← SUBSTRING(word1, 1, 1) otherLetters1 ← SUBSTRING(word1, 2, LENGTH(word1) - 1) pigLatin1 ← CONCAT(otherLetters1, firstLetter1, "ay") sentence ← CONCAT(sentence, pigLatin1, " ") word2 ← "Mister" firstLetter2 ← SUBSTRING(word2, 1, 1) otherLetters2 ← SUBSTRING(word2, 2, LENGTH(word2) - 1) pigLatin2 ← CONCAT(otherLetters2, firstLetter2, "ay") sentence ← CONCAT(sentence, pigLatin2, " ") word3 ← "Rogers" firstLetter3 ← SUBSTRING(word3, 1, 1) otherLetters3 ← SUBSTRING(word3, 2, LENGTH(word3) - 1) pigLatin3 ← CONCAT(otherLetters3, firstLetter3, "ay") sentence ← CONCAT(sentence, pigLatin3, " ") DISPLAY(sentence) Which of these is the best refactor of the code? Ans- words ← ["Hello", "Mister", "Rogers"] sentence ← "" FOR EACH word IN words { firstLetter ← SUBSTRING(word, 1, 1) otherLetters ← SUBSTRING(word, 2, LENGTH(word) - 1) pigLatin ← CONCAT(otherLetters, firstLetter, "ay") sentence ← CONCAT(sentence, pigLatin, " ") } DISPLAY(sentence) filledWater ← 0 tankCapacity ← 50 fillAmount ← 10 waterHeight ← measureHeight() REPEAT UNTIL (waterHeight ≥ 30 OR filledWater ≥ tankCapacity) { fillTub(fillAmount) filledWater ← filledWater + fillAmount waterHeight ← measureHeight() } Part 1: In what situations will the computer execute the code inside the REPEAT loop? Part 2: What is the maximum times the computer will execute the code inside the REPEAT loop? Ans- When waterHeight is 0 and filledWater is 25 The computer wouldn't execute the code more than 5 times. 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? Ans- "Justify", "Bravazo", "Tenfold", "Good Magic", "Lone Sailor", "Sporting Chance", "Diamond King", "Quip" rowNum ← 1 REPEAT 4 TIMES { colNum ← rowNum + 4 REPEAT (5 - rowNum) TIMES { fillPixel(rowNum, colNum, "blue") colNum ← colNum + 1 } rowNum ← rowNum + 1 } Ans- top right corner words ← ["cab", "lab", "cable", "cables", "bales", "bale"] wordScore ← 0 FOR EACH word IN words { IF (LEN(word) ≥ 5) { wordScore ← wordScore + 3 } ELSE { IF (LEN(word) ≥ 4) { wordScore ← wordScore + 2 } ELSE { IF (LEN(word) ≥ 3) { wordScore ← wordScore + 1 } } } } DISPLAY(wordScore) Ans- 13 Mr. Pink: moveAmount ← 1 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount ← moveAmount + 1 } Purple Pi: moveAmount ← 1 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount ← moveAmount * 2 } Hopper: moveAmount ← 4 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount ← moveAmount / 2 } Spunky Sam: moveAmount ← 4 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount ← moveAmount - 2 } After the first 3 repetitions of each loop, which avatar will be ahead? Ans- Two avatars will be tied for the lead. 1: player1Misses ← 0 2: player2Misses ← 0 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! ☹") 10: } 11: IF (player2Shot = 2) 12: { 13: DISPLAY("Player 2 missed! ☹") 14: } 15: IF (player1Shot = 1 AND player2Shot = 1) 16: { 17: DISPLAY("No misses! ☺") 18: } 19: } Ans- 9 and 10 13 and 14 APPEND(localFavs, "Udupi") 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) Ans- "Udupi", "The Flying Falafel", "Gaumenkitzel", "Rojbas Grill", "Platano", "Cafe Nostos" result ← 0 i ← 8 REPEAT 7 TIMES { result ← result + i i ← i - 1 } Ans- This program sums up the integers from 2 to 8 (inclusive). words ← ["See", "Jane", "run", "swiftly", "towards", "home"] bigWords ← [] FOR EACH word IN words { IF (LEN(word) > 5) { <MISSING CODE> } } Ans- APPEND(bigWords, word) strings ← ["A", "b", "C", "d", "e"] numFound ← 0 FOR EACH string IN strings { IF (UPPER(string) != string) { numFound ← numFound + 1 } } DISPLAY(numFound)` Ans- 3 The following numbers are displayed by a program: 4 8 12 16 The program code is shown below, but it is missing three values: <COUNTER>, <AMOUNT>, and <STEP>. i ← <COUNTER> REPEAT <AMOUNT> TIMES { DISPLAY(i * 2) i ← i + <STEP> } Given the displayed output, what must the missing values be? Choose 1 answer: Ans- <COUNTER> = 2, <AMOUNT> = 4, <STEP> = 2 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]) Ans- 7 8 1: startPlayer ← 0 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") 17: } 18: } Unfortunately, this code is incorrect; the REPEAT UNTIL loop never stops repeating. Where would you add code so that the game starts when expected? Ans- Between line 8 and 9 Between line 12 and 13 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? Ans- "The Lion King", "The Matrix", "Lord of the Rings", "Shrek", "Spider-Man" We want to program KittyBot so that she pounces on both of the MouseyBots, walking exactly into the squares where they're hiding. Which of these code segments accomplishes that goal? Ans- REPEAT 2 TIMES { ..........................
Written for
- Institution
- Khan
- Course
- Khan
Document information
- Uploaded on
- April 21, 2024
- Number of pages
- 54
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
ap computer science principles programming khan a
-
which of the following is a benefit of procedures
-
at that distance the code should give the player
-
which operator could replace so that the code
Also available in package deal