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 3rd, 2008

 


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

6

 

 

2

6

 

 

3

6

 

 

4

6

 

 

5

6

 

 

6

5

 

 

7

5

 

 

8

10

 

 

9

8

 

 

10

8

 

 

11

8

 

 

12

9

 

 

13

8

 

 

14

6

 

 

15

5

 

 

16

10

 

 

17

13

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

  1. Assume that in your home directory there is a directory named "Dir1" and inside "Dir1" there is a directory named "Dir2".

 

Write a single Unix command to make "Dir2" your current directory. This command should work regardless of what the current directory is at the time you are typing it at the Unix prompt.

 

 

_________cd  ~/Dir1/Dir2   or  cd  ~netid/Dir1/Dir2__________________________

 

 

 

 

  1. Write a single Unix command that would display the name of your current (working) directory when typed at the Unix terminal.  

 

 

________pwd______________________________________________________________

 

 

 

 

  1. Assume your current directory has a subdirectory called "Sdir".  Write a single Unix command to list the names of all the files located in "Sdir" without changing directory.

 

 

________ls  Sdir______________________________________________________________

 

 

 

 

 

 

  1. Circle the only true statement about the Unix command "mv" listed below.

(Circle only one choice.)

 

a)         It can be used to copy a file without deleting the original version.



b)          It cannot be used to move files from one directory into another.



c)         It can be used to rename a file.


 

d)         It launches a program called "my voicemail".

 

 

 

 

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

 

#include <stdio.h>

#define LARGE 20

#define SMALL 10

 

void main(void)

{

   int value = 7;

   if (value > LARGE)

       printf("large\n");

   if (value > SMALL)

         printf("med\n");

   else

         printf("small\n");

}

 

 

__________small______________________________________________________

 

 

 

 

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

 

a)      5 / 3     =   ________1_____________
            

b)      5 / 6     =   ________0_____________

c)      5 % 3     =   ________2_____________

             

d)     3 % 5     =   ________3_____________

 

e)      6 + 3 / 2 =   ________7_____________

 

 

 

 

  1. Complete the following line of C code to declare a 3x3 matrix named "wow" so that the matrix holds the following values at each position:

 

 

int wow[ _3  or blank_ ] [ _3  not blank__ ] = { __{1, 9, 1}, { 2 , 2, 3}, {4, 5, 6}________ };

                                                                                    (inner curly braces not required)

 

 

 

  1. Consider the following C program:

#include <stdio.h>

 

void main(void)

{

     int x;

     scanf("%i", &x);

     switch (x/32)

{

          case 0:

              printf("Too cold!\n");

              break;

          case 3:

              printf("Too hot!\n");

              break;

          case 2:
          case 1:

              printf("Just right!\n");

              break;

          default:

              printf("Weird weather!\n");

              break;

     }

    

}

 

Fill in the blanks below to re-implement the program using if statements. After you enter your code below the program should be equivalent to the program above.

#include <stdio.h>

 

void main(void)

{

     int x;

     scanf("%i", &x);

 

     if (_______x/32 == 0__________________)

printf("Too cold!\n");

     else if (_______x/32 == 3______________________________) 

               printf("Too hot!\n");

          else if (____(x/32 == 2) || (x/32 == 1)__________) 

                    printf("Just right!\n");

               else

                    printf("Weird weather!\n");

}

 

 

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

 

if (a < b)

if (c < d)

      printf("A");

   else

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

   if (a == c)

            printf("B");

             else

            printf("C");

else

if (c > d)

              printf("D");

else

              printf("E");

printf("F");

 

 

 

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


      ____
AF___________________________________________________

 

b.      a = 3, b = 4, c = 2, d = 1


      ____
DF___________________________________________________

 

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



____F___________________________________________________

 

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



                                    ____CF___________________________________________________

 

 

 

 

 

 

 

 

 

 

 

  1. Which of the following are infinite loops (do not terminate)? You may assume that the variable i is an integer variable. Circle Infinite if the loop is infinite or Terminate if the loop terminates, for each loop.

 

a)        Infinite                      Terminate

for (i = 1; i == i + 1; i = i - 1)

{

          printf(" loop \n");

}

 

 

b)        Infinite                      Terminate

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

{

          if (i > 10)

              break;

}

 

 

c)        Infinite                      Terminate

i = 0;

while(i < 1)

