CMSC104 Fall 2010

Exam 2 Sample Questions

True/False Questions

Circle TRUE or FALSE for each of the following questions.

  1. TRUE/FALSE
    int is a reserved word in JavaScript.

  2. TRUE/FALSE
    2doList is a legal identifier in JavaScript.

  3. TRUE/FALSE
    JavaScript programs are interpreted in the browser.

  4. TRUE/FALSE
    If n is an integer, the value of the expression n % 5 is 0 whenever n is a multiple of 5.

  5. TRUE/FALSE
    If n is an integer, the value of the expression n % 7 is 0 whenever n is a not a multiple of 7.

  6. TRUE/FALSE
    If n is an integer, the value of the expression n % 6 is always less than 6.

  7. TRUE/FALSE
    In the JavaScript programing language, variable names can have at most 8 characters.

  8. TRUE/FALSE
    HTML is used to show the semantic meaning of the text on a Web page.

  9. TRUE/FALSE
    CSS allows us to change the presentation of an HTML document.

  10. TRUE/FALSE
    What is the value of the expression 2 * 6 - 5 + 8 % 5

  11. TRUE/FALSE
    What is the value of the expression 3 * 5 - 19 + 7 % 4

  12. TRUE/FALSE
    What is the value of the expression 5 * 5 - 19 - 6 % 7

  13. TRUE/FALSE
    What is the value of the expression 3 * 5 - 12

  14. TRUE/FALSE
    The Internet and the WWW are interchangeable.

  15. TRUE/FALSE
    If the variables a and b are holding the values 5 and 2, respectively, the expression
    a / b produces the result 2.5

  16. TRUE/FALSE
    A Web page is stored inside of the Web browser.

  17. TRUE/FALSE
    The CSS comment delimeters are /* and */.

  18. TRUE/FALSE
    Any code that can be written as a for loop can also be written as a while loop.

  19. TRUE/FALSE
    All variables should be declared before they are used

  20. TRUE/FALSE
    <p>...</p> is an example of a self closing tag.

  21. TRUE/FALSE
    Each call to document.write() causes printing to start at the beginning of the next line.

  22. TRUE/FALSE
    JavaScript is a client-side scripting language.

  23. TRUE/FALSE
    Displaying text with an alert() has the same effect as displaying text with document.write().


Multiple Choice Questions

  1. HTML stands for:

  2. Which of the following denotes a comment in JavaScript?

  3. What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1

  4. A valid sentinel value for a program getting grades as input would be
  5. A valid sentinel value for a program getting ages as input would be

  6. Execution of a break statement in the body of a while loop

  7. Execution of a continue statement in the body of a for loop

  8. Determine the output of the code fragment.
    	var a = 5, b = 4, c = 1 ;
    
    	if ( a < b + 1 ) {
    	   alert("tea, earl grey, hot!");
    	} else if ( a >= b + c ) {
    	   alert("Ahead warp factor 9. Engage!");
    	} else if ( a % b >= c ) {
    	   alert("Warp core breach in 20 seconds!");
    	} else {
    	   alert("I sense a lot of anxiety in this room.");
    	}
    

  9. What is the effect of the following code?
    	var i;
    
    	for (i = 1 ; i <= 15 ; i = i + 1)
    	{
    	   if ( i % 3 == 0)
    	   {
    	      alert(i + " ");
               }
    	}
    

  10. The URL of a Web site is also called:

  11. JavaScript and Java


Short Answers

