Controlling Program Flow in JavaScript (Quiz)
Which of the following examples demonstrates correct syntax for a JavaScript if... else if ...else statement?
(NO ANSWER IS PROVIDED)
Which type of statement provides a way to exit a loop that would otherwise continue to execute?
The break statement
Question 6 : Consider the following code: for (x; y; z) { //one or more statements } What does x represent in this code?
The loop counter variable
The continue statement can be used only:
within a for loop or a while loop.
Question 20 : Consider the following code: var x = 0; do { document.write(x + " "); } while (x > 10) How many times will the code inside the curly brackets execute?
1 time
Consider the following code: for (var x = 0; x < 26; x+=5) { if (x == 10) continue; document.write(x + "<br/>"); } How many lines will this statement output to the document?
5 lines
Consider the following code: for (var x = 0; x < 26; x+=5) { document.write(x + "<br/>"); } How many lines will this statement output to the document?
6 lines
Question 25 : Which of the following is true about control structures?
Control structures make desicions based on Boolean values.
Consider the following code: var x = 0; while (x < 10){ document.write(x); } How many times will this statement execute?
Infinitely, until the browser is closed
What is the purpose of the do...while statement?
It executes the code once, then checks to see if the condition is true and continues to execute the code for as long as a condition is true.
Which JavaScript statement is used to force the flow of control back to the top of a loop?
The continue statement
Which JavaScript statement should you use to repeat a group of statements for some particular range of values?
The continue statement
How does the do...while statement differ from the while statement?
The do...while statement guarantees that the code within the curly braces will execute at least once, whereas the while statement does not.
Which JavaScript statement should you use to test for a single condition and branch to a process?
The if statement
Question 24 : Consider the following code: for (var x = 0; x < 26; x+=5) { document.write(x + " "); if (x == 10) break; } What will be the last number printed to the document?
The last number will be: 10
You are developing an application for enrollment in a grade school. You have included a variable for the grade level, and you want to assign a default value based on the age of the applicant. Which control structure will allow you to accomplish this in the most efficient way?
The switch statement
Consider the following code: var x = 100; while (x > 5) { x--; } What will be the value of x after execution?
The value will be: 5
Consider the following code: var userName = prompt("Enter User Name",""); var accessLevel = "None"; if (userName == "Admin") { accessLevel = "Full"; } else { accessLevel="Limited" } What will be the value of the accessLevel variable after execution if the user enters John as the user name?
The value will be: Limited
Consider the following code: var myVar = isNaN("One"); What will be the value of myVar after execution of this code?
The value will be: true
What is the purpose of the switch statement?
To compare a value against other values, searching for a match
What is the purpose of the while statement?
To execute a block of code for as long as a condition is true
Controlling Program Flow in JavaScript
To repeat a group of statements for some particular range of values
Consider the following code: var season = 5; var seasonName = "Summer"; switch (season) { case(1): seasonName = "Winter"; break; case(2): seasonName = "Spring"; break; case(3): seasonName = "Summer"; break; case(4): seasonName = "Fall"; break; default: seasonName = "Unknown"; } What is the value of seasonName after execution?
Unknown
Which code will display an alert if the variable timerMinutes has a value 10?
if (timerMinutes == 10) alert("Time Expired!");
Which of the following demonstrates the correct general syntax for a while statement?
while (boolean expression) { //statements to execute }