1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Which of the following is true about while loops in Javascript?
While loops run as longs as a given boolean condition is true.
Tiffany is writing a program to help manage a bake sale. She writes the following code which
prompts the user to enter the total number of items a person is buying and then a loop repeatedly
prompts for the cost of each item. She wrote the code but there is a problem: it runs in an infinite
loop. How can Tiffany change her code so it doesn't loop forever?
0 var numItems = promptNum("How many items?");
1 var total = 0;
2 while (numItems > 0){
3 total = total + promptNum("Enter next item price");
4 }
5 console.log("The total is" + total);
Add after line 3: numItems = numItems - 1;
What value will be displayed after the loop has executed?
var a = 5;
while (a > = 3){
a = a-1;
}
console.log(n)
2
What will be displayed as a result of the code below executing?
var a = 5;
while (a < 5) {
a = a - 1;
}
console.log(a)
5
A robot is represented as a triangle in a grid of squares. The initial position and direction of the robot is
shown below. The robot can move onto a white square, but cannot move into a black region.
Consider the following program:
REPEAT 4 TIMES
{
REPEAT UNTIL (NOT CAN_MOVE (forward))
{
MOVE_FORWARD ()
}
REPEAT UNTIL ( CAN_MOVE(forward) )
{
ROTATE_LEFT ()
}
}
After running the code above, what will the ending location and direction of the robot be?
Robot will be in the bottom left corner of the screen, tip pointing up towards the top.
What will be displayed as a result of the javascript code below executing?
var a = 0;
while( a != 5 ){
a = a+2;
}
console.log(a);
Infinite Loop
Study the code segment below. Values of x and y will be displayed multiple times during the
running of the program. From the list of possible outputs, choose two of the outputs that are
impossible for this code to produce.
x ← 0
REPEAT_UNTIL( x = 3 )
y ← 0
x ← x+1
REPEAT_UNTIL ( y = 3 ){
y ← y+1
DISPLAY( x + ", " + y)
}
}
- 0,0
- 2,4
The counter variable in the code below increments by 1 each time through the loop. What will
the value of counterbe after the following loop executes?
var counter = 0;
var n = 6;
while(n > 0){
n = n 2;
counter = counter + 1;
}
console.log(counter)
3
A statistician would like to simulate rolling a single die until she has rolled the die a total of 100
times, or she rolls a six a total of 25 times, whichever comes first. Which boolean expression should
she use for the
var rolls = 0;
var sixes = 0;
while (
var nextRoll = randomNumber(1, 6);
if(nextRoll == 6)
{
sixes = sixes + 1;
}
rolls = rolls + 1;
}
console.log(rolls);
console.log(sixes);
rolls < 100 && sixes < 25
Given the following array, what will the code segment below display after being run? The initial
state of the array is shown for you to use as a reference.
Index: 1, 2, 3, 4, 5, 6
Data: 5, 8, 3, 4, 2, 1
data ← [5, 8, 3, 4, 2, 1]
a ← data[5]
b ← data[2]
DISPLAY(a + b)
10
What is the output of the code segment below? The initial state of the array is shown for you to use
as a reference.
Index: 1, 2, 3, 4, 5, 6
Data: 5, 8, 3, 4, 2, 1
i ← 4
a ← data[i]
b ← data[i + 1]
DISPLAY(a + b)
6
What is stored in list data after the following code segment is run?
Index: 1, 2, 3
Data: 0, 1, 2
data ← [0,1,2]
INSERT(data, 1, 3)
INSERT(data, 2, 4)
INSERT(data, 3, 5)
DISPLAY(data)
3, 4, 5, 0, 1, 2
What does the following code segment display?
Index: 1, 2, 3, 4, 5
Data: 3, 5, 8, 2, 1
data ← [3, 5, 8, 2, 1]
REMOVE(data, 3)
DISPLAY(data[3])
2
What is contained in the list data after the following code segment is run?
Index: 1, 2, 3, 4, 5
data: 10, 45, 38, 16, 23
data ← [10, 45, 38 16 23]
REMOVE(data, 2)
REMOVE(data, 3)
10, 38, 23
A programmer created a list of all of her sibling's ages:
1 2 3
ages: 10 12 17
Unfortunately, she made a typo! Her middle sibling is 13 years old. Which of the following solutions
will notfix the typo?
INSERT the value 13 at the index of 2
Consider the following code segment that appends numbers to a list one at a time.
data ← [ ]
APPEND (data, 2)
APPEND (data, 6)
APPEND (data, 9)
APPEND (data, 3)
APPEND (data, 1)
What is the value of data[2]after the code above is executed (Assume indexing begins
with 1)
6
You are trying to write a function swap(list, a, b), which will swap the position of the two values at indexes and bin the list.
Example: before and after a call to swap(list, 1, 2)on the list shown below:
Swap(list, 1, 2)
Before swap:
Index: 1 2 3
List: 30 75 80
After swap:
Index: 1 2 3
list: 75 30 80
The function header is defined for you. Choose the 3 lines of code that will perform the swap correctly.
function swap(list, a, b){
}
var temp = list[b];
list[b] = list[a];
list[a] = temp;
The Javascript code below creates an array of numbers and makes a call to the mystery function. The code
for the mystery function is also shown and it makes use of the swap function which you should
assume works as described in the previous question.
var data = [3, 8, 1];
mystery(data);
function mystery(data){
if(data[1] < data[0]){
swap(data, 0, 1);
}
if(data[2] < data[1]){
swap(data, 1, 2);
}
if(data[1] < data[0]){
swap(data, 0, 1);
}
}
Index: 0, 1, 2
data: 3, 8, 1
Choose the option that shows the state of the array (along with its corresponding explanation) after
the code is executed.
data: 1, 3, 8
Mystery method sorts 3 elements into
ascending order