Unit 6: Lists, Loops, Traversals Flashcards

1
Q

If Karel is facing North and the code

turnLeft();
turnLeft();
runs, which direction is Karel facing now?

A) North
B) South
C) East
D) West

A

South

Notes: Great job! After the first command, Karel will face West and after the second command Karel will face South.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If Karel starts at Street 1 and Avenue 3 facing East, what street (row) and avenue (column) will Karel be on after this code runs?

move();
move();
move();
turnLeft();
move();

A) Street 1 and Avenue 3
B) Street 4 and Avenue 4
C) Street 2 and Avenue 6
D) Street 6 and Avenue 2

A

Street 2 and Avenue 6

Notes: Excellent! The first 3 move(); commands will bring Karel to Avenue 6. Then, since Karel turns, the last move will bring him to Street 2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many times should Karel turn left in order to turn right?

1
2
3
4

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Question: 1
Which function will teach Karel how to spin in a circle one time?

A

function spin() {
turnRight();
}
B

function spin() {
turnLeft();
turnLeft();
turnLeft();
turnLeft();
}
C

function spin() {
turnLeft();
turnLeft();
}
D

function spin() {
move();
move();
move();
move();
}

A
B
C
D

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the best way for Karel to move 10 times?

move();
move();
move();
move();
move();
move();
move();
move();
move();
move();

for (var i = 0; i < 10; i++) {
move();
}

move(10);

move10();

A

for (var i = 0; i < 10; i++) {
move();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why do we use while loops in JavaScript?

To break out of some block of code
To do something if a condition is true
To repeat some code while a condition is true
To repeat something for a fixed number of times

A

To repeat some code while a condition is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which general while loop definition is written correctly?

A

while (x is true) {
// code
}
B

if (i<5) {
//code
}
C

while (var i = 0; i < count; i++) {
//code
}
D

while (condition) {
//code
}

A
B
C
D

A

D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do we use control structures for in JavaScript?

Control the flow of the program; how the commands execute.
Start our JavaScript program
Store information for later
Teach Karel new commands

A

Control the flow of the program; how the commands execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the below are examples of control structures?

(I) if
(II) if/else
(III) while
(IV) for

I only
I and II only
III and I only
I, II, III, and IV

A

I, II, III, and IV

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Will the following program move Karel one space, pick up three tennis balls, then move forward and put down three tennis balls?

function start() {
move();
tennisBalls();
move();
tennisBalls();
}

function tennisBalls() {
for(var i = 0; i < 3; i ++) {
if(noBallsPresent()){
putBall();
} else {
takeBall();
}
}
}

Yes, because it is using the tennisBalls() function.

No, because the tennisBalls() function is looping the wrong number of times.

Yes, because the move() command is being called outside of the function.

No, because the tennisBalls() function has an incorrect if/else statement.

A

No, because the tennisBalls() function has an incorrect if/else statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many times will the following program print “hello”?

for (var i = 0; i < 5; i++) {
println(“hello”);
}

4
5
6
i

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the following code, what will be the last number to print to the screen before the program finishes?

for (var i = 0; i < 10; i++) {
if (i % 2 == 0) {
println(i);
} else {
println(2 * i);
}
}

10
20
9
18

A

18

Notes: Correct! In this for loop, i will go from 0 to 9. So on the last run of the loop, i will have the value of 9. Since 9 is odd, i % 2 == 0 will be false, and the else statement will execute, causing 2 * i, or 18, to be printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many times will the following code print “hello”?

for (var i = 3; i < 8; i++) {
println(“hello”);
}

3
5
6
8

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many times will the following code print “hello”?

for (var i = 0; i < 8; i += 2) {
println(“hello”);
}
JavaScript

0
2
4
8

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will be the value of sum after this code runs?

var START = 1;
var END = 4;

function start(){
var sum = 0;
for(var i = START; i <= END; i++){
sum += i;
}
}

10
4
1
40

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many times will this program print “hello”?

var i = 10;
while (i > 0) {
println(“hello”);
i–;
}
JavaScript

0
10
i
This code will loop infinitely

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How many times will this program print “hello”?

var i = 50;
while (i < 100) {
println(“hello”);
}
JavaScript

0
50
100
This code will loop infinitely

A

This code will loop infinitely

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The AP Exam does not use for loops and while loops, but rather REPEAT or REPEAT UNTIL commands as shown below.

REPEAT n TIMES
{

<block>
}

REPEAT UNTIL(condition)
{
<block>
}
The program below uses a robot in a 5x5 grid of squares. The robot is represented as a triangle, which is initially in the bottom-left square of the grid and facing toward the right of the grid.

Given the following code segment:

REPEAT 2 TIMES
{
MOVE_FORWARD ()
MOVE_FORWARD ()
ROTATE_LEFT ()
}
Which of the following code segments could be used to produce the same results?

A)
REPEAT 4 TIMES
{
MOVE_FORWARD ()
ROTATE_LEFT ()
}

