Unit 5 Quiz Review ANSWERS

Unit 5 Quiz Review ANSWERS
1. Which of the following is FALSE about event-driven programs?
A. Event-driven programs do not implement algorithms.
B. Some portions of an event-driven program may never execute while the program is running.
C. An event-driven program is written to respond to specified events by executing a block of
code or function associated with the event.
D. The order in which an event-driven program will run cannot always be known ahead of
time.
E. Event-driven programs can be run multiple times with different outcomes, based on user
interactions.
Answer: A: Event driven programs will implement algorithms as long as the event (click,
keypress, mouseover) you accounted for occurs.
2. Which of the following is FALSE about element IDs?
A. An element with a unique ID must always have an event handler associated with it.
B. Any element that needs to be triggered by onEvent must have a unique ID.
C. Two or more onEvent calls may reference the same ID.
D. While not a requirement, IDs should be meaningful and descriptive.
E. IDs allow a programmer to reference interface elements within their code.
Answer: A: Just because an element on screen has an ID that doesn’t mean it needs to be
referenced in the code
3. Consider the code segment below:
What are the values of a, b, and c after this code segment has been run?
A. a:4 , b:4 , c:4
B. a:4 , b:8, c:8
C. a:4 , b:8 , c:12
D. a:4 , b:8 , c:16
E. a:0 , b:3 , c:4
Answer: B: Keep track of the variables values as they change line by line.
4. Jasmine is writing a shopping app. She has created a variable to keep track of the number of items in
the shopping cart. Every time someone clicks the “addItemButton”, she would like the variable to
increase by 1.
What code should Jasmine insert where it says in order for her app to work?
A. cart total = 1;
B. cartTotal + 1;
C. cartTotal = cartTotal +1;
D. var cartTotal = cartTotal + 1;
E. var cartTotal + 1;
Answer: C: Proper way to add one to a variable. OR carTotal++
5. What is the output to the console after the following code segment is executed?
1 var x = 10;
2 increase();
3 x = x+3;
4 console.log(x);
5 function increase(){
6 x = 5;
7}
A. 5
B. 8
C. 10
D. 13
E. Error. Cannot make a new variable x inside function increase()
Answer: B: The function occurs before 3 is added to x. In the function increase, x gets the value of 5
before x gets 3 added to it.
6. What is the output to the console after the following code segment is executed?
1 fiveMore();
2 function fiveMore(){
3 var x = 5;
4}
5 var y = 3 + x;
6 console.log(y);
A. -2
B. 3
C. 5
D. 8
E. Error. Unknown Identifier: x
Answer: E: Since x was created inside the function fiveMore() it is a local variable meaning it
cannot be referenced outside of the function it was created in.
7. Which of the following statements about strings in JavaScript is FALSE?
A. Strings consist of a sequence of concatenated characters.
B. Strings are indicated by quotation marks.
C. Strings with numerical digits in them are invalid.
D. A string can be empty, meaning that it contains nothing.
E. Strings sometimes include spaces.
Answer: C: Strings can have numerical digits. Numerical digits are also just characters
8. Which of the following JavaScript statements will result in the following output being displayed to the
console?
Hello!
How are you?
A. console.log(“Hello! \tHow are you?”);
B. console.log(“Hello! \bHow are you?”);
C. console.log(“Hello! \newLineHow are you?”);
D. console.log(“Hello! \nHow are you?”);
Answer: D: \n is the only way to create a new line inside your strings.
9. What is the output of the following JavaScript code segment?
var num = 5;
var str = “hello”;
var result = num + str;
console.log(result);
A. 5hello
B. 5 + hello
C. error on num + str: type mismatch
D. result
Answer: A: String concatenation. If you add a number to a string, they will simply combine to
make one string that contains the number and the original string itself.
10. A Boolean expression is an expression that evaluates to which of the following?
A. Yes/Maybe/No
B. True/False
C. Any Integer
D. Integers between 1 and 10
E. Any single character
Answer: B: Only True or False
11. What is the expected output of the given JavaScript code segment?
var age = 35
IF (age < 35){
DISPLAY (You are not old enough to be President.)
}
ELSE {
DISPLAY (You are old enough to be President!)
}
A. 35
B. true
C. You are not old enough to be President.
D. You are old enough to be President!
Answer: Since age isn’t less than 35, the code inside the ELSE statement is executed.
12. 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”;
}
A. day == “Saturday”
B. day == “Sunday”
C. (day == “Saturday”) || (day == “Sunday”)
D. (day == “Saturday”) && (day == “Sunday”)
E. day != “Monday”
`
Answer: C: If day is “Saturday” or “Sunday” you can wake up at 9. || means or.
13. Assume that a variable temperature is assigned like this:
var temperature = 30;
Choose the Boolean expression that evaluates to FALSE.
A. (temperature > 0) && (temperature < 32)
B. (temperature == 0) || (temperature < 32)
C. (temperature != 0) && (temperature < 32)
D. (temperature == 0) || (temperature > 32)
Answer : D: Since it is an OR statement then both conditions must be false for the entire
boolean expression to be false. Tembeature isn’t equal to zero and temperature isn’t greater
than 32 degrees
14. What is displayed in the debug console based on the following code segment?
var name = “Hello”;
console.log( name.length );
A.
B.
C.
D.
Hello
4
5
name.length
Answer: C: Since “Hello” has 5 characters then the length of the string is 5 so 5 will be outputted
since that’s the length of the string.
15. Which of the following is true about while loops in JavaScript?
A. While loops terminate after a fixed number of loops that is pre-determined
B. While loops terminate after a fixed number of loops that is determined after the loop
executes once.
C. While loops run as long as a given boolean condition is true.
D. While loops run as long as a given boolean condition is false.
E. While loops are known as “forever loops” and never stop until the program is halted by the
user.
Answer: C: While loops will continue to repeat as long as the boolean condition is true
16. What value will be displayed after the loop has executed?
var a = 5;
while (a > = 3){
a = a-1;
}
console.log(a)
A. 5
B. 4
C. 3
D. 2
E. Infinite loop
Answer: D: For the while loop to continue a has to be greater than or equal to 3 so anything less
will make the loop terminate. Since during each iteration of the loop a is decreased by one,
when the loop terminates a is equal to 2.
17. Which of the following variables are Global variables in the code segment below?
var score = 0;
function increaseScore(){
var amount = randomNumber(1,10);
score = score + amount;
}
A. amount
B. score
Answer: A: Since amount was created inside a function it is a local variable. Score was created outside of
any function, loop, or event so it is a global variable.