CMPS 350 HW 7 - Data Types and Expressions

You must 'check' your answer on each question or your score will not be recorded.

resources:
C reference
hw07.c
C/C++ precedence chart

01. Which of the following are considerations in the design of arithmetic 
    expressions?
    A. operator associativity rules
    B. operation heirarchy rules
    C. whether to support user-defined operator overloading
    D. whether array indices can be floating point numbers 
A B C D
02. The semantics of an arithmetic expression is determined by which of these?
    A. whether the language includes pointers or references or both
    B. operator precedence and associativity
    C. the size of different integer data types in the language  
A B C
03. Which of these operations are right associative? Note that associativity
    always involves operators of the same precedence.

    A. a = b = c; (assignment)   
    B. a-- ++ (unary post increment and decrement) 
    C. both A & B
A B C
04. Under which condition might this statement cause in an unwanted side effect?

         C = A + fun(A) + A; 

A. if A is a global variable and its value is modified in function fun()
B. if A is a 2-way parameter its value is modified in function fun()
C. both A & B 
A B C
05. Which of the following statements is true?
    A. a widening conversion is always safe
    B. a narrowing conversion is sometimes safe 
    C. both A & B
A B C
06. Unwanted functional side effects can be prevented by what?
    A. eliminating two-way parameters and access to non-local variables
    B. precisely defining the evaluation order of operands in expressions 
    C. both A and B are correct  
A B C
07. What is true regarding this loop if C DOES NOT short-circuit on '&&'?

      int list[MAX]; // list indices are 0..MAX-1
      index = 0;      
      while ((index < MAX) && (LIST[index] != value))   {
          index++;
      }
   A. LIST[index] will be evaluated past the bounds of the array.
   B. The loop will always successfully terminate without a problem. 
A B
08. What will happen to the code in #7 if we short-circuit on &&? (C does this)
    A. When the loop exits, the index will be one past the range of the array.
    B. The loop will exit without error.
    C. both A and B are true.   
A B C
09. The relational operators (<,>,<=,>=,==,!=) in C are left associative. 
    C returns 1 for T and 0 for F. What is the result of this relational 
    expression in C?

        int a = 5, b = 6, c = 6; 
        a <= b == c;

   A. 0       B. 1  
A B
10. The code below does not produce a runtime error in C. By this you know that
    C supports short-circuit evaluation of Boolean expressions.

          while ((20 < 15) && (5/0))
             ;
T F
11. The following chunk of C code is an example of the ternary conditional 
    operator used in an assignment statement. What is the result?

       int flag = 1;
       count1 = 0; count2 = 0;
       count1 = (flag) ? 100 : 200;

    A.  count1 = 1     B. count1 = 100     C. count1 = 200   
A B C
12. What is this code an example of?

       float x = 5;
       int i = x;
       x = i;

    A. mixed mode assignment 
    B. an implicit coercion of int to float 
    C. a narrowing conversion
    D. a widening conversion
A B C D
13. The function prototype of pow() is
         double pow(double,double);    

    What is happening in this statement, where offset, i and j are all ints
    and i_array is a pointer to int?
           *(i_array + offset) = pow(2,i) + j;
    A. all coercions are widening 
    B. all coercions are narrowing 
    C. widening and narrowing conversions are both occurring     
A B C
14. The following code compiles, and unary minus has precedence over unary 
    postincrement. Under Gnu gcc the result is -4 and under DEC C the result 
    is 6. Under ANSI C which compiler is correct?

        count = 5;
        count = -count++;  
  
    A. DEC C 
    B. Gnu gcc
    C. both compilers are correct 
A B C
15. Since this code compiles and runs under gnu C what can you conclude?
        m = 2;        // m is an int
        n = 32.345;   // this is a double
        m = n;
        n = m;
    A. C supports both widening and narrowing coercions in assignments.  
    B. After execution the value of m and n are both 32.  
    C. both A & B
A B C
16. The following C code compiles and runs. What is true concerning Line 3?
        short s_short;            // a 2 byte signed integer
        int i_int = __INT_MAX__;  // largest 4 byte signed integer 
        s_short = i_int;          // <== line 3
   A. this is a narrowing coercion 
   B. s_short is assigned the value of -1 
   C. both statements are true 
A B C
17. Given this code, what is true after returning from swap?

     int n1 = 10, n2 = 20;
     int *ptr1 = &n1;
     int *ptr2 = &n2;
     swap(&ptr1, &ptr2);
     void swap(int **ptr1, int **ptr2){
         int *tmp_ptr = *ptr1;
         *ptr1 = *ptr2;
         *ptr2 = tmp_ptr; 
     }
   A. *ptr1 is 20 and *ptr2 is 10
   B. the value in ptr1 is an address to an integer
   C. both statements are true   