B)
REPEAT 2 TIMES
{
MOVE_FORWARD ()
MOVE_FORWARD ()
}

ROTATE_LEFT ()

C)
REPEAT 2 TIMES
{
MOVE_FORWARD ()
MOVE_FORWARD ()
}

REPEAT 2 TIMES
{
ROTATE_LEFT ()
ROTATE_LEFT ()
}

D)
REPEAT 2 TIMES
{
MOVE_FORWARD ()
}

ROTATE_LEFT ()

REPEAT 2 TIMES
{
MOVE_FORWARD ()
}
ROTATE_LEFT ()
</block></block>

A

D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Consider the following program, which is intended to print the sum of all the positive integers up to number.

sum ← 0

REPEAT number TIMES
{
sum ← sum + number
}

DISPLAY sum
Which of the following best describes the behavior of this program?

The program correctly displays the sum of all positive integers from 1 to number

The program does not work as intended but rather it displays the number squared.

The program does not work as intended but rather it displays the number factorial.

The program does not work as intended because sum should be initialized to 1.

A

The program does not work as intended but rather it displays the number squared.

Notes: Awesome! Since the DISPLAY command is outside of the loop, only the last result of sum is displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Consider the following program, which is intended to print the count of even numbers between 1 and number

count ← 0

i ← 1

REPEAT number TIMES
{
IF (i MOD 2 = 0)
{
count ← count + 1
}

i ← i + 1 }

DISPLAY count
Which of the following best describes the behavior of this program?

The program correctly displays the count of even numbers between 1 and number

The program does not work as intended because it displays the count of odd numbers between 1 and number

The program does not work as intended because it displays count but should instead display i

The program does not work as intended because the condition in the if statement needs to say (number MOD 2 = 0)

A

The program correctly displays the count of even numbers between 1 and number

Notes: Great job! If there is no remainder (MOD) then the number i is even. Count is incremented and 1 is added to the number i. Then, it repeats.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

In the procedure Mystery written below, the parameter number is a positive integer.

PROCEDURE Mystery (number)
{
value ← number

REPEAT UNTIL (number = 0)
{
    value ← value * -1
    number ← number - 1
}

IF (value > 0)
{
    RETURN (true)
}
ELSE
{
    RETURN (false)
} } Which of the following best describes the result of running the Mystery procedure?

The result will always be true for any initial value of number

The result will always be false for any initial value of number

The result will be true whenever the initial value of number is even

The result will be true whenever the initial value of number is odd

A

The result will be true whenever the initial value of number is even

Notes: Excellent! If the number is even, then the sign of value will return to its original sign by the end of the loop. Since number is a positive integer, value will always start as a positive integer which means it will end as a positive integer (value > 0).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

In the procedure Mystery written below, the parameter number is a positive integer.

