University of Delaware

CISC105 - General Computer Science

Quiz #1
(solutions)

Making Decisions, Arrays and Functions

 
 
 
 1. (6) Write a simple 'if' statement to keep a count of all
        grades that are less than 60.

    if( grade < 60 )
      ++f_grades;

 
 
 2. (6) Using the following code write an 'if-else' statement to
        display a message stating whether 'number' is even or odd.

	   remainder = number % 2;

    if( remainder == 0 )
      printf( "number is even\n" );
    else
      printf( "number is odd\n" );
 
 

 3. (6) Write a compound 'if' statement to keep a count of all
        grades that are 80 through 89 inclusive.

    if( grade >= 80 && grade <= 89 )
      ++count;

 

 4. (6) Rewrite the compound 'if' in problem 3 above using a
        nested ‘if'.

    if( grade >= 80 )
      if( grade <= 89 )
        ++count;

 

 5. (6) Write an 'else-if' statement to keep a count of grades in
        the following ranges: 90 and above, below 60, 60 through 74,
        75 through 89.

    if( grade >= 90 )
      ++a_grade;
    else if( grade >= 75 )
      ++b_grade;
    else if( grade >= 60 )
      ++c_grade;
    else               
      ++d_grade;



 6. (6) What is the value of result in the following expression?
    
	   a = -10;
	   (int) result = ( a < 0) ? ( a * -1) : ( a * a );
	
    result = 10
 
 

 7. (6) Circle the 'printf' statement that will be executed in
        each of the following 'if' statements.

           remainder = 1;                     
           if ( remainder )
    ==>     printf ("The number is odd\n");   
           else
            printf ("The number is even\n"); 

           remainder = 0;                     
           if ( ! remainder )                   
    ==>     printf ("The number is even\n");   
           else
            printf ("The number is odd\n"); 

 

 8. (6) Given the following code write a statement that sets the
        50th element in the grades array to 95. 

	   int grades[100];

    grades[49] = 95;

 

 9. (6) Given the following code write two lines of code that
        will initialize all the elements in the grades array to zero.

	   int grades[100];

    for( i = 0; i < 100; ++i )
       grades[i] = 0;

 
 
10. (6) Write the code to initialize a character array containing
        all digits needed ( 0 thru F ) to convert a number to Hexadecimal.


    static char a[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};



11. (6) Write the code to initialize all the elements in the
        following array to zeros.

           int matrix[3][3];

    for( a = 0; a < 3; ++a )
       for( b = 0; b < 3; ++b )
          matrix[a][b] = 0;



12. (6) Rewrite the following code using an assignment operator.

           array[index] = array[index] * 2;

    array[index] *= 2;



(28) The remaining questions require a TRUE(T) or FALSE(F) answer.


13. _T__  The variables declared in the parentheses of a function
          declaration statement are known as arguments or     
          formal parameters.

14. _T__  The variables declared inside the body of a function are
          known as automatic local variables.

15. _T__  Local variables can only be accessed by the function in
          which they are defined.

16. _T__  The result that is returned by a function does not have
          to be assigned to a variable.

17. _T__  If the declaration of the type returned by a function
          is omitted, then the C compiler assumes that the
          function will return an integer.

18. _T__  When a variable is passed to a function as an argument,
          its value is automatically copied into the formal
          parameter so that any changes made to the formal
          parameter in the function affect only the formal
          parameter and not the variable that was passed to the
          function.
 
19. _T__  The result returned by a function can be used in an
          arithmetic expression.

20. _T__  The type of argument that is passed to a function must
          agree with the type of the argument as declared inside
          the function.

21. _T__  Many different functions may contain formal parameters
          and local variables with the same name.

22. _T__  It is possible to pass the value of an array element
          and even an entire array as an argument to a function.

23. _T__  If a function changes the value of an array element,then
          that change will be made to the original array
          that was passed to the function.

24. _T__  For a two-dimentional array, the number of rows may be
          omitted, but the declaration must contain the number of
          columns of the array.

25. _T__  Any function in a program can access the value of a
          global variable and can also change its value.

26. _T__  Static variables are initialized once only at the start
          of the program.