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 6, 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 11 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 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

6

 

 

2

6

 

 

3

5

 

 

4

6

 

 

5

5

 

 

6

10

 

 

7

9

 

 

8

9

 

 

9

6

 

 

10

8

 

 

11

8

 

 

12

9

 

 

13

6

 

 

14

8

 

 

15

12

 

 

16

6

 

 

17

6

 

 

Total

125

 

 

A

 

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

 

  subdirectory:  files in it

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

    dogs          lassie   snooppy   scooby    buck

    cats          garfield   simba   tom
    other
           bigbird   jerry
      big           <empty>

 

Write a SINGLE Unix command to perform each of the following actions: (assume your pwd (current directory) is your home directory)

        a)  Copy the file lassie in the dogs directory to a file named
                    
favoritedog in the home directory.
        

    _____________________________________________________

        b)  Move the file called bigbird from the other directory to the big
             directory.


    _____________________________________________________



        c) Rename the cats directory cartoon_cats .


    _____________________________________________________

 

 

 

2.      You wite a program in C and save your code in a file named test.c and compile
       by typing:

gcc test.c

Your program compiles without errors. You want to use Unix redirection to read data from the file input.dat when you run your program. Which of the commands correctly uses Unix redirection while executing your program ? (Circle all correct answers)



        a)        test.c | input.dat

        b)        a.out | input.dat

        c)        a.out < input.dat

        d)        a.out > input.dat

        e)        test.c < input.dat

        f)        test.c > input.dat

 

3.  Write a single Unix command that will display details on how to use
       the
more command.

 

 

_____________________________________________________

 

 

 

4.  The following program is compiled and executed without error.

Write the output of the program:

 

#include <stdio.h>

 

void main(void)

{

    int a = 3, b = 2;

    int c, d;

    float e = 2.0;

    float f;

 

    c = (a+b)/2;

    d = (a-b)/e;

    f = (a+b)/e;

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

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

    printf("f = %f \n", f);

}

 

_________________________________________

 

_________________________________________

 

_________________________________________

 

 

5.  The following program generates and prints a single random, integer

      between 10 and 20 (inclusive).  

      Fill in the blank to complete the program.

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

void main(void)

{

    int r;

    srand(time(NULL));

    /* generate the random number here */

    /* Hint: Use the rand() function. */

 

    r = ________________________________________________;

 

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

}

6.      Every room in Siebel Center is assigned a 4-digit code number, where the first digit refers to the floor number, the second digit refers to the area number and the final 2 digits refer to the room number. For example, 3120 refers to floor number 3, area number 1 and room number 20. Write a program, by filling in the blanks below, that asks the user for a 4-digit code and prints the appropriate floor, area and room numbers. Here is a sample run of the program (note: user input is in bold font):

 

  Enter 4-digit code: 2302

  Floor number: 2

  Area number: 3

  Room number: 2

 

Floor and area numbers range from 1 to 4, while room numbers are from 1 to 30. You may assume that the user always enters a valid 4-digit code. Fill in the following blanks to complete the program:

 

#include <stdio.h>

 

void main(void)

{

  /* Variables to store the code, floor, area and room

     numbers */

    int code;

    int floor, area, room;

 

  /* Ask the user for the code and then read the code */



    printf( ___________________________________________ );



    scanf( ____________________________________________ );

 

  /* Calculate the floor, area and room numbers */

  /* Hint: You may need to use the modulus operator */



    floor = ____________________________________________

 

    area = _____________________________________________

 

    room = _____________________________________________

 

  /* Print the results */

 

    printf(“Floor number: %i\n”,floor);

    printf(“Area number: %i\n”,area);

    printf(“Room number: %i\n”,room);

 

}

 

 

 

 

 

7.      Consider the following program:

<