Data Structures - TEST Ans Review
What is the output of the following program: var groceries = ["bread", "bananas", "apples", "cheese", "peanuts"]; println(groceries.indexOf("bananas"));
1
What is an array (or list)?
An ordered collection of items.
What kinds of elements can you store in data structures?
Any kind of objects (anything you can store in a variable)
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 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
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. The indices on the exam are different. They do not start at 0, but rather at 1. Consider the following code segment. arrList ← [17, "Karel", false, true, "hello"] Which element can be found at index 2?
Karel
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. This program removes every element from list starting at the end of list and adds it to result. result will end up with every element in list in reverse order and list will be left empty.
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.
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
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
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]
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);
function addGrocery(groceryList, item) { groceryList.push(item); }
We want to be able to read our grocery list in a readable format while we're shopping. Should print out: 1. milk 2. bread 3. eggs 4. sugar 5. carrots 6. apples 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?
function printGroceries(groceryList){ for(var i = 0; i < groceryList.length; i++){ println((i + 1) + ". " + groceryList[i]); }
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); Which of the following is the best implementation of removeGrocery?
function removeGrocery(groceryList, item) { var index = groceryList.indexOf(item); if(index != -1){ groceryList.remove(index); } }
In the following array: var groceries = ["milk", "eggs", "cookies", "cake"]; Which of the following statements will change "cookies" to "bread"?
groceries[2] = "bread";
What method can we use to find the index of a particular element in an array?
indexOf
var numbers = [1, 1, 2, 3, 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();
How can we get the length of an array arr?
arr.length
Which of the following will create an empty list and assign it to arrList?
arrList ← [ ]
var numbers = [1, 1, 2, 3, 5]; How can we add an 8 to the end of numbers?
numbers.push(8);
