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 8, 2010

 


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

6

 

 

2

6

 

 

3

6

 

 

4

6

 

 

5

5

 

 

6

6

 

 

7

6

 

 

8

6

 

 

9

6

 

 

10

6

 

 

11

8

 

 

12

9

 

 

13

8

 

 

14

6

 

 

15

8

 

 

16

9

 

 

17

8

 

 

18

10

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

Use the figure below for the next three questions. Assume that your home directory is pictured as in the figure below. Thus, your home directory contains files named data2.txt and test.c . Your home directory also contains the directories named lab8 and fun, the directory lab8 contains the file named data.txt , the directory fun contains files named lab8.c and data3.txt and a directory named lab9.   

 

 

 

 

1.      Assume that your current directory is lab8.  Write a single Unix command to copy all the files located in the directory named fun into the lab8 directory.

 

 

_____________________________________________________________

 

 

 

2.      Assume that your current directory is lab8.  Write a single Unix command to move only the file named data2.txt located in your home directory into the lab8 directory.

 

 

_____________________________________________________________

 

 

 

3.      Assume that your current directory is your home directory. Write a single Unix command to move the directory named lab9 so that it becomes an immediate subdirectory of your home directory.

 

 

_____________________________________________________________

 

 

 

4.      The following program compiles and runs without errors. Write the output produced by the program.

 

 

#include <stdio.h>

 

void main(void)

{

     int value = 12;

     int max = 10;

     int min = 5;

 

if (value > max )

          printf("too large! ");

     if (value < min)

          printf("too low! ");

     else

          printf("greater than min! ");

}

 

 

_____________________________________________________________

 

 

 

 

 

 

 

 

 

5.      Write the correct results of evaluating the following C language expressions:

 

 

a)      9 / 2                  _____________________

 

b)      9 % 2                =   _____________________

 

c)      2 / 9                =   _____________________

 

d)      2 % 9                =   _____________________

 

e)      13 / 5 + 7 * 4      =   _____________________

 

 

 

 

 

 

 

 

 

 

 

6.      Fill in the blanks to correctly print the values asked for in the comments.

 

 

#include <stdio.h>

 

void main(void)

{

int x = 8;

     float y = 10.0;

char z = 'R';

 

/* print the value of x on the screen */

 

printf("%___________ \n", _______________ );

 

 

/* print the value of y on the screen */

 

     printf("%___________ \n", _______________);

 

 

/* print the value of z on the screen */

 

     printf("%___________ \n", _______________);

 

}

 

 

 

 

7.      Circle the correct choice of commands you would type at the UNIX prompt to compile and run the C program located in the file myExam.c . The program uses the math library. Circle only one answer choice.

 

 

 

a)   gcc myExam.c –include <math.h>

     a.out

 

 

b)   gcc myExam.c –lm –o exam

     ./exam.out

 

 

c)   gcc myExam.c –o math

     .\math

 

d)   gcc myExam.c -lm

     ./a.out

 

8.      The following C program compiles and runs without errors. Circle the correct output produced by this program. Circle only one answer choice.

 

#include <stdio.h>

 

void main(void)

{

int x = 4;

if (x%3 == 1)

{

switch(x/2)

          {

case 3:  printf("A ");

break;

case 2:  printf("B ");

break;

case 1:  printf("C ");

break;

default: x++;

}

     }

    

if (x%2 == 0)

{

switch(x/2)

          {

case 3:  printf("X ");

break;

case 2:  printf("Y ");

break;

case 1:  printf("Z ");

break;

default: x++;

}

 

if (x < 5)

               printf("P " );

          else

printf("Q " );

    }

 }

 

a) B Y P

 

 

b) B

 

 

c) C Z P

 

 

d) C Y Q

9.      For each set of variable values given below, write the output that the following code fragment would produce if the fragment was included in a complete C program and compiled and run without errors. You may assume that the variables, a, b, c and d are declared as integer data type variables.

  

 

if (a < b)

if (c >= d)

         printf("A");

     else