{

          printf(" loop \n");

}

i = i + 1;

 

 

d)       Infinite                      Terminate

do{

i = 0;

          printf(" loop \n");

i = i + 1;

}while(i < 1);

 

  1. Fill in the blanks to complete the following program so that it reads characters one at a time, prints "Good" when 'g' is input, and stops when ' ' (a space) is input:

 

#include <stdio.h>

 

void main(void)

{

    char c;

 

    do {

        scanf(_____"%c"________, _______&c___________);

 

        if (__________c == 'g'_____________)

            printf("Good\n");

       

    } while (______c != ' '_________);

}

  1. Complete the code for the following function named funstring. The function funstring has two parameters named a and b that contain strings. The function funstring compares any two input strings that a and b contain and copies the shorter string to the longer string. For example, if a contains "cat" and b contains "mouse" then copy "cat" into b. Also, the function must return -1 if the string a contains is longer, 1 if the string b contains is longer, and 0 otherwise.

 

int funstring (char a[], char b[])

{

  int lena = strlen( __a___ );

 

  int lenb = strlen( __b___ );

 

  if (lena < lenb)

  {

      strcpy( ___b_____ , ___a_____);

 

      return __1____ ;

  }

  else if (lena > lenb)

       {

           strcpy( ___a_____ , ____b____);

 

           return __-1____ ;

       }

       else

           return __0____ ;

}

 

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

#include <stdio.h>

 

void main (void)

{

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

 int i, j, temp;

 

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

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

    {

              if (array[i] > array[j])

         {

                 temp = array[i];

                 array[i] = array[j];

                 array[j] = temp;

              }

         }

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

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

}

 

________2      3          4          7_________________________

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

 

#include <stdio.h>

 

void func(int y[], int x)

{

   if (x >= 2)

   {

       y[0] = x / 2;

       y[1] = x;

   }

   x = 15;

}

 

 

void main(void)

{

  int x[2] = {4, 10};

 

  func(x, x[0]);

 

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

 

}

 

 

 

x[0] = ____________2__________ , x[1] = _______4_______________

 

 

 

  1. Circle each statement below that has an error, either a syntax error, a run time error or a logical error. You may assume that the variables have been declared as follows:

 

float  power;

double resistance;

 

a)      scanf("%f", &power);

 

b)      scanf("%lf", resistance);



c)      printf("Resistance is %d (ohms)\n", &resistance);



d)     printf("Resistance is %lf (ohms)\n", resistance);

 

e)      printf("power = %f \n" power);



  1. 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 password[32] = "CS101";
   char course[32] = {'C','S','1','\0','5'};
   printf("%s \n", password);
   printf("%s \n", course);
   printf("%c \n",password[1]);
   strcpy(password, course);
   printf("%s \n", password);
   if (strcmp( password, "CS101")  == 0)
      printf("yes \n");
   else
      printf("no \n");
}

 

_______CS101________________________________


_______CS1________________________________


_______S________________________________


_______CS1________________________________


_______no________________________________

 

 

 

 

 

 

 

 

  1. Write a complete function named step . This function has one input parameter x of data type double. The function step returns a value of data type double.  If the value of x is greater than or equal to 0.0 then the function step returns the value 1.0 otherwise step returns 0.0 .
     

For example, once your function step has been written it should produce the following results when it is called from main :

 

#include <stdio.h>

 

double step( double x);

 

void main(void)

{

printf("%.2lf \n", step(3.5));
     printf("%.2lf \n", step(-7.1));

}

which would display the result:

 1.00
 0.00

      Of course your code should work correctly for any values and not just for 3.5 and -7.1 as shown above . Write your code for the function step below.

Do NOT write the code for main.
Do NOT include comments in your code.

Do NOT use the printf or scanf function in your code. This will result in a score of zero.

 

 

double step( double x)

{

   if (x >= 0.0)

       return 1.0;

   else

       return 0.0;

}

or

double step( double x)

{

   if (x >= 0.0)

       return 1.0;

   return 0.0;

}

or

double step( double x)

{

   if (x >= 0.0)

       return 1.0;

   if (x < 0.0)

       return 0.0;

}

 

or

 

double step( double x)

{

       return  x >=0 ;

}

 

or using the conditional operator  ? :

 

double step( double x)

{

       return (x >=0) ? 1.0 : 0.0 ;

}