Department of Computer Science

University of Illinois at Urbana-Champaign

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

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: April 5, 2007

 


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.

 

This examination contains 12 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. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.

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. If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.

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


DO NOT WRITE IN THIS SPACE

Section

Possible Score

Score

Grader

1

7

 

 

2

6

 

 

3

6

 

 

4

6

 

 

5

4

 

 

6

8

 

 

7

10

 

 

8

8

 

 

9

6

 

 

10

8

 

 

11

10

 

 

12

4

 

 

13

4

 

 

14

8

 

 

15

4

 

 

16

6

 

 

17

8

 

 

18

12

 

 

Total

125

 

 

A

  1. Fill in the blanks with the corresponding letter to match the following UNIX commands with the descriptions of what they do.

 

Unix commands:

 

a)      cp

 

b)      mv

 

c)      cd

 

d)      ls

 

e)      more

 

f)        rm

 

g)      mkdir

 

 

 

Descriptions:

 

1.      display the contents of a directory               _________________

 

2.      make a new directory                                             _________________

 

3.      copy a file or files                                                    _________________

 

4.      rename or move a file or directory               _________________

 

5.      delete a file or files                                      _________________

 

6.      display the contents of a file                         _________________

 

7.      change the current directory                                    _________________

 

 

 

 

 

 

2.      Write a single UNIX command that compiles a C program stored in a file named lab99.c , found in your current directory, into an executable file named lab99. That is, to run this program you will type lab99 and NOT a.out at the UNIX prompt.

 

 

____________________________________________________________________________

 

 

 

  1. You are currently in a directory named "stuff" which is located inside a directory "lab5" which is located inside your home directory. There is also a directory "lab10" inside your home directory. The diagram below shows your directory tree. Write a single Unix command to display the contents of the directory named "lab10" without changing your current directory("stuff" ) .




 

 

__________________________________________________________________

 

 

 

 

4.      A “block” in C programming language is defined by using which of the following pairs of symbols? Circle only one choice.

 

a)      ( )



b)      { }



c)      [ ]



d)      /* */



e)      ""

 

  1. Write the output the following code would produce if it was included in a complete C program that compiles and executes without errors.

 

char sport[] = "baseball";

printf(" %i \n", strlen(sport));

 

 

 

 

            ________________________________________________________________

 

 

 

 

  1. Write the output of the following program when it runs.

 

#include <stdio.h>

#include <string.h>

 

void main(void)

{

    char password[32] ={'k','i','t','t','y','\0','c','a','t','\0'};

    char animal[32] = "dog";

    printf("%s \n", password);

    printf("%c \n",password[1]);

    strcpy(password, "dog");

    printf("%s \n", animal);

    if (strcmp( password, animal)  > 0)

      printf("yes \n");

    else

      printf("no \n");

  }

 

 

____________________________________________________

 

 

 

            ____________________________________________________

 


____________________________________________________

 

 

 

            ____________________________________________________


 

 

  1. Suppose you have a 2 dimensional integer array named scores that already contains the exam scores for 5 students in 5 subjects. That is, for the first student, her five quiz scores are 10, 9, 9, 10, 8. Complete the following code by filling in the blanks to compute the average score of each student, and store it in the array named average. For example, the average score of the second student should be stored in average[1]. Do NOT use integer division to calculate the average. Your code should work correctly not just for the values assigned to the scores array below but for any set of values.

 

 

#include <stdio.h>

void main(void)

{
  int i, j, total;

  int scores[5][5]={10, 9, 9, 10, 8,

                 10, 5, 6, 6, 2,

                  8, 8, 8, 5, 4,

                 10, 8 , 5, 4, 5,

                  6, 2, 6, 7, 8};

  double average[5];

/* not shown here is code that may change values in scores array*/
/* fill in the blanks below with the correct C code */

  for(i=0; i < 5 ; _____________________)

  {
        total = 0;

       for(j=0; j < 5 ;  ____________________ )

       {

 

           ______________________________________;

 

       }

       average[______] = __________________________________;

  }

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

      printf("average[%i] = %lf \n", i, average[i]);

}

 

           

 

 

 

 

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

 

#include <stdio.h>

void main(void)

