Exam 2 Sample Test Questions


For all of the questions below, you must not only give an answer but you must show all of your work and describe all of your reasons for choosing your answer.

  1. Question: Scope: What is the output of this

      <script language=javascript type="text/javascript">  
        var x = 5;
        document.write("<p>x before function call = " + x);
        test();
        document.write("<p>x after function call = " + x);    
       
        function test()
        {
            var x = 1000;
            document.write("<p>x in function = " + x)
        }    
    </script> 
  2. Question: Write a function where the name of the function is foo which has one parameter called parm. The body of foo uses document.write to print out the value of parm. Don't just print the value out - also include text saying what is being printed out.

  3. Question: What is wrong with the following code? How would you fix it?

    <script language=javascript type="text/javascript">  
        toDays(21);
        function toDays(years)
        {
            var time;
            time = 365*years;
            return time;
        }       
        document.write("My age is " + time); 
    </script> 
  4. Question: Explain how an assignment works in Javascript. Which of the following statements are legal?

    1. x = x + 1;
    2. y = 5 + 6;
    3. 23 = 3 + x;
    4. x + 4 = 45;

  5. Question: Explain how the mod function (%) works. For example, what does the statement x = y % 5 compute?

  6. Question: Given the following code:

        <script language=javascript type="text/javascript">  
             for (i=0;i < 10; i++) {
                document.write(i);
             }  
        </script> 
    1. What is the output?

    2. How would you modify the code so that it prints the numbers backwards?

    3. How would you modify the code so that it prints only the even numbers?

    4. How would you modify the code so that it sums all the values of i and prints the final sum?

  7. Question: Arrays: Assume that you are creating a new array called birds
    1. How do you declare a new empty array?

    2. How do you declare a new array with space allotted for 4 elements?

    3. How do you declare a new array containing the names of 4 birds (note, some types of birds are robin, sparrow, hummingbird, eagle, crow)

    4. Assuming the array has already been created, how do you set the element at the initial position of the array to the bird name "robin"?

    5. Write a loop that prints out all of the elements in birds.

  8. Question: Suppose you have an array called months that contains the names of the months, "January" through "December". Let monthIndex be a variable that contains the number of a month (0 corresponds to January, 1 corresponds to February, etc). How do you print out using document.write the actual name of month corresponding to the monthIndex?

  9. Question:Suppose a company stores data in 5 year periods. The month indices are numbered starting at 0 and going up to 5*12-1. Thus, a value of 0 and 12 both correspond to January. How do you use the array of month names from the previous problem to write out the name of the month for a given monthIndex?

  10. Question: A particular if-else statement has the form:
    <script language=javascript type="text/javascript">  
        if (CONDITION)     {
            document.write("The result is true");    
        }   
        else  {
            document.write("The result is false");    
        }    
    </script> 
    What code should replace CONDITION for the following situations. You may need to define a few variables. The first one is done for you.
    1. The variable i is larger than 10.

      Answer: Assume we have a variable i that contains some number. Then replace CONDITION with

      i > 10
    2. The variable x is equal to 10

    3. The variable x is larger than 10 and smaller than 20

    4. The card is an ace and the score is over 21.

    5. x is either zero or y is larger than 5.

    6. It is not true that either x is zero or y is larger than 5.