Please answer each of these questions by clicking on the circle next to the letter. At the end, please click on the "Mark Answers" button to see how you did.
Look at the section of a program below. Underneath it are five diagrams where the diamond represents the condition and each rectangle represents an instruction. Which of the five diagrams best describes the program flow through the if statement?
if (var1 <= 100)
{ a = 6;
b = 4;
}
else
{ a = a + 100; // Add 100 to a
b = b - 100; // Subtract 100 from b
}
What number is displayed when the following program is run?
<script language="JavaScript">
var x = 2;
if (x > 2)
{ x = x + 1; // Add 1 to x
if (x < 3)
x = x + 2; // Add 2 to x
}
else
{ x = x + 2;
if (x == 4)
x = x + 1;
}
document.write(x);
</script>
2
3
4
5
6
(The instruction x = x + 1 (and similar ones) simply adds 1 to the value x. There is a more efficient way of doing this, by you won't meet that until the next chapter!)
The condition (vr < 10 || vr > 10) can be rewritten more clearly as which one of the following?
switch (variable)
{ case 20 : x = 2;
break;
case 21 : test = 3;
x = prompt("Enter new value of x","");
default : test = 4;
}
Which of the following statements is true?
The instruction does nothing unless variable is either 20 or 21.
If variable has the value 21, then test is set to the value 4.
If variable has the value 20, then variable test is not changed.
If variable has the value 16, then the switch statement does not display anything on the screen.
The switch statement does not test the value of test in order to decide what action to take.
In the following code segment, one of the lines contains an error which will prevent the program from working. Click on the letter which indicates which contains the error:
if (x == 6);
x = 2;
else
y = 10;
x = 12;
The following code section displays the value 2 only if which of the following options is true?
if (w == 4 || x == 2 && y == 3)
document.write(2);
w is 4, x is 2 and y is 3
Either w is 4 and x is 2, or y is 3.
Either w is 4, or both x is 2 and y is 3.
Either w is 4, or x is 2, or y is 3.
Either w isn't 4, or both x is some number other than 2 and y is some number other than 3.
I want to create a condition for an if statement that will succeed only if variable x is not within the range 10 to 20 (inclusive) and variable y is not within the range 20 to 30 (inclusive). Which of the following is the correct condition to use?
((x < 10 && x > 20) || (y < 20 && y > 30)) (x <= 10 || x >= 20 && y <= 20 || y >= 30) (x < 10 || x > 20 && y < 20 || y > 30) ((x <= 10 || x >= 20) && (y <= 20 || y >= 30)) ((x < 10 || x > 20) && (y < 20 || y > 30))
Here you see an extract from a program:
if (x < 5)
a = 6;
else
a = 7;
Which of the following code segments has exactly the same effect as that extract?
if (x >= 5)
a = 6;
else
a = 7;
if (x <= 5)
a = 7;
else
a = 6;
if (x > 5)
a = 6;
else
a = 7;
if (x >= 5)
a = 7;
else
a = 6;
if (x > 5)
a = 7;
else
a = 6;
Which of the following switch statements most closely matches the pattern shown in the following diagram?
switch (d) {
case 7 : alert("Statement 1"); break;
case 4 : alert("Statement 2"); break;
case 2 : alert("Statement 3"); break;
}
switch (d) {
case 7 : alert("Statement 1"); break;
case 4 : alert("Statement 2");
case 2 : alert("Statement 3"); break;
}
switch (d) {
case 7 : alert("Statement 1"); break;
case 4 : alert("Statement 2"); break;
case 2 : alert("Statement 3");
}
switch (d) {
case 7 : alert("Statement 1"); break;
case 4 : alert("Statement 2");
case 2 : alert("Statement 3");
}
switch (d) {
case 7 : alert("Statement 1");
case 4 : alert("Statement 2");
case 2 : alert("Statement 3");
}
In this multiple if statement, one of the statements can never be executed. Which statement is it?