























































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!
























































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.cos(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? - CORRECT 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. boba, which he drinks 2 days a week and costs $6. Which of these code snippets successfully calculates and stores their total cost? 👁️Note that there are 2 answers to this question. - CORRECT 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? - CORRECT 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. - CORRECT 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:
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? - CORRECT 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 "cactus.io"? - CORRECT 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. - CORRECT 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? - CORRECT 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. - CORRECT 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? - CORRECT 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? - CORRECT ANS 7
What is a benefit to using pseudocode? - CORRECT 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 together in order.SUBSTRING (string - CORRECT 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? - CORRECT 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? - CORRECT 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? - CORRECT 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 arguments.max(a, b)Returns the greater of the two arguments. After the code runs, what value is stored in result? - CORRECT 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? - CORRECT 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? - CORRECT ANS PROCEDURE calcResistance (voltage, current) { DISPLAY (voltage/current) }
The following variable assignments are from an online music app. Identify which variables store numbers and which store strings: - CORRECT 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? - CORRECT 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? - CORRECT 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")? - CORRECT 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? - CORRECT 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. - CORRECT 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.
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? - CORRECT 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? - CORRECT 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. - CORRECT ANS Tyrone is creating a text-based card game. He starts off with this code that deals 3 cards: DISPLAY ("Ace of clubs\n") DISPLAY (" ___ \n") DISPLAY ("|A |\n") DISPLAY ("| O |\n") DISPLAY ("|OxO|
n") DISPLAY ("7 of diamonds\n") DISPLAY (" ___ \n") DISPLAY ("|7 |\n") DISPLAY ("| /|\n") DISPLAY ("| _/|\n") DISPLAY ("5 of clubs\n") 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 clubs\n") DISPLAY (" ___ \n") DISPLAY ("|A |\n") drawClubs () DISPLAY ("7 of diamonds\n") DISPLAY (" ___ \n") DISPLAY ("|7 |\n") DISPLAY ("| /\|\n") DISP - CORRECT 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. - CORRECT 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? - CORRECT ANS 3
NameDescriptionadd(n, m)Returns the addition of n to m.sqrt(n)Returns the square root of nnn.square(n)Returns the value of n^2n2n, squared.cube(n)Returns the value of n^3n3n, cubed.pow(n, m)Returns the value of n^mnmn, start superscript, m, end superscript. How could the code be rewritten using the constant and procedures? 👁️Note that there are 2 answers to this question. - CORRECT ANS volume ← (4/3) * PI * cube(radius) volume ← (4/3) * PI * pow(radius, 3) Elvira is experimenting with assigning and displaying variables. Here's a snippet of her code: a ← 12 DISPLAY (a) b ← 32 DISPLAY (b) a ← b b ← 52 DISPLAY (a) DISPLAY (b) What will be the output of that code? - CORRECT ANS 12 32 32 52 An embedded systems engineer is writing a program to automate filling a bath tub with water: filledWater ← 0 tankCapacity ← 50 fillAmount ← 10 waterHeight ← measureHeight() REPEAT UNTIL (waterHeight ≥ 30 OR filledWater ≥ tankCapacity) { fillTub(fillAmount) filledWater ← filledWater + fillAmount waterHeight ← measureHeight() } The program relies on two methods provided by the tub software: NameDescriptionfillTub(numGallons)Fills the tub with the specified number of gallons of water.measureHeight()Returns the height of the water in the tub, in inches. Part 1: In what situations will the computer execute the code inside the REPEAT loop? 👁️Note that there may be multiple answers to this question. Part 2: What is the maximum times the computer will execute the code inside the REPEAT loop? - CORRECT ANS When waterHeight is 0 and filledWater is 25 The computer wouldn't execute the code more than 5 times. This program uses a nested conditional to assign the variable mystery to a value:
IF (x > y) { mystery ← x - y } ELSE { IF (x < y) { mystery ← y / x } ELSE { mystery ← x + y } } If we set x to 8 and y to 24, what value will the mystery variable store after running this code? - CORRECT ANS 3 A vending machine manufacturer is writing code to determine the optimal prices for their products. The program below processes a list of costs (in dollars and cents). The goal of the program is to create a new list that contains only the costs that can be paid entirely in quarters. costs ← [1.15, 1.25, 2.50, 2.45, 3.75, 2.00] quarterCosts ← [] FOR EACH cost IN costs { costInCents ← cost * 100 IF (costInCents MOD 25 = 0) {
RETURN distance KittyBot is a programmable robot that obeys the following commands: NameDescriptionwalkForward()Walks forward one space in the grid.turnLeft()Rotates left 90 degrees (without moving forward).turnRight()Rotates right 90 degrees (without moving forward). KittyBot is currently positioned in the second row and second column of the grid, and is facing the right side of the grid. We want to program KittyBot to reach the ball of yarn, located in the fifth row and fifth column. Which of these code segments accomplishes that goal? - CORRECT ANS REPEAT 3 TIMES { walkForward() turnRight() walkForward() turnLeft() } Nyala is making a program to display a monster on the screen. This is her code for storing the monster's coordinates: x ← 55 y ← 82 What will be the value of x after this code runs? - CORRECT ANS 55 This program uses a conditional to help a gardener plan their potato harvesting. IF (month = "July" OR tuberSize > 2) { activity ← "harvest" } ELSE { activity ← "wait" } In which situations will activity be "harvest"? 👁️Note that there may be multiple answers to this question. - CORRECT ANS When month is "July" and tuberSize is 1. When month is "July" and tuberSize is 2. When month is "August" and tuberSize is 2. Ayaan is writing a program that generates wizard spells.
The program relies on multiple string operations: PseudocodeDescriptionCONCAT(string1, string2)Concatenates (joins) two strings to each other, returning the combined string.UPPER(string)Returns the string in uppercase. Here's a snippet of his code: start ← "Bibbidi Bobbidi" end ← "Boo" spell ← CONCAT(start, UPPER(end)) What value does the spell variable store? - CORRECT ANS "Bibbidi BobbidiBOO" The following procedure calculates the area of a trapezoid and takes three parameters: the width of the first base, the width of the second base, and the height of the trapezoid. PROCEDURE trapezoidArea (b1, b2, h) { result ← ((b1 + b2) * h) / 2 DISPLAY (result) } Here is a trapezoid with an unknown area: 8\text{ m}8 m4\text{ m}4 m2\text{ m}2 m Trapezoid diagram with upper base width of 4 meters, lower base width of 8 meters, and height of 2 meters. Which of these lines of code correctly calls the procedure to calculate the area of this trapezoid? 👁️Note that there may be multiple answers to this question. - CORRECT ANS trapezoidArea (8, 4, 2) trapezoidArea (4, 8, 2) Emmet is creating a program to display words in their binary form, according to the ASCII encoding standard. This is what his program contains so far: PROCEDURE showA () { DISPLAY ("01000001") } PROCEDURE showB () { DISPLAY ("01000010") } PROCEDURE showC () { DISPLAY ("01000011") } PROCEDURE showD () { DISPLAY ("01000100") } PROCEDURE showE () { DISPLAY ("01000101") } showD () showA () showB () showB () showE () showD () When this program executes, how many total calls does it make to the DISPLAY procedure? - CORRECT ANS 6