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.


    

 

 

 

 

 

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

                 command to make the lab8 directory your current directory.


 

 



            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.

 

 

 

 

      

 

     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.

 

 

 

 

 

 

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.

 

 

 

 

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;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

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 = _________________________________



j = ____________________________________

 

 

 

 

 

 

 

 

 

 

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

 

    if ((a <= 5) && (a >= -5))

        printf("A ");

     if (a != 5)

        printf("B ");

     if ((a >= 10) || (a <= 5))

        printf("C ");

     printf("\n");

}

 

   

 

 

 

 

 

 

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

}

 

 

 

 

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

 

#include <stdio.h>

 

void main(void)

{

  int i;

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

    printf("Blah\n");

  printf("%d", i);

}

 

 

              

               

 

 

8.            Complete the program below by filling in the blanks with the correct answers.  The code below should output all the prefixes of the string “abcde”. Your output should be exactly as below:

 

abcde

abcd

abc

ab

a

 

#include <stdio.h>

#include <string.h>

 

void main(void){

  char a[] = “abcde”;

  int i,j;



      for(i = 5; i > ______
_______; i=i-1)( 3 points)

    {

      for( j = 0; j < i; j=j+1)

          printf(“%c”, ___
______________);

      printf(“\n”);

    }     

}

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

void main(void){

   int i = 14.99;
   int j = 5;    
   int k = 2; 

   double x,y;
  
   x =  1 / 6;
   y = 1 / 2.0;
   printf("%i %f %f \n", i, x, y);



      printf( "%i %i %i %i \n", j/k , k/j, k%j, j%k);
 

 

}

 

 

         _______________________________________________________________




        _______________________________________________________________

 

 

 

 

 

10.        Given the following C code,

 

 int i; 

 

 i = 0;

 while(i < 5){

    printf("%d ", i);

    i++;
}

        complete the following code by filling in the blanks so that the code below produces the exact same  output as the code above.

int i;

for (i= _________________; __________________; __________________ )
    

     printf("%d ",___________________);

11.        Write C statement(s) that declares a 2-dimensional array named mat of size 100 by 100 of  datatype float and initializes all 10,000 elements of mat to zero.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12.        Complete the C code fragment below by filling in the blanks.  The code fragment should read a string (not containing spaces) typed by the user into the array named scannedString.

 

 

char scannedString[1024];

 

scanf( ________________, _________________________);

 

 

 

 

 

 

13.        Given the following declarations of integer variables, a, b and c and an integer array named y,

 

int a = 0, b = 0, c = 0;

int y[4] = {2, -3, 5, 100};

 

fill in the blanks with the values that were assigned to the variables, a, b, c and  y[3].

 

a) a = y[0];

     a = ________________________

 

b) b = y[++y[0]];

 

  b = _________________________

  

c) y[3] = c--;

 

  y[3] = __________________

      c = _____________________

 

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

 

#include <stdio.h>

#include <string.h>

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

 

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

 

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

 

  a = 7;

 

    }

 

int didsumpin(char e[]){

 

  int len = strlen(e);

     int c;

  strcpy(e , “horse”);

     c = 100;

  return  len;

    }

 

    void main(void){

      int  a = 9;

int c;

      double b[2] = {4.0,3.0};

      char animal[] = “elephant”;

 

dosumpin(a,b);

 

printf("a = %i  b[0] = %lf  b[1] = %lf \n", a, b[0], b[1]);

 
    c = didsumpin(animal);

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

 

}

 

 

___________________________________________________________

 

 

 

 

     _______________________________________________________________________

 

 

 

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

 

#include <stdio.h>

 

int f(int a, int b) {

    return a + 2 * b;

}

 

void main(void) {

    int a, b, c;

    a = 3;

    b = 5;

    c = f(b, a);

    printf("%d ", c);

}

 




_______________________________________________________

 

 

 

 

16.        Write a complete function named comp that has two input parameters named a and b. The function comp returns an integer value. The function comp returns  -1 if a is less than b,  else comp returns 0 if a is equal to b, otherwise comp returns 1 if a is greater than b.  Write only the code for the function comp and NOT a complete program.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

17.        Assume the following code is typed into the file named test.c .  One line of code is missing.

 

 

#include <stdio.h>

 

int f(int x, int y) {

    return x + y;

}

 

void main(void){

  int x, y, z;

  x = 3;

  y = 4;

    /* missing line of code */

    ____________________________________

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

 

}

 

Which of the following lines of code when typed in the blank above in main would NOT generate a compile or run time error when the file is saved, compiled and run?.  Of course we assume that the underscore characters ( __________ ) in the text above would be removed and replaced with the code shown below.

Circle all correct answers:

 

 

a) z = f(y, x);

 

 

 

 

 

b) z = int f(x, y);

 

 

 

 



c) z = int f( int x, int y);

 

 

 

 



d) z = f(int x, int y);