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               ________d_________

 

2.      make a new directory                                             ________g________

 

3.      copy a file or files                                                    ________a_________

 

4.      rename or move a file or directory               ________b_________

 

5.      delete a file or files                                      ________f_________

 

6.      display the contents of a file                         ________e_________

 

7.      change the current directory                                    ________c_________

 

 

 

 

 

 

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.

 

 

_____gcc lab99.c –o lab99___OR  gcc ./lab99.c –o lab99______________________

 

 

 

  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" ) .




 

 

___ls ../../lab10 OR ls ../../lab10/* OR ls ~/lab10 OR ls ~/lab10/* _________

 

 

 

 

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));

 

 

 

 

            _________8______________________________________________________

 

 

 

 

  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");

  }

 

 

_____kitty_______________________________________________

 

 

 

            _____i_______________________________________________

 


_____dog_______________________________________________

 

 

 

            _____no_______________________________________________


 

 

  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 ; ___++i OR i++ OR i=i+1__)

  {
        total = 0;

       for(j=0; j < 5 ;  _++j OR j++ OR j=j+1_____ )

       {

 

           ___total += scores[i][j]___________;

 

       }

       average[__i____] = ______total/5.0_____;

  }

  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 = _____5________ ,  b = ____4_________ ,  c = ____3_________ ,  d = _____2_________

 

 

 

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___2___2___4_____________________________________________

 

  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);