Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam II (60 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: November 2, 2006

 


No questions will be answered during this examination.  If you do not understand a question, read it again.  If you still do not understand it, make reasonable assumptions and write them down. (Points will be deducted if unreasonable assumptions are made.)
DO NOT CHEAT: Cheating includes not only copying from another person but also allowing someone to copy from you.  Anyone who copies or allows someone to copy will receive a score of zero.  So be defensive and protect your work.

      You may not use any electronic devices, book, notes or other references during this examination.

 

This examination contains 13 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. The number of points for a question is roughly proportional to the amount of time you may need for it. Don’t spend too much time on any one question.

Do not forget to sign the attendance list and to write your signature on the line below:

 

_______________________________________________________________________
(If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.)

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Missed Points

Grader

1

8

 

 

2

4

 

 

3

4

 

 

4

8

 

 

5

6

 

 

6

6

 

 

7

10

 

 

8

9

 

 

9

10

 

 

10

10

 

 

11

6

 

 

12

4

 

 

13

4

 

 

14

8

 

 

15

9

 

 

16

8

 

 

17

11

 

 

Total

125

 

 

A

 

 

 

 

1.      Assume that you are in your home directory. You have the following subdirectories:

 

subdirectory:   files in it

          -------------   ------------

          source          intro.c    test.c    test.m

     mp1             find_h.m   main.m

mp2             1cs101.c   Xcs121.c  cs.c   input.dat


a
) Write a single UNIX command to create another subdirectory with the name “backup”.

 

 

________________________________________________________________________

 

b) Write a single UNIX command to make “backup” your current directory.

 

 

_______________________________________________________________________

 

c) Assume that you are in your “backup” directory. Write a single UNIX command to copy all the files in “source” subdirectory to “backup” directory.   (Hint:  You may use the wildcard "*").

 

 

______________________________________________________________________


d) Assume that you are in your home directory. Write a single UNIX command to move all     files in the mp2 directory that have an extension (suffix) of  .c into the source directory.

  

  _______________________________________________________________________

  

 

2.      Choose the single correct UNIX command to rename a file name from “file1” to “file2”:

a)       cp file1 file2
b)       cp file2 file1
c)       rn file1 file2
d)       rn file2 file1
e)       mv file1 file2
f)         mv file2 file1

Correct answer: _________________________
 
 
 
 
 
3.      Circle the correct commands you would type at the UNIX prompt to compile and run the C program located in the file lab9.c . The program uses the math library. Circle only one choice.

a)         gcc lab9.c –lm
./a.out



b)        gcc lab9.c –include <math.h>
a.out



c)         gcc lab9.c –o math 
.\math



d)        gcc a.out -lm
lab9
 
 
 
 
 
 

 

4.      Fill in the blanks to correctly print the values of the variables w, x, y and the array z (not necessarily in this order).
 

   #include <stdio.h>

 

        void main(void)

   {
       int w = 4;

           float x = 2.345;

           char y = ‘a’;

           char z[] = “cat”;


      printf(“%c \n”, ________________);            



      printf(“%d \n”, ________________);            



      printf(“%s \n”, ________________);


 printf(“%f \n”, ________________);

    }

5.      Write the output the following programs produce.

     a)      
         
#include <stdio.h>


               void main(void){
                 int  num = 3, i = 0 , count = 0;

   while ( i < num )

 {

count++;

++i;

 }
  printf(“ %i ”, count);
}

_____________________________________________

 

    b)
          #include <stdio.h>


              void main(void){
                 int num = 2, i = 0, count = 0;

  for ( i = 0; i < num; ++i )

  {

count++;

  }
  printf(“ %i ”, count);
}

 

_____________________________________________


        c)    
              
#include <stdio.h>

           void main(void){
                 int num = 0, i = 0, count = 0;

 

   do

 {

 count++;

 ++i;

  } while (i < num);

 

  printf(“ %i ”, count);
}

                     _____________________________________________________

 

 

6.      The following C program compiles and runs without errors.

 

#include <stdio.h>

 

int add3(int a) {

     a = a + 3;

     printf(“ %i ”, a);

     return a;

}

 

void main(void) {

     int a = 3;

     int b = add3(a);

     printf(“ %i ”, a);

     printf(“ %i ”, b);

}

 

Fill in the blanks with the correct output of the program:

 

 ____________________   ____________________   ____________________

 

 

 

 

7.      Which of the following are valid declarations to make a string that can be used in printf(“%s”, a) to always print the string “cs101” (without the quotes)? Circle each correct answer. There may be more than one correct answer.

 

 

 

a)  char a[] = “cs101”;

 

 

 

b) char a[5] = “cs101”;

 

 

 

c)  char a[6] = “cs101”;

 

 

 

d)  char a[] = {‘c’, ‘s’, ‘1’, ‘0’, ‘1’};

 

 

 

e)  char a[] = {‘c’, ‘s’, ‘1’, ‘0’, ‘1’, ‘\0’};

 

 

 

8.      The following C program compiles and runs without errors. Write the output the following program produces when run.

 
   #include <stdio.h>
   void main(void)
   {
     float x, y;
     int z;
     x = 16/5;
     y = 16.0/5.0;
     z = 16/5;           
     printf("x=%f  y=%f  z=%i \n", x,y,z);
   }


     ______________________________________________________________

 

 

 

 

 

 