A B C
18. Given these values for X and Y 
       X: 0000000100
       Y: 0000000101
    What is  X & Y in decimal where '&' is bitwise AND? 
    A. 4     B. 5     C. 0     D. 1   
A B C D
 
19. Given these values for X and Y 
       X: 00000100
       Y: 00000111
    What is  X ^ Y in binary?   (^ is xor)

    A. 100    B. 000   C. 011
A B C
20. How can you flip the last 4 least significant bits of an integer X?
    A. X & 15
    B. X ^ 15
    C. X | 16  
A B C
21. How can you test if two integers X and Y are equal?
    A. if (X & Y) is false 
    B. if (X ^ Y) is false   
A B
22. An example of the conditional target assignment statement is

       (7 > 4) ? i : j = 5;    // this compiles under gnu c++

    What is the meaning of the statement? 
    A. If 7 is greater than 4 then return i otherwise assign 5 to j 
    B. If 7 is greater than 4 assign 5 to i otherwise assign 5 to j  
A B
23. If (7 & 3) results in 3, and if the operation occurs from the most 
    significant bit to the least significant bit, what do you know about the 
    bitwise & operator? 
    A. bitwise & is a short-circuit operator 
    B. bitwise & is not a short-circuit operator  
A B
24. Assume you execute this C code, where C uses two's complement for integers.
      int num = MAX_INT;   // the largest positive integer on the system
      int num2 = num + 1; 

    The bit-by-bit contents of num and num2 are now this:
          01111111111111111111111111111111    // num
          10000000000000000000000000000000    // num2

    According to two's complement representation, what is the value of num2?
    A. the largest negative integer on the system 
    B. -0 
    C. -1  
A B C
25. What is true after executing this JavaScript code?

         var junk = 55;
         junk  = "" + junk; 

    A. junk now contains ""55
    B. junk now contains a string of two numeric characters    
A B
26. The onLoad event in an HTML document (as shown below) is triggered when?
 
          <BODY onLoad="window.document.bgColor='#FF33FF'">

    A. each time the browser client loads/reloads the document in the window
    B. before any JavaScript code in the document is executed
    C. Both A and B are true  
A B C
27. Select the true statement concerning the execution of this JavaScript code: 
    
        var circle = new Image(50,50);
        circle.src = "circle.gif";

    This JavaScript code will
    A. display the image circle.gif in the document window at location (50,50).
    B. create an Image object named circle and link it to an image file.
    C. preload the image circle.gif into the browser's cache.  
    D. both B & C 
A B C D
28. How do you make this function an event handler for an onClick event?
  
       function stuff(in)
       {
          if (in == 0)
              return in;
          else return 
              9/in;
       }
    A. Put the function definition inside the quotes of onClick="...."
    B. Put the function definition into the HEAD and call stuff inside onClick="...."    
A B
29. In the code below an HTML radio button lets the user select one choice out 
    of many. The HTML and a JavaScript onClick handler to access the button 
    are shown below: 

    <FORM name="myForm">
    cat A<input type="radio" name="pet" value="cat"> 
    dog B<input type="radio" name="pet" value="chimpanzee"> 
    bat C<input type="radio" name="pet" value="fish"> 
 
    <input type="button" value="checkIT" onClick="
          len = document.myForm.pet.length;
          var myChoice = 0;
          for (i = 0; i < len; i++){
             if (myForm.pet[i].checked)
                 myChoice = i;
          }
          alert('you have a ' + myForm.pet[myChoice].value + ' for a pet.');">

   The values from the radio form element 'pet' are stored where?
   A. In an associative array named 'pet.'
   B. In an array indexed by the location of the radio button in the form.
A B
30. In the JavaScript code above what happens if the user selects fish?
    A. myForm.pet[2].checked is assigned the value fish
    B. myForm.pet[2].value is assigned the value 1 
    C. myForm.pet[2].checked is assigned true and all other checked
       variables are assigned false
A B C
31. What is true about this C code?
         while (r = read(0,buf,len), r >= 5) {
                { body of loop }
         }
A. it is an example of the C comma sequencing operator
B. the condition r >= 5 will be evaluated regardless of the value of r.
C. both A & B
A B C

  Right: Wrong: Percent: (Must be 100% for credit)

Errors:

Name (first name only): Sleipnir Username: 3-digit ID: