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, 2005

 


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

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

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Score

Grader

1

8

 

 

2

4

 

 

3

10

 

 

4

6

 

 

5

8

 

 

6

4

 

 

7

8

 

 

8

6

 

 

9

7

 

 

10

8

 

 

11

6

 

 

12

6

 

 

13

12

 

 

14

10

 

 

15

4

 

 

16

10

 

 

17

8

 

 

Total

125

 

 

A

 

 

 

 

 

1.            You are in your Unix home directory. You have the following subdirectories:

 

  subdirectory:  files in it

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

    lab7          labsoln.c  

    lab8          guess.c
    lab9          bandit.c       
             

 

 


a)
Assuming your pwd (current directory) is your home directory, write a single Unix command    

    to list all the contents of the lab7  directory.


    
ls lab7    OR   ls –a lab7   OR  ls ~/lab7 (other variations possible)

       

     b) Assuming your pwd (current directory) is your home directory, write a single Unix

                 command to make the lab8 directory your current directory.

                  cd lab8      OR     cd   ~/lab8  
(other variations possible)

      

            c) Assuming that
lab8 is your current directory, write a single Unix command to copy the file

    bandit.c (located in the lab9 directory) into the lab8 directory. Do not change the

    name of the file.

 

       cp ~/lab9/bandit.c .   OR cp ../lab9/bandit .   (other variations possible)

     

       

 

     d) Assuming that lab8 is your current directory, write a single Unix command to move the

                file guess.c (located in the lab8 directory) into the lab9 directory. Do not change the

                name of the file.

 

 

      mv  guess.c ~/lab9  OR mv ~/lab8/guess.c ../lab9   (other variations possible)

 

 

   

 

 

2.            Using the Unix  ls command and Unix redirection( > ), write a single Unix command to create a new text file named screen.txt (to be located in the current directory) that contains the names of all the other files in the current directory.

 

 

ls > screen.txt

 

 

 

 

3.            Using nested if statements (or if-else statements), write C code that will produce the same output as the following switch statement.  Your code may not include a switch statement. Do not write a complete C program. You may assume that x will only be assigned the three values: -1, 0, and 1.

 

switch(x){

     case -1:

           printf(“Negative\n”);

           break;

     case 0:

           printf(“Zero\n”);

           break;

     case 1:

           printf(“Positive\n”);

           break;

}

 

 

if ( x == -1)                

  printf(“Negative\n”);          if ( x == -1)

else                                printf(“Negative\n”);        

   if ( x == 0)

      printf(“Zero\n”);           if ( x == 0)

   else                             printf(“Zero\n”);

     if ( x == 1) (may omit if) 

       printf(“Positive\n”);      if ( x == 1) 

                                printf(“Positive\n”);  

 

 

4.            Write the value of i and j after the following loop is executed.

 

int i = 36, j = 3;

while(i !=  0){

 

  i -= 4;

  j += 2;

}

 

i = _____________0___________________



j = ____________21______________________

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

void main(void){

     int no = 1, v = 0;

     int a = 4;

 

     switch ( no ) {

        case 1: v = v - 5;

        case 2: v = v - 3;

                   break;

        case 3: v = v + 1;

        default:

           printf("%i " , no); 

     }

     printf("%i \n", v);