9.      The following program will output the maximum value of a 3 by 4 matrix. Please complete the program by filling in the blanks. The output should be:

 

    maximal value is 34

    Your code should work correctly for any integer values assigned to the matrix named mat and

     not just for the values listed below.

 

#include <stdio.h>

void main(void)

{ 

  int mat[3][4]={{1,3,5,7},{2,4,6,8},{15,17,34,12}};

  int row,col,max = __________________;

  for(row = 0 ; row < 3 ; row++)

    for (col = 0 ; col < 4 ; col++)

 

         if(____________________ > _____________________) 

 

 

                 max =  ___________________________;



  printf("maximal value is %i \n", ______________________);

 

}

 

10.  Suppose y is a function of x, which is defined as:

 

 

The definition above means, when x is less than 0 then y equals -1 and when x equals 0 then y equals 0 and if x is greater than 0 then y equals 1. Which of the following C statements correctly computes y for all possible values of x ? Circle each valid statement. There may be more than one valid statement. You may assume that both x and y are declared as integer variables.

 

 

a)         if(x >= 0)

         if(x > 0) 
            y = 1;

else      
   y = 0;

      else

         y = -1;

 

 

 

b)         if(x < 0)
        y = -1;

     else if(x == 0)
            y = 0;

          else

            y = 1;

 

 

 

c)         y = -1;

      if(x != 0)

         if (x > 0)
            y = 1;

         else

            y = 0;

 

 

d)         y = -1;

     if(x >= 0)

        if(x > 0)

          y = 1;

        else

          y = 0;

 

 

 

 

11.  Given the following code:

 

#include <stdio.h>


void main(void)

{            

  int n;

 
  n = 22;

  while(n > 0)

  {

    if (n == 10)
      break;

    printf(" %i ",n);
    n -= 4;

  }

 

    }

 

Fill in the blank with the correct output of the program:




______________________________________________________

 

          

 

 

 

12.  For the following problem circle the correct answer. Circle only one choice.


 To avoid Compile-Time errors, before a variable named x is used it must be:


a) Both declared and initialized.



b) Declared, but not necessarily initialized.



c) Initialized, but not necessarily declared.



d) Either initialized, or declared, but not necessarily both.



e) Neither initialized nor declared is necessary.

                                              

13.  Consider the following C code:

 

#include <stdio.h>

 
void main(void)

{

  int m,n;

         
  m=1;

  n=0;

  switch(m)

   {

    case 1: m=m+1;

    case 2: n=n+2;

    case 3: m=m-1;

    case 4: n=n-2;
            break;

    default: m=m-2;

   }

         

  printf("m = %i, n = %i\n", m, n);          

}

 

Circle the answer that gives the correct output of the program.

 

a)  m = 0, n = 0




b)  m = 1, n = 0




c)  m = 2, n = 0



d)  m = -1, n = 0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.  The following C program compiles and runs without errors. Write the output the program produces.

 

 #include <stdio.h>

 #include <string.h>

 

 void main(void)

 {

   char password[32] = "CS101";


   printf("%s \n", password);
   printf("%c \n",password[1]);
   strcpy(password, "CS105");
   printf("%s \n", password);
   if (strlen("CS101")  > 5)
     printf("yes \n");
   else
     printf("no \n");

 }

 

 

__________________________________________________________________

 

 

 

__________________________________________________________________

 

 

 

__________________________________________________________________

 

 

 

__________________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

15.  The following C program compiles and runs without errors. Write the output the program produces.

 

 #include <stdio.h>

 

 void main(void)

 {

 

   int i = 0;
   int a,b,d;

 

   a = (i++) * 10 ; 

   printf(“i = %i \n”, i);

   printf(“a = %i \n”, a);

   b = 0;

   if (b = 0)
     d = 1;
   else
     d = 0;
                          

   printf(“d = %i \n”, d);

      }

 

 

__________________________________________________________________

 

 

 

__________________________________________________________________

 

 

 

__________________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

16.  The following C program compiles and runs without errors. Write the output of this program.

 

#include <stdio.h>

    double dosumpin(int a, double b[]){

      b[0] = b[0] + a;

      b[1] = b[1] + a;

 a = 7;
 return b[0];

    }

 

 

    void main(void)

    {

      int  x = 9;

      double y[] ={10.0,11.0}; 

double z = 12.0;

     

z = dosumpin(x,y);

printf("x = %i z = %lf \n", x, z);

printf("y[0] = %lf  y[1] = %lf \n", y[0], y[1]);

}

 

 

 

 

___________________________________________________________

 

 

     _______________________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

17.  Write a complete function named factorial and fill in the blank with a valid prototype for factorial . The factorial function has one input parameter, an integer n  and the function returns an integer.

The mathematical formula for factorial is:

factorial(0) = 1,

factorial(1) = 1,

factorial(n) = n*(n-1)*(n-2)*...*2*1   for n > 1

that is,  factorial(2) =  2*1,  factorial(3) = 3*2*1,  factorial(4) = 4*3*2*1 and so on…

For example, your function could be used in the following way.

#include <stdio.h>
     /* write a prototype for the factorial function here */

______________________________________________________

void main(void)

{

  int n = 5;

  /*      print factorial(5) = 120 */

  printf(“factorial(%i) = %i \n”, n, factorial(n)); 

   }
 
      Of course your function should work correctly not just for n = 5 but for any non-negative    
       integer.
 
      Write the complete code for the factorial function below. Do not include comments.