Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam II (60 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: 4/9/09

 


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

5

 

 

3

8

 

 

4

4

 

 

5

8

 

 

6

9

 

 

7

6

 

 

8

8

 

 

9

4

 

 

10

6

 

 

11

10

 

 

12

9

 

 

13

6

 

 

14

9

 

 

15

8

 

 

16

9

 

 

17

12

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

1.      Write a single Unix command make the root directory be your current directory. (Hint: The root directory has the name “/”).

 

 

____________________________________________________________________



 

 

 

2.      Suppose you wrote a program in C, saved in the file named myprog.c. Write a single Unix-style command to compile the program. Then write another command to run the program and have the output redirected to a file called output.txt. The C program does NOT use the math library.

 

 

____________________________________________________________________

 

 

 

____________________________________________________________________



3.      Write a single Unix-style command to move the file ~/cs101/project/myprog.c to the ~/cs101/secret/ directory and at the same time rename the file so it's called secretprog.c .

 

 

____________________________________________________________________

 

 

 

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

 

#include <stdio.h>

 

void main(void)

{

      float ans = 1.23456;

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

}

 

 

 

____________________________________________________________________

 

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

#include <stdio.h>

 

void main(void)

{

  int a = 7;

  int b = 5;

  int c, d, e, f;

 

 

  c = b/a;

  d = b%a;

  e = a/b;

  f = a+b/2;

 

  printf("%i ", c);

  printf("%i ", d);

  printf("%i ", e);

  printf("%i ", f);

}

 

 

 

_____________  _____________  _____________  _____________

 

 

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

#include <stdio.h> 

void main(void)
{
  int i = 14.99;
  double x, y;
  
  x =  1 / 6;
  y =  1 / 2.0;
  printf("%i %lf %lf \n", i, x, y);
}

        _________________  _________________  _________________ 

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

#include <stdio.h>

void main(void)
{
   int a = 0;
   int b = 1;
   int c = 2;
   int d = 1;

   if ( ((b >= 1) && ( c < 2)) || (c < 3) ) 
      printf("A ");
 
   else if ((d > 5) || (c < 3))  
         printf("B ");
   else
         printf("C ");


   if ( !((a > 2) || ( a <= 2)) )          
         printf("D ");


   if (--d)
       printf("E ");
   else
       printf("F ");

   printf("\n");
}

 




________________________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      Which of the following code fragments will assign to the variable named grade the value 'P' if score is 4 or 5 and 'F' if the score is any other value? Circle each correct choice. There may be more than one correct choice.  You  may assume that score has been declared as a variable of datatype int and grade has been declared as a variable of datatype char .

a)
  switch (score)
  {
       case 4:
       case 5:
               grade = 'P';
               break;
       default:
               grade = 'F';
  }

b)
  switch (score)
  {
       case 4,5:
               grade = 'P';
               break;
       default:
               grade = 'F';
  }


c)
  switch (score)
  {
       case 4:
       case 5:
               grade = 'P';
              
       default:
               grade = 'F';
              break;
  }

 

d)
  switch (score+1)
  {
       case 5:
       case 6:
               grade = 'P';
               break; 
       default:
               grade = 'F';
              break;
  }

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

#include <stdio.h>

 

void main(void)

{

int i = 0;

 

while(i = 0)

i = i-1;

 

printf("%i",i);

}

 

 

________________________________________________________________________




10.  The file named index.dat contains the following three numbers:

 

3

4

5

 

The following C program compiles and runs without errors.

#include <stdio.h>

 

void main(void)

{

int value, temp=1;

int i=1;

scanf("%i",&value);

do

{

temp = temp*value;

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

 

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

 

}

 

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

 

a.out < index.dat

 

 

________________________________________________________________________

11.  Which of the following expressions represent correct ways of writing a prototype for the function named fun that takes a single integer argument as input and returns a double? Circle each correct answer. There may be more than one correct answer. 

 

a) int fun(double x);



 

b) double fun(int x);



 

c) int x = fun(double []);



 

d) double fun(int);



 

e) double = fun(int []);

 

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

#include <stdio.h>

 

void main(void)

{

  int value;

  int mat[4][3]={{1,3,5},{7,2,4},{6,8,15},{17,34,12}};

  int row, col;

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

  {

      value = 9999;

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

      {

         if (mat[row][col] < value)

         {

            value = mat[row][col];

         }

      }

      printf("%i ", value);

  }

}



_________________  _________________  _________________

 

13.  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 = 2 * y;

  y = 2 * x;

  return y;

}

 

void main(void)

{

  int x = 1;
  int y = 2;

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

}

 

 

 

_______________________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



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

 

#include <stdio.h>

#include <string.h>

 

void func(char str[])

{

   int i = 0;
   char temp;

   int size = strlen(str);

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

   {    

     temp = str[i];

     str[i] = str[size - i - 1];

     str[size - i - 1] = temp;

   }

}

void main(void)

{

  char str1[5] = {'d' ,'o', '\0', 'g', '\0'};

  char str2[]  = "cat";
  char str3[32]= "mickey ";

 

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

  func(str2);

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

  strcat(str3, "mouse");
  printf("%s\n", str3);

}

 

 

 

 

   __________________________________________________________

 

 

 

 

 

     __________________________________________________________

 

 

 

 

__________________________________________________________

 

 

15.  Which of the following code fragments assign the value 2 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];

int i,j;

for (i = 0, j = 0; i < 10 && j < 10; i++, j++)

Array[i][j]=2;

 

 

 

b)

int Array[10];

int i=0;

Array[0] = 2;

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

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

 

 

 

c)

int Array[10];

int i = 0;

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

{

   Array[i] = 1;

   Array[i]++;

}

 

 

 

d)

int Array[10][10];

int i,j;

 

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

{

     Array[0][i] = 2;

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

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

}

 

 

 

 

 

 

 

 

 

 

 

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

#include <stdio.h>

 

int update(int a[], int size, int i, int x)

{

if (i < 0 || i >= size)
        return 1;

    a[i] = x;

    return 0;

}

 

void main(void)

{

  int a[4] = {3, 4, 2, 7};

  int r = update(a, 4, 3, a[2]);

  printf("%i %i %i", a[r] , a[2] , a[3]);

}

 

 

__________________  __________________  __________________


17.  Complete the following C program by filling in the blanks. This program should prompt the user to enter two integers. Then the program should call the function named test. If the two integers are equal the test function returns the character 'T' otherwise the function returns 'F'. The function main then displays the character that test returns.

 

 

#include <stdio.h>

 

char test(int x, int y)

{

  if ( ____________________ )

     return ___________ ;
  else

     return ___________ ;

}

 

void main(void)

{

  int a, b;
  char c;

  printf("Enter two integers: ");

        scanf("%i %i", &a, &b);

        ___________  = test( ___________ , ___________ );

        printf("%c",c);

}