WorksheetsAP CSP: Unit 5 Test Review - List, Loops, & Traversals
Total questions: 50
Worksheet time: 47mins
OnEvent
Design Mode
Consider the JavaScript code segment below. Which statement should be used in place of <missing code> such that the alarm is set to 9:00 am on weekends, and 6:30 am on weekdays.
var day = prompt("What day is it tomorrow?");
if ( <missing code> ){
setAlarm = "9:00am";
}
else {
setAlarm = "6:30am";
}
day == "Saturday"
day == "Sunday"
(day == "Saturday") || (day == "Sunday")
(day == "Saturday") && (day == "Sunday")
What is an event-driven program?
A program that does not implement algorithms
A program in which all portions of the code will always be executed while it is running
A program written to respond to specified events by executing a block of code of function associated with the event
A program that can only be run once and only have one possible outcome, regardless of the user interactions.
Which of the following is true about element IDs?
An element with a unique ID must always have an event handler associated with it.
An element that needs to be triggered by onEvent does not need a unique ID.
Two or more onEvent calls may reference the same ID
IDs cannot be referenced multiple times
"You win!" never displays. What line is responsible for this error?
1
3
4
5
Which of the following methods expects the new element as an argument and adds the new element to the end of the list?
append
expand
insert
search
Which type of function usually tests its argument for the presence or absence of some property?
return
main
Boolean
square
What is first index value of an array?
-1
0
1
undefine
What is the length of this array?
What is proper syntax for declaring a array in JavaScript?
var arrayName =[ ];
var arrayName[ ] ={ };
array arrayName ={ };
obj var arrayName =[ ];
What is the highest position in this array?
HINT: Remember arrays start at position 0!
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
For this scenario related to turtle/robot drawing, indicate whether it is better to write a loop or a function (or a set of functions) to handle the task:
Drawing a hexagon (six-sided shape)
Loop
Function
A Boolean expression is an expression that evaluates to which of the of the following?
Yes/Maybe/No
True/False
Any Integer
Integers between 1 and 10
Any single character
Which vocabulary word is:
a common method for referencing the elements in a list or string using numbers
list
element
index
Which vocabulary word is:
an ordered collection of elements
list
element
index
Match the index with the value.
var myNumbers = [32, 64, 33, 0, 15, 26, 3]
myNumbers[4]
64
15
0
32
//Consider this code:
var list = [10, 12, 14, 16, 18];
appendItem(list, 3);
insertItem(list, 2, 20);
removeItem(list, 0);
//What is printed to the console?
console.log(list[0]);
(a)
//Consider this code:
var list = [10, 12, 14, 16, 18];
appendItem(list, 3);
insertItem(list, 2, 20);
removeItem(list, 0);
//What is printed to the console?
console.log(list.length);
(a)
//Consider this code.
var beginning = "My name is ";
var end = "Mrs. Anderson";
var sentence = beginning + end;
//What is printed to the console?
console.log(sentence.length);
name
name is
y name
23
n
4
7
9
10
ab
Which type of loop iterates until condition become False?
FOR loop
A count-controlled loop
Repeat-once loop
WHILE loop
In a while loop the minimum time a code will be executed is
Once
Twice
0
A condition is
a statement that is either True or False
a string
an integer
a list
What does the code output?
0
1
2
3
1
2
3
0
1
2
Print i as long as i is less than 6.
What will be the code?
i = 1
while i < 6:
print(i)
i += 1
i = 1
when i < 6;
print(i)
i += 1
i = 1
i < 6:
print(i)
i += 1
What is the output of the below Java code?
int score=1;
for(; true; score++)
{
System.out.print(score +",");
if(score > 3)
break;
}
1,2,3,
1,2,3
1,2,3,4,
1,2,3,4
This loop can only be used when the programmer knows the number of times a code is to be repeated
while
for
do while
repeat until
FOR loops are
loops which run an unknown number of times
loops which run for a specific number of times
the same as if statements
not part of programming
A school is developing a program to keep track of information about students and their class schedules. In which of the following instances would a data abstraction be most helpful?
The program includes individual variables to store the names of each student rather than a single list of students.
A program includes multiple comments that could be combined into a single comment
A program includes repeated programming statements that could be moved inside a loop
A program includes repeated code that could be moved inside a function
Which of the following will result in ONLY all the items in list being printed to the console if placed where the program reads <INSERT CODE> and the program is run?
i < list{list.length]
i < list.length
i < list[0]
i < list [1]
wordList is a list of words that currently contains the values
["tree", "rock", "air"]
Which of the following lines will result in the list containing the values ["air", "rock", "air"]
wordList[0] = wordList [2]
wordList[2] = wordList [0]
insertItem (wordList, 0, "air")
removeItem(wordList, 0)
What will be displayed in the console when this program runs?
2
3
20
30
What will be displayed when this program finishes running?
3
6
7
5
What will the following program display in the console?
0
1
2
3
0
1
2
3
4
1
2
3
1
2
3
4
What will the following program display in the console?
0
5
10
15
If the program above is run, what will be displayed in the console?
[apple, dream]
[bug, car, ear]
bug, car ear, food]
[apple, dream, food]
A restaurant knows from historical data that 60 out of 100 of its customers purchase a full meal while 40 out of 100 only order a side dish. The program above is intended to simulate the orders of 1000 customers.
Which of the following can be used to replace <MISSING CODE> so that the simulation works as intended?
random(1,100) <= 60
random(1,100) = 40
random(1,100) <=40
random(1,100) = 60
