CMSC 104, Fall 2010

Exam 3 Sample Questions

True or False

Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer is considered wrong.

  1. TRUE/FALSE
    switch is a legal identifier in JavaScript.

  2. TRUE/FALSE
    count++; is equivalent to count = count + 1;

  3. TRUE/FALSE
    Every function must have a return statement.

  4. TRUE/FALSE
    In JavaScript, the function keyword is used to define a function.

  5. TRUE/FALSE
    When a function is called, its local variables initially contain the value 0.

  6. TRUE/FALSE
    Actual and formal function parameters must have the same name.

  7. TRUE/FALSE
    A function always returns the value 0.

  8. TRUE/FALSE
    When a function is called, the number of arguments passed should match the number of formal parameters.

  9. TRUE/FALSE
    After a function has returned, the values stored in its local variables are copied into variables in the calling function that have the same name.

  10. TRUE/FALSE
    The values stored in the local variables of a function are not saved when the function exits.

  11. TRUE/FALSE
    JavaScript is an interpreted language.

  12. TRUE/FALSE
    If the variables a and b are holding the values 0 and 1, respectively, what is the value of the following expression : ( a || b ) ?

  13. TRUE/FALSE
    If the variables a and b are holding the values 0 and 1, respectively, what is the value of the following expression : ( a && b ) ?

  14. TRUE/FALSE
    If the variables a and b are holding the values 1 and 2, respectively, what is the value of the following expression : ( a + b ) ?

  15. TRUE/FALSE
    If the variables a and b are holding the values 0 and 6, respectively, after the statement a += b++ the new values of a and b are both 7.

  16. TRUE/FALSE
    All array elements are initialized to 0 when the array is created.

  17. TRUE/FALSE
    The default case is required in the switch selection structure.

  18. TRUE/FALSE
    The default case must be the last case in the switch selection structure.

  19. TRUE/FALSE
    The break statement is required in the default case of the switch selection structure.

  20. TRUE/ FALSE
    An array is used store a list of items.

  21. TRUE/ FALSE
    Array subscripts must always start at 0.

  22. TRUE/ FALSE
    The function keyword is used when making a function call.

  23. TRUE/ FALSE
    The fourth element of an array named bob is bob[4]

  24. TRUE/ FALSE
    Arrays must always be initialized using a loop.


Multiple Choice Questions

Circle the letter of the best choice.

  1. Given the function call,
        PrintGreeting();
    how many arguments does the function have?
  2. [a.] 1
    [b.] 0
    [c.] 2
    [d.] none of the above

  3. The function header comment

    [a.] tells the browser how to use the function.
    [b.] gives information about the function to the person reading the code.
    [c.] is necessary for the program to run.
    [d.] all of the above.

  4. What is the output of the following code fragment?
  5. 	var a = 3, b = 17 ;
    	
    	a = b % a ;
    	b = ++a + 5 ; 
    	alert("a = " + a + ", b = " + b);
    

    [a.] a = 3, b = 8
    [b.] a = 2, b = 7
    [c.] a = 3, b = 7
    [d.] a = 2, b = 8
    [e.] None of the above.

  6. The output of the following program fragment is:
  7. 	test = 1 ;
    	if (test = 2) {
    	   alert("Beam down the landing party.\n") ;
    	} else if (test == 1) {
    	   alert("Jim, he's dead.\n") ;
    	} else if (test == 0) {
    	   alert("Scotty, four to beam up.\n") ;
    	}
    

    [a.] Beam down the landing party.
    [b.] Jim, he's dead.
    [c.] Scotty, four to beam up.
    [d.] No output is produced

  8. Which of the following statements will print out the variable x with exactly 1 decimal place, e.g. 123.4
  9. [a.] alert (x.toFixed(1));
    [b.] alert (toFixed(x) = 1);
    [c.] alert (x.toFixed(x));
    [d.] alert (toFixed(1));

  10. A single JavaScript statement that assigns the sum of x and y to z and then increments the value of x by 1 after the calculation.
  11. [a.] z = y + (x + 1);
    [b.] z = ++x + y;
    [c.] z = y + x++;
    [d.] none of the above

  12. A single JavaScript statement that decrements the variable x by one then subtracts it from the variable total is
  13. [a.] total = total - x--;
    [b.] total -= --x;
    [c.] total -= (x - 1);
    [d.] none of the above

  14. A single JavaScript statement that adds the variable x to the variable total and then decrements x by one is
  15. [a.] total = total + x--;
    [b.] total += --x;
    [c.] total += (x - 1);
    [d.] none of the above

  16. Assuming that the variables high and low contain the values 3 and 5, respectively, then the new values of high and low after the statement high *= --low + 4 are
  17. [a.] high = 24 and low = 4
    [b.] high = 24 and low = 8
    [c.] high = 19 and low = 4
    [d.] high = 19 and low = 8

  18. How many arguments does the following function call have?
     ave = FindAverage(num1, num1);

    [a.] 1
    [b.] 0
    [c.] 3
    [d.] 2

  19. What is the index of the last element in an array of size 10?
  20. [a.] 0
    [b.] 10
    [c.] 9
    [d.] 1

  21. The character used to display a newline in an alert statement is
  22. [a.] \n
    [b.] /n
    [c.] enter
    [d.] <br />

  23. The tag used to display a newline in a document.write() statement is
  24. [a.] <hr />
    [b.] <newline></newline>
    [c.] <nl />
    [d.] <br />

  25. Assuming that the variables a and b contain the values 20 and 3, respectively, then the new values of a and b after the statement a -= --b * 2 are
  26. [a.] a = 14 and b = 2
    [b.] a = 16 and b = 2
    [c.] a = 14 and b = 4
    [d.] a = 16 and b = 4

  27. Which operator can be used on strings and numbers?

    [a.] *
    [b.] +
    [c.] -
    [d.] all of the above
    [e.] none of the above


