apcps

0.0(0)
Studied by 1 person
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/24

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 11:08 PM on 3/12/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

25 Terms

1
New cards

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?

2
New cards

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

3
New cards

What is an array 

an ordered collection of items

4
New cards

In the following array:

var groceries = [“milk”, “eggs”, “cookies”, “cake”]’

Which of the following statements will change cookies to bread

groceries[2] = “bread”;

5
New cards

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

6
New cards

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

7
New cards

What kinds of elements can you store in data structures?

Any kind of objects (anything you can store in a variable)

8
New cards

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

9
New cards

Which of the following will create an empty list and assign it to aList?

aList ← [ ]

10
New cards

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

11
New cards

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.

12
New cards

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

13
New cards

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

14
New cards

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

15
New cards

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]

16
New cards

var numbers = [1,1,2,3,5]

How can we add an 8 to the end of numbers?

numbers.push(8);

17
New cards

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

18
New cards

What is the output of the following program:
var groceries = ["bread", "bananas", "apples", "cheese", "peanuts"]; println(groceries.indexOf("bananas"));

1

19
New cards

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

20
New cards

What method can we use to find the index of a particular element in an array?

indexOf

21
New cards

How can we get the length of an array arr?

arr.length

22
New cards

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]); }

23
New cards

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

24
New cards

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();

25
New cards

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);
}