• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 4 out of 63 pages
Exam (elaborations)

khan academy programming unit test

Document preview thumbnail
Preview 4 out of 63 pages

khan academy programming unit test khan academy programming unit test khan academy programming unit test

Content preview

khan academy programming unit test
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.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. - 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:

4556

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? - CORRECT 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. - CORRECT 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? - CORRECT 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}1\small{2}2\small{3}3\small{4}4\small{1}1\small{2}2\small{3}3\small{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? - CORRECT
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? - 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")

Document information

Uploaded on
December 10, 2024
Number of pages
63
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$14.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
lectjoseph
3.5
(10)
Sold
81
Followers
23
Items
5455
Last sold
2 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions