ARJUN'S KHAN ACADEMY PROGRAMMING UNIT TEST UPDATED QUESTIONS AND ANSWERS SOLVED & VERIFIED 100%
ARJUN'S KHAN ACADEMY PROGRAMMING UNIT TEST UPDATED QUESTIONS AND ANSWERS SOLVED & VERIFIED 100% A digital artist is creating an animation with code. Their code needs to convert polar coordinates to cartesian coordinates, using these formulas: x = r × cos( θ )y = r × sin( θ )x=r×cos(θ)y=r×sin(θ) The environment provides these built-in procedures: NameDescriptionsin(angle)Returns the sine of the given (angle)Returns the cosine of the given angle. In their code, theta represents the current angle and r represents the current radius. Which of these code snippets properly implements the conversion formulas? Ans- x ← r * cos(theta) y ← r * sin(theta) Yong is making a program to help him figure out how much money he spends eating. This procedure calculates a yearly cost based on how much an item costs, and how many times a week he consumes it: PROCEDURE calcYearlyCost(numPerWeek, itemCost) { numPerYear ← numPerWeek * 52 yearlyCost ← numPerYear * itemCost RETURN yearlyCost } Yong wants to use that procedure to calculate the total cost of his breakfast beverages: hot tea, which he drinks 5 days a week and costs $2.00 boba, which he drinks 2 days a week and costs $6.00 Which of these code snippets successfully calculates and stores their total cost? ️Note that there are 2 answers to this question. Ans- totalCost ← calcYearlyCost(2, 6.00) + calcYearlyCost(5, 2.00) teaCost ← calcYearlyCost(5, 2.00) bobaCost ← calcYearlyCost(2, 6.00) totalCost ← teaCost + bobaCost The following code snippet processes a list of strings with a loop and conditionals: words ← ["belly", "rub", "kitty", "pet", "cat", "water"] counter ← 0 FOR EACH word IN words { IF (FIND(word, "e") = -1 AND FIND(word, "a") = -1) { counter ← counter + 1 } } DISPLAY(counter) The code relies on one string procedure, FIND(source, target), which returns the first index of the string target inside of the string source, and returns -1 if target is not found. What value will this program display? Ans- 2 Lucie is developing a program to assign pass/fail grades to students in her class, based on their percentage grades. Their college follows this grading system: PercentageGrade70% and abovePASSLower than 70%FAIL The variable percentGrade represents a student's percentage grade, and her program needs to set grade to the appropriate value. Which of these code segments correctly sets the value of grade? ️Note that there are 2 answers to this question. Ans- IF (percentGrade ≥ 70) { grade ← "PASS" } ELSE { grade ← "FAIL" } IF (percentGrade < 70) { grade ← "FAIL" } ELSE { grade ← "PASS" } The following numbers are displayed by a program: 4 5 5 6 The program code is shown below, but it is missing three values: <COUNTER>, <AMOUNT>, and <STEP>. i ← <COUNTER> REPEAT <AMOUNT> TIMES { DISPLAY(i) DISPLAY(i + 1) i ← i + <STEP> } Given the displayed output, what must the missing values be? Ans- <COUNTER> = 4, <AMOUNT> = 2, <STEP> = 1 This program uses a conditional to predict the hair type of a baby. IF (fatherAllele = "C" AND motherAllele = "C") { hairType ← "curly" } ELSE { IF (fatherAllele = "s" AND motherAllele = "s") { hairType ← "straight" } ELSE { hairType ← "wavy" } } In which situations will hairType be "wavy"? ️Note that there may be multiple answers to this question. Ans- When fatherAllele is "s" and motherAllele is "C" When fatherAllele is "C" and motherAllele is "s" Darrell is making a program to track his grades. He made the mistake of using the same variable name to track 3 different test grades, though. Here's a snippet of his code: a ← 89 a ← 97 a ← 93 What will be the value of a after this code runs? Ans- 93 The following procedure calculates the slope of a line and takes 4 numeric parameters: the x coordinate of the first point, the y coordinate of the first point, the x coordinate of the second point, and the y coordinate of the second point. PROCEDURE lineSlope (x1, y1, x2, y2) { result ← (y2 - y1) / (x2 - x1) DISPLAY (result) } This graph contains a line with unknown slope, going through the points [1, 1][1,1]open bracket, 1, comma, 1, close bracket and [3, 4][3,4]open bracket, 3, comma, 4, close bracket: small{1}1small{2}2small{3}3small{4}4small{1}1small{2}2small{3}3small{4}4yyxx Graph with line going through 2 marked points [1, 1] and [3, 4] Which of these lines of code correctly calls the procedure to calculate the slope of this line? Ans- lineSlope(1, 1, 3, 4) A local search website lets users create lists of their favorite restaurants. When the user first starts, the website runs this code to create an empty list: localFavs ← [] The user can then insert and remove items from the list. Here's the code that was executed from one user's list making session: 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) What does the localFavs variable store after that code runs? Ans- "Udupi", "The Flying Falafel", "Gaumenkitzel", "Rojbas Grill", "Platano", "Cafe Nostos" Aden is working on a program that can generate domain names. His program uses the following procedure for string concatenation: PseudocodeDescriptionconcatenate(string1, string2)Concatenates (joins) two strings to each other, returning the combined string. These variables are at the start of his program: company ← "cactus" tld1 ← "com" tld2 ← "io" Which line of code would store the string ""? Ans- name2 ← concatenate(company, concatenate(".", tld2)) This program simulates a game where two players try to make basketball shots . Once either player misses 5 shots, the game is over. 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: } Unfortunately, this code is incorrect; the REPEAT UNTIL loop never stops repeating. Where would you add code so that the game ends when expected? ️Note that there are 2 answers to this question. Ans- Between line 9 and 10 Between line 13 and 14 This short program displays the winning result in the Greenpeace whale naming contest: DISPLAY ("Mister") DISPLAY ("Splashy") DISPLAY ("Pants") Part 1: How many statements are in the above program? Part 2: What does the program output? Ans- 3 Mister Splashy Pants Nanami is researching how much software engineers make. She's writing a program to convert yearly salaries into hourly rates, based on 52 weeks in a year and 40 hours in a week. The program starts with this code: salary ← 105000 wksInYear ← 52 hrsInWeek ← 40 Which lines of code successfully calculate and store the hourly rate? ️Note that there may be multiple answers to this question. Ans- ourlyRate ← salary / wksInYear / hrsInWeek hourlyRate ← (salary / wksInYear) / hrsInWeek Casper is programming a game called GhostHunter Extreme, where players must capture as many ghosts as possible in 5 minutes. Which variable would he most likely use a string data type for? Ans- playerName: The player's name Shari is making an app to sing her favorite silly songs. Here's part of the code: PROCEDURE singVerse () { DISPLAY ("This is the song that never ends.") DISPLAY ("Yes, it just goes on and on my friends.") DISPLAY ("Some people started singing it, not knowing what it was,") DISPLAY ("And they'll continue singing it forever just because...") } singVerse () singVerse () singVerse () singVerse () singVerse () singVerse () singVerse () In total, how many times does this code call the singVerse procedure? Ans- 7 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" } } Which of these tables shows the expected values of solutionType for the given values of phLevel? Ans- phLevelsolutionType-0.4"acidic" 4.7"acidic" 6.9"acidic" 7"neutral" 7.4"basic" 14.2"basic" A program races four creatures against each other: a frog, a fox, a dog, and an alien: Each of the creatures is controlled by a different program. Frog: moveAmount ← 0.5 REPEAT UNTIL ( reachedFinish() ) { moveAmount ← moveAmount * 2 moveForward(moveAmount) } Fox: moveAmount ← 1 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount ← moveAmount + 2 } Dog: moveAmount ← 1 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) } Alien: moveAmount ← 4 REPEAT UNTIL ( reachedFinish() ) { moveForward(moveAmount) moveAmount ← moveAmount / 2 } After 3 repetitions of each loop, which creature will be ahead? Ans- The fox will be ahead. What is a benefit to using pseudocode? Ans- Pseudocode can represent coding concepts common to all programming languages. Charlotte is writing code to turn sentences into Pig Latin. This is what she has so far: 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) The code relies on two string procedures: NameDescriptionCONCATENATE (string1, string2, ...)Returns a single string that combines the provided strings tog 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) The code segment below uses a loop to repeatedly operate on a sequence of numbers. result ← 1 i ← 6 REPEAT 6 TIMES { result ← result * i i ← i + 3 } Which description best describes what this program does? Ans- This program multiplies together the multiples of 3 from 6 to 21 (inclusive). This code is from a website error monitoring system. IF (currentNum > expectedNum) { status ← "Elevated error rate" } ELSE { status ← "All is well" } Which of these tables shows the expected values of status for the given values of currentNum and expectedNum? Ans- currentNumexpectedNumstatus 2 5"All is well" 5 5"All is well" 6 5"Elevated error rate" 9 10"All is well" 10 10"All is well" 15 10"Elevated error rate" Ada notices that her classmate's program is very long and none of the code is grouped into procedures. How can Ada persuade her classmate to start organizing their code into procedures? Ans- She can find places where her classmate's program has repeated code, and suggest using a procedure to wrap up that code. Consider the following code segment: a ← 2 b ← 4 c ← 6 d ← 8 result ← max( min(a, b), min(c, d) ) The code relies on these built-in procedures: NameDescriptionmin(a, b)Returns the smaller of the two (a, b)Returns the greater of the two arguments. After the code runs, what value is stored in result? Ans- 6 A programmer uses this nested conditional in an online graphing calculator. IF (x < 0 AND y < 0) { quadrant ← "BL" } ELSE { IF (x < 0 AND y > 0) { quadrant ← "TL" } ELSE { IF (x > 0 AND y < 0) { quadrant ← "BR" } ELSE { IF (x > 0 AND y > 0) { quadrant ← "TR" } } } } When x is 52 and y is -23, what will be the value of quadrant? Ans- "BR" Joline is writing code to calculate formulas from her electrical engineering class. She's currently working on a procedure to calculate electrical resistance, based on this formula: text{Resistance} = dfrac{text{Voltage}}{text{Current}}Resistance=CurrentVoltagestart text, R, e, s, i, s, t, a, n, c, e, end text, equals, start fraction, start text, V, o, l, t, a, g, e, end text, divided by, start text, C, u, r, r, e, n, t, end text, end fraction Which of these is the best procedure for calculating and displaying resistance? Ans- PROCEDURE calcResistance (voltage, current) { DISPLAY (voltage/current) } This list represents the top runners in a marathon, using their bib numbers as identifiers: frontRunners ← [308, 147, 93, 125, 412, 219, 73, 34, 252, 78] This code snippet updates the list: tempRunner ← frontRunners[3] frontRunners[3] ← frontRunners[2] frontRunners[2] ← tempRunner What does the frontRunners variable store after that code runs? Ans- 308, 93, 147, 125, 412, 219, 73, 34, 252, 78 Greg is hosting a chocolate tasting night for his friends to taste chocolate truffles. He's writing a program to help him plan the festivities. The box of chocolates comes with 35 truffles, and there will be 6 people at the party. numChocolates ← 35 numPeople ← 6 Greg is going to distribute the truffles evenly and then save the extras for himself. Which line of code successfully calculates and stores the number of extra truffles? Ans- numExtras ← numChocolates MOD numPeople Dixie is planning a water balloon fight for her birthday party and writing a program to help calculate how many bags of balloons they'll need to buy. The procedure calcBalloonBags returns the number of bags needed for a given number of players, number of rounds, and number of balloons in the bag. PROCEDURE calcBalloonBags(numPlayers, numRounds, balloonsInBag) { numTeams ← FLOOR(numPlayers / 2) numBalloons ← numTeams * numRounds RETURN CEILING(numBalloons / balloonsInBag) } This procedure relies on two built-in procedures, FLOOR for rounding a number down and CEILING for rounding a number up, which avoids the issue of partial teams and partial bags. Dixie then runs this line of code: bagsNeeded ← calcBalloonBags(30, 10, 50) What value is stored in bagsNeeded? Ans- 3 The following variable assignments are from an online music app. Identify which variables store numbers and which store strings: Ans- title ← "I am a rock" string minutes ←2 number seconds ← 50 number artist ← "Simon & Garfunkel" string recorded ← "Dec 14, 1965" string A software engineer for a movie theater is writing a program to calculate ticket prices based on customer ages. The program needs to implement this pricing chart: Ticket typePriceGeneral Admission$16Senior (Ages 65+)$12Child (Ages 2-12)$8Infants (Ages 0-1)Free The code segment below uses nested conditionals to assign the price variable to the appropriate value, but its conditions are missing operators. price ← 0 IF (age <?> 64) { price ← 12 } ELSE { IF (age <?> 12) { price ← 16 } ELSE { IF (age <?> 1) { price ← 8 } } } Which operator could replace <?> so that the code snippet works as expected? Ans- > Marlon is programming a simulation of a vegetable garden. Here's the start of his code: temperature ← 65 moisture ← 30 acidity ← 3 DISPLAY (acidity) DISPLAY (temperature) DISPLAY (moisture) After running that code, what will be displayed? Ans- 3 65 30 Barry is making a program to process user birthdays. The program uses the following procedure for string slicing: NameDescriptionSUBSTRING (string, startPos, numChars)Returns a substring of string starting at startPos of length numChars. The first character in the string is at position 1. His program starts with this line of code: userBday ← "03/31/84" Which of these lines of code displays the year ("84")? Ans- DISPLAY (SUBSTRING (userBday, 7, 2)) The two procedures below are both intended to return the total number of excess calories eaten in a day based on a list of calories in each meal. Procedure 1: PROCEDURE calcExcess1(meals) { totalCalories ← 0 FOR EACH meal IN meals { totalCalories ← totalCalories + meal } excessCalories ← totalCalories - 2000 RETURN excessCalories } Procedure 2: PROCEDURE calcExcess2(meals) { totalCalories ← 0 FOR EACH meal IN meals { totalCalories ← totalCalories + meal excessCalories ← totalCalories - 2000 } RETURN excessCalories } Consider these procedure calls: excess1 ← calcExcess1([700, 800, 600, 300]) excess2 ← calcExcess2([700, 800, 600, 300]) Which of these statements best describes the difference between the procedure calls? Ans- Both procedure calls return the same value, but the second procedure requires more computations. Haruki is writing a program to share fun emoticons: DISPLAY ("Going for a walk:") DISPLAY ("ᕕ( ᐛ )ᕗ") The following paragraph describes how the program works. Ans- This is a computer program with 2 statements. Each statement calls a procedure named DISPLAY which expects a single parameter to determine what to display on the screen. This is a computer program with 2 statements. Each statement calls a procedure named DISPLAY which expects a single parameter to determine what to display on the screen. Ans- Nothing will be displayed A visual artist is programming a 7x7 LED display: 7x7 grid of squares. This is their program so far: rowNum ← 1 numPixels ← 1 REPEAT 3 TIMES { colNum ← 5 - rowNum REPEAT (numPixels) TIMES { fillPixel(rowNum, colNum, "green") colNum ← colNum + 1 } numPixels ← numPixels + 2 rowNum ← rowNum + 1 } The code relies on this procedure: fillPixel(row, column, color) : Lights up the pixel at the given row and column with the given color (specified as a string). The top row is row 1 and the left-most column is column 1. What will the output of their program look like? Ans- One in the middle like dead center Maria is baking cookies for a bake sale and writing a program to help her decide what price to sell them at. Her program computes possible profits based on estimates for how many she'll sell at different price points. numSold ← 10 pricePer ← 2 moneyMade ← numSold * pricePer DISPLAY(CONCAT(numSold, CONCAT(" x ", pricePer))) DISPLAY(CONCAT(" = ", moneyMade)) numSold ← 20 pricePer ← 1.5 moneyMade ← numSold * pricePer DISPLAY(CONCAT(numSold, CONCAT(" x ", pricePer))) DISPLAY(CONCAT(" = ", moneyMade)) numSold ← 30 pricePer ← 1.25 moneyMade ← numSold * pricePer DISPLAY(CONCAT(numSold, CONCAT(" x ", pricePer))) DISPLAY(CONCAT(" = ", moneyMade)) Maria realizes her program has a lot of duplicate code, and decides to make a procedure to reduce the duplicated code. Which of these procedures best generalizes her code for easy reuse? Ans- PROCEDURE calcProfit(numSold, pricePer) { moneyMade ← numSold * pricePer DISPLAY(CONCAT(numSold, CONCAT(" x ", pricePer))) DISPLAY(CONCAT(" = ", moneyMade)) } Eliott is working with a game designer on a video game. The designer sends them this flow chart: Flow chart that starts with diamond and branches into two rectangles. * Diamond contains question, "Are experience points greater than 100?" * Arrow marked "true" leads to rectangle with text "Add 10 to the level". * Arrow marked "false" leads to rectangle with text "Add 1 to the level". [How do you read a flowchart?] He must implement that logic in code, using the variables experiencePoints and level. Which of these code snippets correctly implements the logic in that flow chart? Ans- IF (experiencePoints > 1000) { level ← level + 10 } ELSE { level ← level + 1 } The following code segment stores and updates a list of cities to visit: topCities ← ["Kyoto", "Florianopolis", "Wellington", "Puerto Viejo", "Sevilla"] topCities[2] ← "Ankara" topCities[4] ← "Taipei" After running that code, what does the topCities list store? Ans- "Kyoto", "Ankara", "Wellington", "Taipei", "Sevilla" Laquan is writing a program to calculate the perimeter of a rectangle, based on this formula: P = 2l + 2wP=2l+2wP, equals, 2, l, plus, 2, w The program starts with this code: length ← 120 width ← 13 Which lines of code successfully calculate the perimeter? ️Note that there may be multiple answers to this question. Ans- Tyrone is creating a text-based card game. He starts off with this code that deals 3 cards: DISPLAY ("Ace of clubsn") DISPLAY (" ___ n") DISPLAY ("|A |n") DISPLAY ("| O |n") DISPLAY ("|OxO|n") DISPLAY ("7 of diamondsn") DISPLAY (" ___ n") DISPLAY ("|7 |n") DISPLAY ("| /|n") DISPLAY ("|_/|n") DISPLAY ("5 of clubsn") DISPLAY (" ___ n") DISPLAY ("|5 |n") DISPLAY ("| O |n") DISPLAY ("|OxO|n") After writing that code, Tyrone decides to use a different way to draw the bottom line of the clubs cards: DISPLAY ("|O,O|n") Part 1: How many lines will Tyrone need to update in his code? Now imagine that Tyrone had originally defined a procedure to draw the club cards, like so: PROCEDURE drawClubs () { DISPLAY ("| O |n") DISPLAY ("|OxO|n") } DISPLAY ("Ace of clubsn") DISPLAY (" ___ n") DISPLAY ("|A |n") drawClubs () DISPLAY ("7 of diamondsn") DISPLAY (" ___ n") DISPLAY ("|7 |n") DISPLAY ("| /|n") DISP Ans- 2 1 If he has to update multiple places in the code, the program will run much slower. Neriah is writing code for a website that publishes attention-grabbing news articles. The code relies on multiple string operations: PseudocodeDescriptionCONCATENATE(string1, string2)Concatenates (joins) two strings to each other, returning the combined string.UPPER(string)Returns the string in uppercase. Her code uses these variables: title1 ← "Cat rips Xmas tree to shreds" title2 ← "Cat stuck in tree for weeks" title3 ← "New world record: fluffiest cat ever" Which of the following expressions results in the string "CAT STUCK IN TREE FOR WEEKS!!!"? ️Note that there are 2 answers to this question. Ans- UPPER( CONCATENATE(title2, "!!!") ) CONCATENATE( UPPER(title2), "!!!") Aleena is developing a program to track her weekly fitness routine. Here is a part of her program that sets up some variables: type ← "swimming" day ← "wednesday" duration ← 50 laps ← 20 location ← "YMCA" How many string variables is this code storing? Ans- 3 Dario is writing an activity planner program to help him remember what physical activities to do each day. IF (today = "Monday") { activity ← "swimming" } ELSE { IF (today = "Tuesday") { activity ← "jogging" } ELSE { IF (today = "Thursday") { activity ← "juggling" } ELSE { IF (today = "Saturday") { activity ← "gardening" } ELSE { activity ← "none" } } } } Which of these tables shows expected values for activity after running this program with a variety of values for today? Ans- todayactivity .......................................................................................................................................... Download and view full test
Written for
- Institution
- Khan
- Module
- Khan
Document information
- Uploaded on
- April 21, 2024
- Number of pages
- 56
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
arjuns khan academy programming unit test updated
-
a digital artist is creating an animation with cod
-
yong is making a program to help him figure out ho
-
which of these code segments correctly sets the
Also available in package deal