Short Answers

In the following questions, no syntax errors have been 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.

  1. Briefly describe one reason to write your own functions.

  2. Write a function called SumTwo that returns the sum of two numbers, where the numbers, num1 and num2 are passed to it.

  3. Name two benefits of using constants in a program.

  4. Write a function called Smallest that takes three arguments, x, y and z and returns the smallest of the three arguments. You can assume x, y and z are numbers.

  5. What is the starting index of an array?

  6. Briefly describe why the function definitions are placed in the <head> section of the document.

  7. In an array declared with n elements, what is the index of the last element ?

  8. Write the statements that would declare a 10-element array and initialize all of its elements to 1.

  9. Write a function called Average that takes three number parameters n1, n2, and n3 and returns the value of the average of the three.

  10. Write a function called FeetToInches that takes numFeet as an argument and returns the number of inches in that many feet.
  11. Write a function called PrintBox that will print a rectangle made up of any character passed to it. Here is the function header:
    function PrintBox (rows, columns, ch)
    
    This call to PrintBox,
           PrintBox(3, 10, "$");

    should print:
      $$$$$$$$$$
      $$$$$$$$$$
      $$$$$$$$$$
    

  12. Write a set of one or more statements that will print out one of the brief messages given in the table below, depending on a person's grade point average. Assume that the grade point average has already been read into a variable called gpa. Include code to print a warning message if the value of gpa is not in the range 0.0 to 4.0. You should decide which control structure (if, if-else, switch, etc...) is appropriate.

    GPA rangeStudent Rating
    3.50 - 4.0 Dean's list
    2.0 - 3.49 Satisfactory
    1.0 - 1.99 Probation
    0.0 - 0.99 Suspended

  13. Given an array of size 100 called numbers, write a code segment that would find the average of the array elements. You can assume that the array has already been initialized to hold values.

  14. Given an array of size 100 called numbers, write a code segment that would find the smallest element in the array. You can assume that the array has already been initialized to hold values.

  15. Given an array of size 100 called numbers, write a code segment that would find the largest element in the array. You can assume that the array has already been initialized to hold values.

  16. What is wrong with the following code segment? Modify the code so it works correctly.
    
         var i = 1;
    
         //this while loop displays the numbers 1-10
         while (i <= 10)
         {
           alert("i is " + i);
           i--;
         }
    
    

  17. Give a description of a problem/program where you would use an array and explain why you would choose to use an array. What would the alternative to an array be?


    Determine the output generated by the following code segments, assuming the surrounding program code (whatever it is) compiles correctly.

  18.     alert ("JavaScript\nis\nfun.");
    


  19.     var  a = 2, b = 3;
    
        b *= a;
        a += b++;
        alert (a + " " + b);
    


  20.     var  a = 5, b = 4;
    
        b += a;
        a -= ++b;
        document.write (a + " " + b);
    
    


  21.     var n = 1234;
    
        while (n > 0)
        {
            document.write(n + "<br />");
            n /= 10;
            n = Math.floor(n);
        }
        /* Hint: The Math.floor() function truncates a number
           downwards to the nearest integer.  For example, 
           the floor of 34.5 would be 34 */
    
    


  22.     var m;
    
        for (m = 1; m <= 5; m++) 
        {
            switch (m) 
            {
                case 1:
                    document.write ("one");
                    break;
                case 2:
                    document.write ("two");
                    break;
                case 3: 
                    document.write ("three");
                    break;
                default:
                    document.write ("Default case");
            }        
    	document.write ("<br />");
        }
    


  23.   <head>
        <script type="text/javascript">
          <!--
            function X (m)
            {
              document.write("In function X: " + m + "<br />");
              return (m + 2);
            }
          //-->
        </script>
      </head>
      <body>
      <script type="text/javascript">
        <!--
          var m;
    
          m = X(5);
          document.write("After the call to function X:  " + m);
        //-->
      </script>