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 5, 2009

 


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.



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, book, notes or other references during this examination.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Deduction

Grader

1

4

 

 

2

8

 

 

3

6

 

 

4

5

 

 

5

5

 

 

6

5

 

 

7

6

 

 

8

8

 

 

9

8

 

 

10

5

 

 

11

8

 

 

12

5

 

 

13

6

 

 

14

9

 

 

15

12

 

 

16

8

 

 

17

8

 

 

18

9

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

 

1.      Write a single Unix command that will display your current directory name.

 

 

_____________________________________________________________

 

 

2.      Write a single Unix command to copy the file named guess located in the directory ~cs101ta/lab8open ,  into your current directory.

 

 

_____________________________________________________________

 

 

 

3.      Write a single Unix command that will display all the files and folders in your home directory. You may NOT assume that you are in your home directory.

 

 

_____________________________________________________________

 

 

4.      Assume that a file mystery.c contains a complete C program and is located in the current directory. Circle the command below that compiles the program and names the executable mystery.exe rather than a.out . There is only one correct answer.

 

 

a) gcc mystery.c


 

b) gcc mystery.c  -o mystery.exe


 

c) gcc mystery.c  -o a.out

 

 

d) gcc mystery.c  mystery.exe  -o

 

 

 

 

 

 

 

 

 

 

 

5.      The following C program produces a segmentation fault. Circle which statement causes the segmentation fault. There is only one correct answer.

 

#include <stdio.h>

void main(void)
{

 

  /* a) */    float i, j;



  /* b) */    printf("Please enter two numbers:");



  /* c) */    scanf("%i %i", i, j);



  /* d) */    printf("You entered %i and %i", i, j);

}

 

 

6.      Circle the correct answer choice so that by replacing the blank ______(answer)_______ the C program below finds the maximum of the value of two variables x and y (of data type double) and displays that value. There is only one correct answer.

 

#include <stdio.h>

#include <math.h>

 

void main (void)

{

    double x,y;

    scanf("%lf %lf", &x, &y);

 

    double max =  (fabs(x-y)+ (y-x))/2.0 +  ______(answer)_______;

    printf(" max = %lf \n", max);

}

  Each answer choice below lists an expression to replace ____(answer)_____  above.

a) x



b) y


 

c) fabs(x)


 

d) fabs(y)

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

#include <stdio.h>

 

void fun(int x)

{

   if(x>5)

       if(x > 7)

          printf("LARGE ");

       else

printf("SMALL ");

}

 

void main(void)

{

   fun(1);

   fun(7);

   fun(9);

}

 

__________________________________________________________

 

 

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

#include <stdio.h>

 

void main(void)

{

     int  i = 5,j=5;

     for(i=5; i < 9 ; i=i+1)

     {

          if(i==6)

             break;

     }

     for(j=5; j < 9; j=j+1)

     {

          if(j == 6)

              j=j+5;

          break;

     }

 

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

}

 

i = __________________    j =  __________________   

9.      Which of the following code fragments, when typed into a complete C program that compiles and runs without errors, displays the following output:

 

            0 3 6 9

  

Circle each correct answer. There may be more than one correct answer.
You may assume that the variable i  has been declared as an integer variable.

 

a)

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

      if(i % 3 != 0)

      {

         printf("%i ", i);

      }

   

 

b)

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

      if(i % 3 == 0)

      {

         printf("%i ", i);

      }

   

 

c)

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

      if(i / 3 != 0)

      {

         printf("%i ", i);

      }

   

d)

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

      if( i / 3 == 0)

      {

         printf("%i ", i);

      }

 

 

 

 

 

 

 

 

 

 

 

10.  The following C program compiles and runs without errors.

 

#include <stdio.h>

 

void main(void)

{

     int  i = 0;

 

    

     while(i > 0)

     {

          printf("Hi ");
          i = i - 1;

     }

 

     printf("Bye ");

    

     i = 0;

    

do

     {

          printf("Hi ");
          i = i - 1;

    }while(i > 0);

 

     printf("Bye  ");

}

 

