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