1/24
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Suppose we have a list groceryListshown below:
var groceryList = [“milk”, “bread”, “eggs”, “sugar”, “carrots”, “apples”];
Once we buy a certain item, we should remove it from the list to show that we bought it. We want to write a function removeGrocery that will take an item as a parameter and remove it from our grocery list. For example
println(groceryList);
removeGrocery(groceryList, “bread”);
println(groceryList);
Should print out
[“milk”, “bread”, “eggs”, “sugar”, “carrots”, “apples”]
[“milk”, “eggs”, “sugar”, "carrots", “apples”]
Which of the following is the best implementation of removeGrocery?
Consider the following code segment.
The indices on the exam are different. They do not start at 0, but rather at 1.
aList <- [“cat”, true, “hello”, 78, “Karel”]
Which of the following will throw an error?
I. aList[0]
II. aList[2]
III. aList[3]
IV. aList[6]
I and IV
What is an array
an ordered collection of items
In the following array:
var groceries = [“milk”, “eggs”, “cookies”, “cake”]’
Which of the following statements will change cookies to bread
groceries[2] = “bread”;
The AP exam uses the notation [index1, index2, index3..] to create a list with those values as the first, second, third, and so on elements
Consider the following code segement.
arrList ← [17, “karel, false, true, “hello”]
Which element can be found at index 2?
Karel
The AP Exam uses the following format to add an item to list and to find the length of a list.
APPEND (aList, value)
LENGTH(aList)
Given the following code segment, how can you best describe its behavior?
You can assume number is a positive integer and isComposite(x) returns true if x is composite (not prime) and false otherwise.
list ← []
i ← 1
REPEAT number TIMES
{
If (isComposite9i))
{
APPEND(list, i)
}
i ← i + 1
}
DISPLAY LENGTH(list)
This code displays the sum of the composite numbers between 1 and number
What kinds of elements can you store in data structures?
Any kind of objects (anything you can store in a variable)
What is the output of the following program?
var arr = [1, 2, 3, 4, 5];
var removed = arr.remove(2);
println(arr);
println(removed);
[1,2,4,5]
3
Which of the following will create an empty list and assign it to aList?
aList ← [ ]
What does the following function mystery do?
function mystery(list){
var result = [];
while(list.length > 0){
var elem = list.pop();
result.push(elem);
}
return result;
}
Returns a new list containing the elements of the list list in reverse order, and leaves list empty
The AP Exam uses the following format to insert an item to a list and to generate a random integer from a to b, including a and b.
INSERT(aList, i, value)
RANDOM(a, b)
Given the following code segment, how can you best describe its behavior?
i ← 1
F
OR EACH x IN list
{
REMOVE(list, i)
random ← RANDOM(1, LENGTH(list))
INSERT(list, random, x)
i ← i + 1
}
This code shuffles the order of the numbers in the list by removing them and inserting them back in a random place.
Which of the following statements are true about simulation models?
I - Models often omit unnecessary features of the phenomena being modeled.II - The time it takes for a simulation to run increases as the underlying model becomes more complex.
I and II
Consider the code segment. What will be the output?
function start(){
var arr = [ ]; arr.push(5);
arr[1] = 12;
println(arr[0]);
arr.push("hello");
arr.push(true);
arr.push(1000);
arr.push(3.2);
println(arr[3]);
var last = arr.pop();
println(last);
println(arr[4]);
}
5, true, 3.2,100
A group of scientists has developed a simulation that simulates traffic in a city based on several factors. Which of the following statements is most likely something that can be learned from this simulation?
Which roads are most likely to need maintenance in the near future
Consider the following code segment.
bList ← [10, 30, 50]
aList ← [20, 40, 60]
bList ← [15, 45, 90]
aList ← bList
What is stored in aList?
[15, 45, 90]
var numbers = [1,1,2,3,5]
How can we add an 8 to the end of numbers?
numbers.push(8);
The AP Exam uses the following format to iterate through a list.
FOR EACH item IN aList
{
<block of statements>
}
Given the following code segment, how can you best describe its behavior?
result ← 0
FOR EACH n IN list
{
IF (n MOD 2 = 1)
{
result ← result + n
}
}
DISPLAY result
This code displays the sum of the odd numbers in list
What is the output of the following program:
var groceries = ["bread", "bananas", "apples", "cheese", "peanuts"]; println(groceries.indexOf("bananas"));
1
What is the output of the following program?
function start(){
var arr = [12, 17, 65, 7, 30, 88];
var elem = arr.pop();
println(arr);
println(elem); }
[12, 17, 65, 7, 30] 88
What method can we use to find the index of a particular element in an array?
indexOf
How can we get the length of an array arr?
arr.length
We want to be able to read our grocery list in a readable format while we're shopping. We decide to write a function printGroceries such that the following code:
var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"]; printGroceries(groceryList);
JavaScript
Should print out
1. milk 2. bread 3. eggs 4. sugar 5. carrots 6. apples
JavaScript
printGroceries should not modify the groceryList array at all, the array should be in the same state after being printed as it was before it was printed. Which of the following is the best implementation of printGroceries?
unction printGroceries(groceryList){ for(var i = 0; i < groceryList.length; i++){ println((i + 1) + ". " + groceryList[i]); }
The AP Exam uses the following format to remove an item from a list and to access the element at a specific index in a list.
REMOVE(aList, i)
aList[i]
Given the following Mystery procedure, how can you best describe the result it returns?
PROCEDURE Mystery (list, target)
{
length ← LENGTH(list)
i ← 1
REPEAT length TIMES
{
IF (list[i] = target)
{
REMOVE(list, i)
}
ELSE
{
i ← i + 1
}
}
RETURN(list)
}
This procedure returns the list without any instance of the given target
var numbers = [1, 2, 3, 4, 5];
How can we remove the last item from the end of the numbers array and store it in a variable called value?
var value = numbers.pop();
Suppose we have a list groceryList shown below:
var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"];
We want to write a function addGrocery that will take a grocery list and an item as a parameter and add the item on to the end of the grocery list. For example
println(groceryList);
addGrocery(groceryList, "potatoes");
println(groceryList);
Should print out
["milk", "bread", "eggs", "sugar", "carrots", "apples"]
["milk", "bread", "eggs", "sugar", "carrots", "apples", "potatoes"]
Which of the following is the correct implementation of addGrocery?
function addGrocery(groceryList, item){
groceryList.push(item);
}