PROCEDURE Mystery (number)
{
value ← 0

REPEAT UNTIL (number = 0)
{
    IF (number MOD 2 = 0)
    {
        value ← value + 1
    }
    ELSE
    {
        value ← value - 1
    }

    number ← number - 1
}

IF (value = 0)
{
    RETURN (true)
}
ELSE
{
    return (false)
} } Which of the following best describes the result of running the Mystery procedure?

The result will always be true for any initial value of number

The result will always be false for any initial value of number

The result will be true whenever the initial value of number is even

The result will be true whenever the initial value of number is odd

A

The result will be true whenever the initial value of number is even

Notes: Amazing! An even number (MOD 2 = 0) will add 1 to the value and then subtract 1 to the value once decremented. It will repeat this until number = 0 but will always result in value = 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Which initial value of number would cause an infinite loop?

REPEAT UNTIL(number = 0)
{
number ← number - 1
}

Any positive integer.

Any negative integer.

Any even integer.

Any odd integer.

A

Any negative integer.

Notes: Wonderful! Since the number is decremented in each iteration, if the number in initialized as a negative number, it will never equal 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Which initial value of number would cause the loop to be skipped?

REPEAT UNTIL(number MOD 2 = 0)
{
number ← number - 1
}

Any positive integer.

Any negative integer.

Any even integer.

Any odd integer.

A

Any even integer.

Notes: Nice work! The loop runs until the number is even (MOD 2 = 0). If the number is already even, the loop won’t run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What does the mystery function do?

function mystery() {
while (noBallsPresent()) {
move();
}
}

Karel moves until it is on a ball

If there is no ball present, Karel will move one time and stop.

Karel moves until it puts down a ball

Karel checks if there is no ball on the current spot and then moves once

A

Karel moves until it is on a ball

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What condition should be used in this while loop to get Karel to pick up all the tennis balls on the current location?

while (________) {
takeBall();
}

noBallsPresent()

ballsPresent()

frontIsClear()

takeBall()

A

ballsPresent()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Which of the following is the correct way to define a turnRight function in Karel?

A)
function turnRight() {
turnLeft();
turnLeft();
turnLeft();
}

B)
function turnRight() {
turnRight();
turnRight();
turnRight();
}

C)
function turnRight {
turnLeft();
turnLeft();
turnLeft();
}

D)
turnRight function() {
turnLeft();
turnLeft();
turnLeft();
}

A