In the following questions, no syntax errors have put in deliberately. (So, "there is a syntax error" is NOT the right answer and will receive no credit.) Please mark your final answer clearly. In order to receive partial credit, you must show your work. For each problem that asks for a code segment, you do not have to show the entire program, but you should show any variable declarations. If the problem says to write a program, you should begin with the script tags and show everything in between.

  1. Write a JavaScript code segment using a for loop that prints the numbers 1 to 10, inclusive, side-by-side on the same line with 1 space between each number

  2. Write a JavaScript code segment using a while loop that prints the even numbers between 1 and 10, inclusive, side-by-side on the same line with 1 space between each number

  3. Write a JavaScript code segment using a for loop that prints a 7 x 7 square out of asterisks.

  4. Write a JavaScript code segment using a for loop that prints the following sequence of numbers: 3, 8, 13, 18, 23

  5. Write a JavaScript code segment using a while loop that calculates and and prints the sum of the even integers from 2 to 30, inclusive.

  6. Write a JavaScript code segment using a for loop that calculates and prints the product of the odd integers from 1 to 15, inclusive.

  7. All programs can be written in terms of three control structures: _____________________, ______________________ and ___________________.

  8. Rewrite the following code segment that uses a for loop as an equivalent code segment that uses a while loop.
        for (i = 1; i <= 10; i = i + 4)
        {
            if (i % 5 == 0)
    	{
                alert(i + " is a multiple of 5.");
            }
            else
            {
                alert(i + " is not a multiple of 5.");
            }
        }
    

  9. Rewrite the following code segment correcting all errors.
    <script type="text/javascript">
      <!--
        /* This is my JavaScript comment
         * that spans more than 1 line.
         *
        document.write("<h1>Hello!<h1>");
      </script>
    //-->
    
    

  10. This question tests your knowledge of nested for loops. Please show line breaks in the proper places in the output. What is the output of the following code?
        var i, j;
    
        for (i = 0; i <= 3; i = i + 1)
        {
            for (j = 0; j <= 4; j = j + 1)
    	{
    	    if ( (i + 1 == j) )
    	    {
    	        document.write("+");
    	    }
    	    else
    	    {
    	        document.write("o");
    	    }
    	}
    	document.write("<br />");
        }
    
  11. In the nested for loop code above, how many times is the if statement executed?

  12. Write a simple loop that prints out the following sequence of numbers: 18 23 28 33 38 43 48

  13. Write a program that asks the user for two numbers and displays their product.

  14. Write a program that prompts the user for 10 values and then displays their sum.

  15. In the list below, circle the items that are NOT legal identfiers in JavaScript.
    	3d               XyYzZx         ABC123
    
    	__yes            star*it        m
    
    	me_to-2          main           money$
    

  16. Determine the output of the following program segment:
    
    	 var a = 2, b;
    
    	 b = a;
    	 document.write("b = " + b + "<br />");
    
    	 b = b + 1;
    	 document.write("b = " + b + "<br />");
    
    	 b = a / b;
    	 document.write("b = " + b + "<br />");
    
    	 document.write("a = " + a + "<br />");
    
    
    
  17. Given:
    var a = 3, b = 7, c = 15;

    Evaluate each expression below as TRUE or FALSE.

    
    	_______   (1)    c / b  ==  2
    
    	_______   (2)    c % b  <=  a % b
    
    	_______   (3)    b + c / a  !=  c - a
    
    


    Determine the output generated by the following JavaScript and HTML code segments, assuming the surrounding program code (whatever it is) works correctly.


  18.     document.write("We sell ");
        document.write("C shells<br />");
        document.write("by the<br />sea shore<br />");
    


  19.     <ul>
           <li>apples</li>
           <li>oranges</li>
           <li>pears</li>
        </ul>
    


  20.     var i = 1, number = 0;
    
        do
        {
          number = number + i;
          i = i + 1;
        } while(i < 5);
    
        document.write("number is: " + number);
    
    


  21.     var x = 1;
    
        if (x > 3)
        {
            if (x > 4)
            {
                document.write("A");
            }
            else
            {
                document.write("B");
            }
        }
        else if (x < 2)
        {
            if (x != 0)
            {
                document.write("c");
            }
        }
        document.write("d");
    


  22.     var j, k;
    
        for (j = 1; j <= 4; j = j + 1)
        {
            for (k = 1; k <= j; k = k + 1)
            {
                document.write("*");
            }
    	document.write("<br />");
        }
    


  23.     var g, h;
    
        for (g = 1; g <= 3; g = g + 1)
        {
            for (h = 0; h < 3; h = h + 1)
            {
                if (g != h)
                {
                    document.write("o");
                }
                else
                {
                    document.write("x");
                }
            }
    	document.write("<br />");
        }