Multiple Choice questions on loops

Please answer each of these questions by clicking on the circled letters. At the end, please click on the "Mark Answers" button to see how you did.

  1. When the following loop terminates, what is the value of a?

    a = 10;
    do
     { a /= 2;
       y = a * 10;
     }
    while (a < 1); 
    
    0.625
    1
    1.25
    5
    10

  2. How many times is the string Hello! written on the screen?

    for (x = 1; x < 10; x++)
     for (y = 1; y < 5; y++)
      document.write("Hello!");
    
    36
    40
    45
    50
    The program does not give us enough information to work it out.

  3. Under which of the following circumstances will the following loop definitely terminate?

    do
     { s = prompt("Please enter a number","");
       n = parseFloat(s);
     }
    while (n < 40 || n < 50); 
    
          A number smaller than 40 is entered.
    A number bigger than 50 is entered.
    A number bigger than 40 is entered.
    A number smaller than 50 is entered.
    A number between 40 and 50 is entered.

  4. Which of the following loops must control one or more statements enclosed within curly brackets?

    while loops only
    while loops and do-while loops
    for loops only
    do-while loops only
    for loops and do-while loops
         

  5. Consider the following line which has been extracted from a program. How many times does the loop execute?

    for (count = 0; count < 31; count+=2)
          15
    16
    30
    31
    We cannot tell just from the line given above.

  6. Consider this segment of a program. Which of the accompanying statements is true?

    a = 6;
    while (a < 1000)
      document.write("The value of a is " + a + "<p />");
      a *= 10;
    alert("The loop has terminated");
    
          The loop displays three values for a, namely 6, 60 and 600.
    The loop displays four values for a, namely 6, 60, 600 and 6000.
    The loop runs forever (until the browser is shut down).
    The browser reports an error because there are no curly brackets.
    The loop doesn't execute even once.

  7. Which of the following diagrams best describes the flow of this program loop?

    do
     { var *= 20;
       temp += var;
     }
    while (temp < 1);
    
             

  8. Tom wants to write a program that asks the user for a number between 1 and 10 (inclusive) and will not accept any number less than 1 or more than 10. If the user enters such a number, the program should ask for the number again. However, when he writes the program, he finds that it asks for the number the first time and if the user enters an unsuitable number, the program appears to stop functioning at all until the browser is cancelled. Which of the following sections of code could Tom have written?

    while (n < 1 || n > 10)
     { s = prompt("Enter a number from 1 to 10","");
       n = parseFloat(s);
     }
    

    do
     { s = prompt("Enter a number from 1 to 10","");
       n = parseFloat(s);
     }
    while (n < 1 || n > 10);
    

    s = prompt("Enter a number from 1 to 10","");
    do
     { n = parseFloat(s);
     }
    while (n < 1 || n > 10);
    

    while (n >= 1 && n <= 10)
     { s = prompt("Enter a number from 1 to 10","");
       n = parseFloat(s);
     }
    

    do
     { s = prompt("Enter a number from 1 to 10","");
       n = parseFloat(s);
     }
    while (n >= 1 && n <= 10);
    

  9. Consider this for loop. Why does the loop never terminate?

    for (count = 1; count != 20; count++)
     { alert("Count is " + count);
       count++;
     }
    

    the variable count is always less than 20.
    the condition part of the loop is always true.
    the variable count is always greater than 20.
    JavaScript does not allow you to alter the value of count within the body of the loop.
    the condition part of the loop fails the first time that it is tested.

  10. What is the best way of describing the action of this loop?

    var x = 10;
    var y = 0;
    while (x > 0)
     { y += x;
       x--;
     }
    document.write("The answer is " + y);
    

    It counts down from 10 to 0.
    It counts up from 0 to 10.
    It counts down from 10 to 1.
    It adds all the whole numbers from 0 to 10.
    It adds all the whole numbers from 1 to 10.

Go back Go back