A)
function turnRight() {
turnLeft();
turnLeft();
turnLeft();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Question: 23
How many total times will Karel move in this program?

function start() {
move();
for (var i = 0; i < 5; i++) {
move();
putBall();
}
}

1
5
6
7

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is wrong with this for loop?

for (var i = 0, i < 10, i + 1) {
move();
}

A. The for loop uses commas instead of semicolons
B. It should say i++ instead of i + 1

A
B
A and B
The for loop is correct

A

A and B

30
Q

What is the last thing printed by the following program?

var start = 30;
var stop = 10;
for(var i = start; i >= stop; i-=5){
if(i % 2 == 0){
println(i * 2);
} else {
println(i);
}
}

10
20
30
60

A

20

31
Q

We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?

For loop

While loop

Loop and a half

Infinite loop

A

While loop

32
Q

How many times will the following program print “hello”?

var i = 0;
while(i < 10){
println(“hello”);
}

10
11
9
This code will loop infinitely

A

This code will loop infinitely

33
Q

What will the following program print when run?

for (var j = 0; j < 2; j++) {
for (var i = 6; i > 4; i–){
println (i);
}
}

4
5
4
5

6
5
4
6
5
4

0
2
6
4

6
5
6
5

A

6
5
6
5

34
Q

What is the output of the following program?

var result = 0;
var max = 5;
for(var i = 0; i < max; i++){
result += i;
}
println(result);

15
10
0
12345

A

10

35
Q

We want to print the phrase “CodeHS is the best” exactly 25 times. What kind of control structure should we use?

If statement

While loop

Break statement

For loop

A

For Loop

36
Q

We want to make a grocery list in our program. Which of the following is the correct way to make an array/list?

var groceries = “milk, sugar, eggs, cake”;

var groceries = [“milk, sugar, eggs, cake”];

var groceries = [“milk” “sugar” “eggs” “cake”];

var groceries = [“milk”, “sugar”, “eggs”, “cake”];

A

var groceries = [“milk”, “sugar”, “eggs”, “cake”];

37
Q

What is an array (or list)?

An ordered collection of items

An unordered collection of items

A function

A type of loop

A

An ordered collection of items

38
Q

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.

aList ← [15, “hello”, true]
Which element can be found at index 2?

aList

15

hello

true

A

hello

39
Q

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

aList ← 0

aList ← [0]

aList ← [ ]

aList ← “ “

A

aList ← [ ]

40
Q

Consider the following code segment.

aList ← [20, 40, 60]
bList ← [10, 30, 50]
aList ← bList
bList ← [15, 45, 90]
What is stored in aList?

[15, 45, 90]

[10, 30, 50]

[20, 40, 60]

[ ] (empty set)

A

[10, 30, 50]

41
Q

Consider the following code segment.

The indices on the exam are different. They do not start at 0, but rather at 1.

aList ← [“dog”, false, “bird”]
Which of the following will throw an error?

I. aList[0]
II. aList[2]
III. aList[3]
IV. aList[4]

I and II

I and IV

II and IV

III and IV

A

I and IV

Notes: Correct! For the AP Exam, the indices start at 1. There is no index 0 or index 4. in aList.

42
Q

What is the index of the very first item in a JavaScript array?

0
1

A

0

43
Q

var shoppingList = [“milk”, “eggs”, “sugar”, “bread”, “cake”];

Which line of code will correctly change “cake” to “apples”?

shoppingList[“cake”] = “apples”;

shoppingList[“apples”] = “cake”;

shoppingList[4] = “apples”;

shoppingList[5] = “apples”;

A

shoppingList[4] = “apples”;

44
Q

How can we get the length of an array arr?

arr.len

arr.length

length(arr)

arr.length()

A

arr.length

45
Q

Which loop will correctly loop over the array arr and print each value inside of it?

A)
for(var i = 0; i < arr.length(); i++){
var curr = arr[i];
println(curr);
}

B)
for(var i = 1; i <= arr.length(); i++){
var curr = arr[i];
println(curr);
}

C)
for(var i = 0; i < arr.length; i++){
var curr = arr[i];
println(curr);
}

D)
for(var i = 1; i <= arr.length; i++){
var curr = arr[i];
println(curr);
}

A

C)
for(var i = 0; i < arr.length; i++){
var curr = arr[i];
println(curr);
}

46
Q

function start(){
//this array is filled with several Rectangle objects
var rectangleArray = […];

//your code here
... }

function updateRectangle(rect){
//changes the color of rect to a random color
rect.setColor(Randomizer.nextColor());
}
J
We have an array of Rectangles called rectangleArray and a function updateRectangle(rect) that takes a single rectangle as a parameter and changes its color to a random color. How can we easily change the color of every rectangle inside rectangleArray?

A)
updateRectangle(rectangleArray[0]);
updateRectangle(rectangleArray[1]);

updateRectangle(rectangleArray[100]);

updateRectangle(rectangleArray[rectangleArray.length - 1]);

B)
updateRectangle(rectangleArray);

C)
for(var i = 0; i < rectangleArray.length; i++){
updateRectangle(rectangleArray[i]);
}

D)
for(var i = 0; i <= rectangleArray.length; i++){
updateRectangle(rectangleArray[i]);
}

A

C

47
Q

What kinds of items can we store in arrays?

numbers only

strings of text only

numbers or strings of text

any kind of object (anything you can store in a variable)

A

any kind of object (anything you can store in a variable)

48
Q

What is the output of the following program?

function start(){
var arr = [1, 2, 3, 4, 5, 6, 7];
var elem = arr.pop();
println(arr);
println(elem);
}

[1, 2, 3, 4, 5, 6]
[7]