{

 int a, b = 3, c = 2, d = 4;

 a= (c++) + b;

 b++;

 d /= 2;

 printf(" a = %i , b = %i , c = %i , d = %i\n", a, b, c, d);

}

 

 

 

 

 a = _____________ ,  b = _____________ ,  c = _____________ ,  d = ______________

 

 

 

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

 

 

#include <stdio.h>

 

void main(void)

{

   int i, j;

 

   for(i = 1; i <= 2; i++)

   {

      for(j = 1; j < 3; j++)

      {

         printf("%i ", i * j);

      }

   }

}

 

 

_________________________________________________________________

 

  1. Circle each of the following loops that precisely produces the output "12345".  There may be more than one correct answer.

 

a)      for(i = 0; i < 6; i++)

{

   printf("%i", i);

}

 

 

 

b)      i = 0;

while (1)

{

   printf("%i",++i);

   if (i==5)

   {

      break;

   }

}

 

 

 

c)      i = 1;

do

{

   if (i != 0)

   {

      printf("%i", i);

   }

} while (i < 6);

 

 

 

d)      for(i = 0; i < 6; i++)

{

   if (i == 0)

   {

      for(j = 1; j < 6; j++)

      {

         printf("%i", j);

      }

   }

}

 

 

 

 

 

 

 

  1. Convert the following piece of C code so that it uses a switch statement instead of nested if. That is, the C code you write should be equivalent to the code below and not use any if statements. You may assume that the variable named choice has been declared as an integer variable.

 

 

if (choice == 1)

   printf("A\n");

else 

   if (choice == 2)

      printf("B\n");

   else

      printf("C\n");

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

void main(void)

{

  int x = -1;

  int y = -2;

  if(((x + y) >=3) || ((x + y) <= -3))    

     if(x > y)

  printf("X");

     else

  printf("Y");             

  else

printf("Z");

}

 

 

___________________________________________________

 

 

 

 

13.  Circle the  (one) correct choice of a data type. You are trying to declare and initialize the variable named pi  with the value 3.1415926535 , with the highest precision possible.

 

________ pi = 3.1415926535;

 

 

a)      long




b)      double




c)      float




d)      char

14.  The following code tries to ask the user to enter their ID number and displays it on the screen, but the program has two errors. Write down the line numbers of the lines that contain errors and write the corrected versions. (Note: the line numbers are not part of the code)

 

1: #include <stdio.h>

2: void main (void)

3: {

4:  int idnum;

5:  printf ("Please enter your student ID number:");

6:  scanf ("%f", &idnum);

7:  printf ("Your ID number is %i \n", &idnum);

8: }

 

 

Line#_________  Corrected Line: __________________________________________________

 

 

Line#_________  Corrected Line: __________________________________________________

 

 

 

 

  1. Given code for the function named plus_ten below, circle each valid prototype for this function(there may be more than one correct answer).
     
       int plus_ten(int x)
       {
         return x + 10;
       }


     a) int plus_ten(int x[ ]);




             b)   int plus_ten(int y);   



             c)   int plus_ten( int);



             d)   void plus_ten( int);

 

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

#include <stdio.h>

 

int fun(int x)

{
   x = 7;
   return x;
}

void main(void)

{
  int x = 5;

  int y = 3;

  y = fun(x);

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

}

 

            x = ______________________ , y = ______________________

 

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

#include <stdio.h>

int didsumpin(int d, double e[])
{
  e[0] = e[0] + 2.0;
  e[1] = e[1] + 3.0;
  return 3*d;
}

        

void main(void)
{
    int  a = 9;
    int c;
    double b[2] = {4.0,3.0};
 
  c = didsumpin(a+1,b);
  printf("a = %i b[0] = %lf b[1] = %lf c = %i\n",a ,b[0] ,b[1] ,c);
}



            a = _____________  b[0] = _____________  b[1] = _____________  c = _____________


  1. Fill in the two blanks and complete the C code for a function named ave. The function ave calculates the average of an array of integers.  The function takes two parameters: first, an array of integers called numbers, second, an integer count, which specifies the number of valid elements in the array numbers.  You may assume that count is always greater than zero and not greater than the actual size of the array.  The function should calculate the average to double precision and return a double. Do NOT include comments.  Do NOT write code for the main function.


 
    Fill in the two blanks and complete the C code for the function named
ave.

__________     ave( int numbers[], _________ count)

{

 double total = 0.0;

 int i;

 /* your code goes here */

 

 

 

 

 

 

 

 

 

 

}