if (a <= c && a != d)

              if (a == c)

                   printf("B");

             else

                    printf("C");

else

if (a < d)

                    printf("D");

else

                    printf("E");

else

printf("F");

 

 

 a = 2, b = 5, c = 3, d = 3

 

 

_____________________________________________________________

 

 

a = 3, b = 4, c = 2, d = 5

 

_____________________________________________________________

 

 

a = 1, b = 1, c = 1, d = 1

 

 

_____________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

10.  The following program compiles and runs without errors. Write the output produced by the program.

 

 

#include <stdio.h>

 

void main(void)

{

int x,y,z;

x = -1;

 

if (x > 0)

printf("positive! ");

 

y = 0;

 

if (y = 1)

printf("equal! ");

else

printf("not equal! ");

 

z = 3;

 

switch(z)

{

case 1:  printf("one ");

 

case 2:  printf("two ");

 

case 3:  printf("three ");

 

default: printf("not valid!");

 

}

 

printf("\n");

}

 

 

 

_____________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

11.  The following program compiles and runs without errors. Write the output produced by the program.

 

 

#include <stdio.h>

 

 void main(void)

 {

int i,j,k;

 

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

     {

          for(j = 1; j < 3; j++)

          {

              for(k = 1; k < 3; k++)

                   printf("%i ", i*j*k);

          }

     }

 }

 

 

________  ________  ________  ________  ________  ________  ________  ________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12.  Which of the following code fragments assign the value 1 to every element of the array named Array?  Circle each correct answer. There may be more than one correct answer.

 

 

a)

int Array[10][10] = {1};

    

 

b)

int Array[10][10];

int i,j;

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

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

          Array[j][i] = 1;

 

 

c)

int Array[10] = {1};

int i;

 

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

Array[i] = Array[i - 1];

 

 

 

 

 

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

 

 

#include <stdio.h>

 

void main(void)

{

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

int i, j;

 

for(i=3 ; i > -1 ; --i)

{

j=i;

Array[i] = ++j;

}

 

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

{

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

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

}

}

 

 

___________ ___________ ___________ ___________

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', '\0', 'G', 'E', '\0'};

    

printf("%s \n", fruit1);

printf("%s \n", fruit2);

printf("%s \n", fruit3);

    

int i;

    

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

{

fruit2[i]= fruit1[i];

fruit3[i]= fruit1[i];

}

 

     printf("%s \n", fruit1);

printf("%s \n", fruit2);

printf("%s \n", fruit3);

 

 }

 

 

_____________________________________________________________

 

 

_____________________________________________________________

 

 

_____________________________________________________________

 

 

_____________________________________________________________

 

 

_____________________________________________________________

 

 

_____________________________________________________________

 

 

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

 

 

3 2 1

6 5 4

 

 

#include <stdio.h>

 

void main(void)

{

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

int row, col;

    

/* This code prints the table as shown above */

 

     for(_________;_________;_________)

 

for(_________;_________;_________)

{

               printf("_______", mat[row][col]);

 

               if( col == _______)

               printf("\n");

          }

}

 

 

 

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


#include <stdio.h>


int coolest(int i)
{
  int x = 10;
  return x + i;
}

void main(void)
{
  int i = 39 , x = 1;
  int z = coolest(i);

  printf("%i %i %i \n", i , x , z);
}

    

 

 

________________  ________________  ________________

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

 

 

#include <stdio.h>

 

int fun(int x)

{

if(x%2)

{

  if(x += 1)

               return x;

             else

     return x/2;

}

return x-1;

}

 

void main(void)

{

printf("%i ",fun(3));

printf("%i ",fun(4));

}

 

 

 

________________  ________________ 

 

 

 

 

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

 

 

#include <stdio.h>

 

void fun(int x[])

{

x[0] = x[0] + 5;

}

 

void main(void)

{

  int y[2] = {3,8};

 

  fun(y);

  printf(" y[0] = %i , y[1] = %i \n", y[0], y[1]);

}

 

 

________________  ________________