[1, 2, 3, 4, 5, 6]
7

[2, 3, 4, 5, 6, 7]
1

[2, 3, 4, 5, 6, 7]
[1]

A

[1, 2, 3, 4, 5, 6]
7

49
Q

What is the output of the following program?

function start(){
var arr = [1, 2, 3, 4, 5, 6, 7];
var elem = arr.remove(2);
println(arr);
}

[1, 2, 3, 4, 5]

[3, 4, 5, 6, 7]

[1, 3, 4, 5, 6, 7]

[1, 2, 4, 5, 6, 7]

A

[1, 2, 4, 5, 6, 7]

50
Q

The AP Exam uses the following format to iterate through a list.

FOR EACH item IN aList
{

<block>
}
Given the following code segment, how can you best describe its behavior?

result ← 0

FOR EACH n IN list
{
IF (n MOD 2 = 0)
{
result ← result + n
}
}

DISPLAY result

This code displays the count of even numbers in list

This code displays the sum of the even numbers in list

This code displays the sum of the numbers at the even indices of list

This code results in an error
</block>

A

This code displays the sum of the even numbers in list

Notes: Great job! This code checks if n is even (n MOD 2 = 0). If it is, then n is added to the result.

51
Q

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 isPrime(x) returns true if x is prime and false otherwise.

list ← []

i ← 1

REPEAT number TIMES
{
IF (isPrime(i))
{
APPEND(list, i)
}
i ← i + 1
}

DISPLAY LENGTH(list)

This code displays the total number of prime numbers between 1 and number

This code displays a list of all of the prime numbers between 1 and number

This code displays the sum of the prime numbers between 1 and number

This code results in an error

A

This code displays the total number of prime numbers between 1 and number

Notes: Excellent! This code checks to see if i is a prime number. If it is it appends, or adds, it to list. Then, i is incremented and checked again.

52
Q

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 the first instance of the given target

This procedure returns the list without any instance of the given target

This procedure returns the list where each instance of the given target is replaced by the index.

This procedure returns the number of times the target appears in the list

A

This procedure returns the list without any instance of the given target

Notes: Wonderful! This code checks to see if each element in the list matches the target. If it does, then it removes the element from the list.

53
Q

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

FOR EACH x IN list
{
REMOVE(list, i)
random ← RANDOM(1, LENGTH(list))
INSERT(list, random, x)
i ← i + 1
}

This code replaces everything in the list with random numbers.

This code shuffles the order of the numbers in the list by removing them and inserting them back in a random place.

This code removes all of the numbers in the list and inserts random numbers in random places in the list.

This code errors by trying to access an element at an index greater than the length of the list.

A

This code shuffles the order of the numbers in the list by removing them and inserting them back in a random place.

Notes: Nice work! This code first removes the item at each index in the list. Then, it generates a random index (from 1 to LENGTH(list)) and inserts the item it removed to this new index. Lastly, i is incremented and the process repeats.

54
Q

In the following array:

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

Which of the following statements will change “cookies” to “bread”?

groceries[“cookies”] = “bread”;

groceries[3] = “bread”;

groceries[2] = “bread”;

groceries.push(“bread”);

A

groceries[2] = “bread”;

55
Q

What is the output of the following program?

var arr = [1, 2, 3, 4, 5];
var removed = arr.remove(2);
println(arr);
println(removed);

A)
[1, 2, 3]
[4, 5]

B)
[3, 4, 5]
[1, 2]

C)
[1, 3, 4, 5]
2

D)
[1, 2, 4, 5]
3

A

D

56
Q

Suppose we have a list groceryList shown below:

var groceryList = [“milk”, “bread”, “eggs”, “sugar”, “carrots”, “apples”];
JavaScript
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);
JavaScript
Should print out

[“milk”, “bread”, “eggs”, “sugar”, “carrots”, “apples”]
[“milk”, “bread”, “eggs”, “sugar”, “carrots”, “apples”, “potatoes”]
JavaScript
Which of the following is the correct implementation of addGrocery?

