Unit 6: Lists, Loops, Traversals Flashcards
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
South
Notes: Great job! After the first command, Karel will face West and after the second command Karel will face South.
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
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 many times should Karel turn left in order to turn right?
1
2
3
4
3
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
B
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();
for (var i = 0; i < 10; i++) {
move();
}
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
To repeat some code while a condition is true
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
D
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
Control the flow of the program; how the commands execute.
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
I, II, III, and IV
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.
No, because the tennisBalls() function has an incorrect if/else statement.
How many times will the following program print “hello”?
for (var i = 0; i < 5; i++) {
println(“hello”);
}
4
5
6
i
5
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
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 many times will the following code print “hello”?
for (var i = 3; i < 8; i++) {
println(“hello”);
}
3
5
6
8
5
How many times will the following code print “hello”?
for (var i = 0; i < 8; i += 2) {
println(“hello”);
}
JavaScript
0
2
4
8
4
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
10
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
10
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
This code will loop infinitely
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>
D
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.
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.
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)
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.
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
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).
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
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.
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.
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.
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.
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.
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
Karel moves until it is on a ball
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()
ballsPresent()
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)
function turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
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
6