Section 0101, 0102, 0103 & Honors, Fall 1995

Quiz 3


True or False Questions, 1 point each

To practice taking this section. Select TRUE or FALSE in the pop-up menus after each question to record your answer. To submit your answers for grading, click on the "submit for grading" button.

On lynx, use down arrow to move to the next question or choice. Hit return to make your selection.

  1. If p is a pointer variable, then the following statement will always add 4 to the address stored in p
    	     p = p + 1 ;  
    	

    Your answer:

  2. To declare an array called A with 10 integer elements, we use the declaration:
    	int A[11] ;
    	

    Your answer:

  3. The type string defined in the course library is equivalent to an array of characters.

    Your answer:

  4. When an array is passed as a parameter, a separate copy of the array is given to the function.

    Your answer:

  5. The expression *&**&p is equivalent to the expression *p.

    Your answer:

To submit this section for grading click here:


Multiple Choice, 2 points each

To practice taking this section. Choose the best answer in the pop-up menu after each question to record your answer. To submit your answers for grading, click on the "submit for grading" button.

On lynx, use down arrow to move to the next question or choice. Hit return to make your selection.

  1. The main disadvantage of using IthChar from the course library is:


    a. The IthChar function does not check out-of-bounds errors.
    b. The IthChar function is slower than using array notation.
    c. The IthChar function is not compatible with standard C.
    d. All of the above.

    Your answer:

  2. Suppose that sizeof(int) is 4, then the function call malloc(24) accomplishes which of the following, assuming that enough system memory is available?


    a. reserves enough space for an array of 6 integers
    b. reserves enough space for an array of 5 integers
    c. reserves enough space for an array of 24 integers
    d. reserves enough space for an array of 23 integers

    Your answer:

  3. Suppose that p is an integer pointer and A is an "honest to goodness" array of 10 integers, then
    	   p = A ;
    	

    a. will cause a syntax error when compiled.
    b. checks whether p and A have the same value.
    c. stores the address of A in p.
    d. allocates memory so that p can be used as an array.

    Your answer:

  4. If ptr1 is a pointer to a double and ptr2 is a pointer to an integer, then the statement
    	   ptr2 = ptr1 ;
    	

    a. will cause a syntax error when compiled.
    b. will truncate the value in ptr1 and store it in ptr2.
    c. will cause a segmentation fault when the statement is executed.
    d. none of the above

    Your answer:

  5. Let str be a string variable, then after the assignment
    	   str = "Gilbert and Sullivan" ;
       

    a. the expression str[5] refers to the string "ert and Sullivan".
    b. the expression str[5] refers to the string "rt and Sullivan".
    c. the expression str[5] refers to the character 'e'.
    d. the expression str[5] refers to the character 'r'.

    Your answer:

To submit this section for grading click here:


Short Answers, 4 points each

This section cannot be graded on-line. To practice taking the exam, jot down your answer and follow the link after each question to see a sample solution.

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.

In each of the following questions, trace through the program and write down the output of the program. It is important that you write down the output in the order that they would appear when the program is executed.

  1. Parameter passing by reference.
    #include <stdio.h>
    int dragoon (int, int *) ;
    
    int dragoon (int a, int *p) {
       int b ;
    
       b = *p + a ;
       *p = b / a ;
       printf("dragoon: a = %d, b = %d, *p = %d\n", a, b, *p) ;
       return(b) ;
    }
    
    main() {
       int a = 13, b = 2, c = 9 ;
    
       a = dragoon(b, &c) ;
       printf("main: a = %d, b = %d, c = %d\n", a, b, c) ;
    }
    
    

    Sample solution

  2. Using an array parameter.
    #include <stdio.h>
    int yeoman(int []) ;
    
    int yeoman(int A[]) {
    
       A[1] = 9 ;
       A[2] = 18 ;
       return(-1) ;
    }
    
    main() {
      int X[4] ;
    
      X[3] = yeoman(X) ;
    
      printf("main: %d, %d, %d\n", X[1], X[2], X[3]) ;
    }
    
    

    Sample solution

  3. Pointer manipulations.
    #include <stdio.h>
    
    main() {
       int a = 1, b = 2, c = 3 ;
       int *ptr1, *ptr2 ;
    
       ptr1 = &a ;
       ptr2 = &b ;
       a = *ptr1 + *ptr2 ;
       printf("a = %d, b = %d, c = %d\n", a, b, c) ;
    
       *ptr2 = *ptr1 + c ;
       printf("a = %d, b = %d, c = %d\n", a, b, c) ;
    
       ptr2 = ptr1 ;
       printf("a = %d, b = %d, c = %d\n", a, b, c) ;
    
       *ptr2 = *ptr1 + c ; 
       printf("a = %d, b = %d, c = %d\n", a, b, c) ;
    }
    

    Sample solution


Last Modified: Dec 7, 1995

Richard Chang, chang@gl.umbc.edu