Circle the correct output of this program. There is only one correct answer.

 

a) Hi Bye Hi Bye

 



b) Bye Bye Bye



 

c) Hi Bye Bye



 

d) Bye Hi Bye



 

e) Hi Bye

 

 

 

 

11.  Circle each answer choice(s) below that when placed in the blank below would NOT lead to an infinite loop? There may be more than one correct answer.

 

 

#include <stdio.h>

 

 

void main(void)

{

     int a;

     a=0;

 

________(answer)________________________ 
            {

          a=a+1;
     }

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

}

 

 

 

a) for(  ;  ; --a)





b) for(  ; a<10 ; ++a )





c) for( a=0 ;  ; a--)




 

d) for( a=0; a<10; a++ )

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12.  The file named input.dat contains the following four numbers:

 

1

2

3

4

 

The following C program compiles and runs without errors.

 

#include <stdio.h>

 

void main(void)

{

     int value, temp = 1;  /* temp = 1 not zero */

     while(EOF != scanf("%i",&value) )

     {

          temp = temp + value;

          if( temp % 2 == 0)

          {

              temp = 1;

          }

     }

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

}

 

The above program is compiled with gcc and an executable is created in a.out. Write the output if we run the above program by typing the following command at Unix prompt.

 

a.out < input.dat



_____________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13.  The following program compiles and runs without errors. Circle the correct output of this program. There is only one correct answer.

 

#include <stdio.h>

 

void main(void)

{

int Array[3], Xarray[] = {1 , 2 , 3, 4};

int i;

 

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

{

Array[i] = i;

printf("%i ", Array[i]);

}

 

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

printf("%i ", Xarray[++i]);

}

 

 

a)      0 1 2 1 3

 

 

b)   0 1 2 1 2 3 4

 

 

c)   1 2 2 4

 

 

d)   0 1 2 2 4

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

#include <string.h>

 

void main(void)

{

     char fruit1[16] = {'P', 'E', 'A', 'R', '\0'};

     char fruit2[16] = {'A', 'P', 'P', '\0', 'E', '\0'};

     char fruit3[16] = {'O', 'R', 'A', 'N', 'G', 'E', '\0'};

 

printf("%s ", fruit2);

 

fruit2[3] = 'L';

     strcpy(fruit1, fruit2);

     strcpy(fruit2, fruit3);

 

printf("%s ", fruit2);

 

     if (strcmp(fruit2, fruit3)  == 0)

          printf("yes \n");

     else

          printf("no \n");

}

 

 

 

____________________  ____________________  ____________________ 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

15.  Complete the following C program by filling in the blanks below. When your program runs it should produce the output exactly as shown below.

 

2 1

4 3

6 5

 

#include <stdio.h>

 

void main(void)

{

int mat[ 3 ] [ 2 ] = {1, 2, 3, 4, 5, 6};

int row, col;

 

 

/* This code prints the values of the matrix as shown above*/

 

     for(row= 0;row < 3; ++row)

 

for(___________; ___________;___________)

{

               printf("%i ", mat[row][col]);

               if( col == _________ )

               printf("\n");

          }

 }

 

 

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

 

#include <stdio.h>

 

char  myFunc (char z[])

{

  z[0] = 'J';

  return z[1];

}

 

void main(void)

{

  char x[64] = "HELLO";

  char y;

 

  y = myFunc(x);

  printf ("x = %s ", x);

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

}

 

 

x =________________     y =________________

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

 

#include <stdio.h>

 

int fun(int x)

{

   x = 7;

   return x;

}

 

 

void main(void)

{

  int x = 8;

  int y = 3;

  y = fun(fun(fun(fun(x))));

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

}

 

 

 

x =________________     y =________________

 

 

 

 

18.  The following C program compiles and runs without errors. Write the output produced by this program.

 

 

#include <stdio.h>

 

int myFun(int x, int y)

{

  x = y * y;

  y = y * x;

  return y;

}

 

void main(void)

{

  int x = 5;

int y = 6;

 

 

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

}

 

 

_______________    _______________    _______________