Multiple Choice questions on if statements

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.

  1. 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
     }
    

    A B C
    D E

  2. 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>
    
    A 2
    B 3
    C 4
    D 5
    E 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!)

  3. The condition (vr < 10 || vr > 10) can be rewritten more clearly as which one of the following?

    A (vr == 10)
    B (vr != 10)
    C (vr >= 10)
    D (vr <= 10)
    E (vr = 10)

  4. Consider this switch statement:

    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?

    A The instruction does nothing unless variable is either 20 or 21.
    B If variable has the value 21, then test is set to the value 4.
    C If variable has the value 20, then variable test is not changed.
    D If variable has the value 16, then the switch statement does not display anything on the screen.
    E The switch statement does not test the value of test in order to decide what action to take.

  5. 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); A
       x = 2; B
    else C
       y = 10; D
       x = 12; E

  6. 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.

  7. 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?

    A ((x < 10 && x > 20) || (y < 20 && y > 30))
    B (x <= 10 || x >= 20 && y <= 20 || y >= 30)
    C (x < 10 || x > 20 && y < 20 || y > 30)
    D ((x <= 10 || x >= 20) && (y <= 20 || y >= 30))
    E ((x < 10 || x > 20) && (y < 20 || y > 30))

  8. 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?

    A if (x >= 5)
      a = 6;
    else
      a = 7;
    B if (x <= 5)
      a = 7;
    else
      a = 6;

    C if (x > 5)
      a = 6;
    else
      a = 7;
    D if (x >= 5)
      a = 7;
    else
      a = 6;
    E if (x > 5)
      a = 7;
    else
      a = 6;

  9. Which of the following switch statements most closely matches the pattern shown in the following diagram?
    A switch (d) {
     case 7 : alert("Statement 1"); break;
     case 4 : alert("Statement 2"); break;
     case 2 : alert("Statement 3"); break;
    }

    B switch (d) {
     case 7 : alert("Statement 1"); break;
     case 4 : alert("Statement 2");
     case 2 : alert("Statement 3"); break;
    }

    C switch (d) {
     case 7 : alert("Statement 1"); break;
     case 4 : alert("Statement 2"); break;
     case 2 : alert("Statement 3");
    }

    D switch (d) {
     case 7 : alert("Statement 1"); break;
     case 4 : alert("Statement 2");
     case 2 : alert("Statement 3");
    }

    E switch (d) {
     case 7 : alert("Statement 1");
     case 4 : alert("Statement 2");
     case 2 : alert("Statement 3");
    }

  10. In this multiple if statement, one of the statements can never be executed. Which statement is it?

    if (m > 2 && n < 10)
    { if (m > 3 || n > 1)
       alert("Statement A"); A
      else
       alert("Statement B"); B
    }
    else
    { if (m > 3 && n < 9)
       alert("Statement C"); C
      else
       alert("Statement D"); D
      alert("Statement E"); E
    }