A)
function addGrocery(groceryList, item){
groceryList += item;
}

B)
function addGrocery(groceryList, item){
groceryList[groceryList.length - 1] = item;
}

C)
function addGrocery(groceryList, item){
groceryList.push(item);
}

D)
function addGrocery(groceryList, item){
groceryList.length++;
groceryList[groceryList.length] = item;
}

A

C

57
Q

Suppose we have a list groceryList shown 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?

A)
function removeGrocery(groceryList, item){
groceryList.pop();
}

B)
function removeGrocery(groceryList, item){
groceryList.remove(item);
}

C)
function removeGrocery(groceryList, item){
var index = groceryList.indexOf(item);
if(index != -1){
groceryList.remove(index);
}
}

D)
function removeGrocery(groceryList, item){
var index = groceryList.indexOf(item);
groceryList.remove(index);
}

A

C

58
Q

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?

A)
function printGroceries(groceryList){
println(“1. “ + groceryList[0]);
println(“2. “ + groceryList[1]);
println(“3. “ + groceryList[2]);
println(“4. “ + groceryList[3]);
println(“5. “ + groceryList[4]);
println(“6. “ + groceryList[5]);
}

B)
function printGroceries(groceryList){
for(var i = 1; i <= groceryList.length; i++){
println(i + “. “ + groceryList[i]);
}

C)
function printGroceries(groceryList){
for(var i = 0; i < groceryList.length; i++){
println((i + 1) + “. “ + groceryList[i]);
}

D)
function printGroceries(groceryList){
var counter = 1;
while(groceryList.length > 0){
println(counter + “. “ + groceryList.remove(0));
counter++;
}
}

A

C

59
Q

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 copy of the list list, and leaves list empty

Returns a new list containing the elements of the list list in reverse order, and leaves list empty

Returns a copy of the list list, and does not modify the contents of list

Returns a new list containing the elements of the list list in reverse order, and does not modify the contents of list

A

Returns a new list containing the elements of the list list in reverse order, and leaves list empty

Notes: Correct! 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.

60
Q

What is the output of the following program:

var groceries = [“bread”, “bananas”, “apples”, “cheese”, “peanuts”];
println(groceries.indexOf(“bananas”));

2

1

-1

true

A

1

61
Q

What kinds of elements can you store in data structures?

Strings of text and numerical values

Strings of text, numerical values, and graphical objects (Circles, Rectangles, etc)

Strings of text, numerical values, and booleans

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

A

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

62
Q

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

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

numbers[6] = 8;

numbers[0] = 8

numbers.push(8);

numbers.pop(8);

A

numbers.push(8);

63
Q

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[5];

var value = numbers.pop();

var value = numbers.push();

var value = numbers;

A

var value = numbers.pop();

64
Q

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

indexOf

find

findElement

index

A

indexOf

65
Q

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

A)
[12, 17, 65, 7, 30]
[88]

B)
[12, 17, 65, 7, 30]
88

C)
[17, 65, 7, 30, 88]
12

D)
[17, 65, 7, 30, 88]
[12]

A

B

66
Q

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?

arrList

17

Karel

false

A

Karel

67
Q

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 II

I and IV

II and IV

III and IV

A

I and IV

68
Q

The AP Exam uses the following format to iterate through a list.

FOR EACH item IN aList
{

<block>
}
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 count of odd numbers in list

This code displays the sum of the odd numbers in list

This code displays the sum of the numbers at the odd indices of list

This code results in an error
</block>

A

This code displays the sum of the odd numbers in list

69
Q

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 (isComposite(i))
{
APPEND(list, i)
}
i ← i + 1
}

DISPLAY LENGTH(list)

This code displays the total number of composite numbers between 1 and number

This code displays a list of all of the composite numbers between 1 and number

This code displays the sum of the composite numbers between 1 and number

This code results in an error

A

This code displays the total number of composite numbers between 1 and number

70
Q
A