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 6, 2006

 


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.

      You may not use any electronic devices, book, notes or other references during this examination.

 

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

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Score

Grader

1

6

 

 

2

9

 

 

3

4

 

 

4

4

 

 

5

6

 

 

6

4

 

 

7

4

 

 

8

4

 

 

9

8

 

 

10

6

 

 

11

6

 

 

12

4

 

 

13

4

 

 

14

10

 

 

15

12

 

 

16

10

 

 

17

8

 

 

18

16

 

 

Total

125

 

 

A

 

 

1.      You are in your home directory. 

 

a) Write the Unix command to create a subdirectory with the name “lab9”.

 

______mkdir_lab9_______________________________________________

 

b) Write the Unix command to make “lab9” your current directory.

 

______cd __lab9______________________________________________

 

c) Assume that you are in your “lab9” directory.  Your home directory contains (only) the files: “readme.txt”, “prog.h”, “prog.c”, and “test.dat”.  Write a SINGLE Unix command to copy all four of these files into your “lab9” subdirectory with one command.   (Hint:  You may use the wildcard "*"). 

 

____cp ~/*  .___OR _____cp ../*.*_____OR___cp jsmith/*  ._if student’s netid is jsmith_____


 

 

 

2.      You are in your home directory. You have the following subdirectories:

 

  subdirectory:   files in it

  -------------   ------------

  lab1            intro.c    calc

  html            index.html

  mp1             find_h.m   main.m

  mp2             <empty>

 

Write a SINGLE Unix command to perform each of the following actions:

 

a)      Copy the file “intro.c” from the “lab1” directory to a file named “newfile” in your home directory.

 

 

______cp lab1/intro.c  newfile_________________________________

 

b)  Move the file “main.m” from the “mp1” directory to the “mp2” directory.

 

 

_______mv mp1/main.m  mp2_________________________________

 

c) Rename the “mp1” directory “old_mp1”.

 

 

_______mv mp1  old_mp1_______________________________________________

 

3.      Which of the following is (are) valid C function prototype(s).  Circle ALL valid prototypes.

 

a)         function output = fun(int first, int second);

 

b)         int fun(int first, int second);



c)         int fun(int, int);



d)         int fun(first, second)

 

 

 

 

4.       Given the following code:

 

#include <stdio.h>

 

int diff(int a, int b) {

     return a – b;

}

 

void main(void) {

     int a = 1;

     int b = 2;

     int out = diff(b,a);

     printf(“Out is %i \n”, out);

}

 

Fill in the blank with the correct output of the program:

 

Out is _____1________________

 

 

 

 

5.      Fill in the blanks below to create a loop that prints the first ten powers of 2, specifically,
      2    4    8    16    32    64    128    256    512    1024
     You may NOT use any math functions from the C math library.


    for( i = _____2__________ ; _____i<=1024__ ; _i=2*i_____)
    {
        printf("%d ", i);
    }

 

6.      Given the following code:

 

#include <stdio.h>

 

void plus_one(int x) {

      x = x + 1;

}

 

void main(void) {

     int x = 1;

     plus_one(x);

     printf("x is %i \n", x);

}

 

Fill in the blank with the correct output of the program:

 

x is _______1_________________

 

                                                                                                                           

           

 

7.      Fill in the blanks with the correct conversion specifiers associated with the corresponding variables.


       int w;

                float x;

                char y;

                double z;


    scanf(“____%i__________”, &w);                         

 

    scanf(“____%f__________”, &x);                          

 

    scanf(“_____%c_________”, &y);                                     

 

    scanf(“______%lf________”, &z);                                     

 

 

 

 

 

8.      Assume we have declared the two dimensional array B with the following C code

 

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

 

Write the value of B[2][1] below.

 

 

            _________1____________________________________________                

9.      Which of the loops below produces the output:
 10  8  6  4  2
 Circle ALL correct answers.

 

a) for(i=10; i>0; i-=2)
             printf("%d ", i);



b)   i = 12;
         while(i>0){
       i-=2;
        printf("%d ", i);
     }


c)   i = 12;
           do{
         i-= 2;
         printf("%d ", i);
     } while(i>0);


d)    for(i=11; ; i--){

      if(i == 1)

  break;

    printf("%d ", --i);

   }



 

10.  Consider the following C code

 

  #include <stdio.h>
  #include <string.h>

 

  void main(void)

  {

     int a = 4;

     char team[] = {'R', 'e', 'd', 's', '\0'};

     /* a = 5; */

     printf(“%d %d \n”, a , strlen(team));

  }

 

Fill in the blank with the correct output of the program:

 

 

            ____________4_____________4______________________

11.  Write the output the following program produces when run.

#include <stdio.h>
#include <stdlib.h>

void main(void)
{
   char password[32] = "CS101
";
 

   printf("%c \n",password[1]);
   strcpy(password, "CS105");
   printf("%s \n", password);
   i
f ((rand() % 10)  == 11)
      printf("
Isadora Duncan \n");
   else
      printf("worked
for Telefunken!\n");
}


_________S______________________________

_________CS105______________________________

_________worked for Telefunken!_________________


 

 

12.  The following program computes the area of a right-triangle using the math formula
 area = ½ *base*height.  Fill in the blanks with one of the following expressions, Compile,
 Run-time, Logical, or No Error , depending on whether the line of code on the left will produce a Compile(syntax) error or a Run-time error or a Logical error or No Error , if all the other errors (if any) were corrected.

             #include <stdio.h>

     void main(void){

 

  float base= 0.0, height = 0.0, area = 0.0;              

  printf("Enter base, height: ")   _____Compile___missing ;__

  scanf("%f %f", base, height);    _____Run-time __& seg fault_

                 area = 1/2*base*height;                      _______Logical __1/2  = 0____________

 

                 printf("Area = %f \n", area);    _____No Error___________

             }

 

13.  Which of the following are valid array declarations? Circle ALL correct answers.

 

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




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




c)  float c[5] = {1.0, 2.0, 3.0};




d)  char d[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘\0’};

 

 

 

 

 

 

 

14.  Write the C code necessary to complete the following function myAbs, which takes integers x and a as input variables and returns |x – a|  , the absolute value of the difference between x and a .  For example, myAbs(5,4) returns 1, myAbs(4,5) returns 1, and myAbs(0,0) returns 0.   You may use any function in the C math library. Do not use the “printf” function.
 
 
 
 int myAbs(int x, int a) {
 
   /* write your code here */
 
 
    return abs(x-a);
 
        OR
 
         if ( x >= a)
            return x-a;
         else
       return a-x;
 
 
 
}

 

 

15.  The following program is compiled and executed without error. Write the output of the program:

 

#include <stdio.h>
 
void changeval(float b[], int a, float val);
 
void main(void){
  int  a = 1;
  float b[2] = {4.0,3.0};
 
  printf("Output 1:  %i  %lf  %lf \n",a,b[0],b[1]);
  changeval(b, a, 5);
printf("Output 2:  %i  %lf  %lf \n",a,b[0],b[1]);
}
 
void changeval(float b[], int a, float val){
  b[a] = val;
  a = 7;
}

 

 

_______1_________4.0___________3.0______________________


_______1_________4.0___________5.0______________________

 

 

 

 

16.  Complete the following function named min by filling in the blanks. This function returns the minimum value of an array data. The function min has two input parameters, an array data of datatype float and a variable length of datatype int that contains the number of elements in the array. Assume that length is greater than 0. The array data can contain any range of float datatype numbers.

 

float min(float data[ ] , int length){

int i;

/* declare and initialize the variable min_value */

float min_value = ____data[0]________________ ;

 

/* loop thru the array, if a smaller value */

/* is found, update the value of min_value */

for(i=0; ____i < length_________________ ; i++){

if (____data[i]___________ < ______min_value___________ )

min_value = data[i];

}

return ___min_value________________ ;

}

17.  The following program compiles and runs without errors. Write the ouput the following code produces given the input listed below.

#include <stdio.h>

  void main(void){
    int no;
    int ir, hr;
    printf("Enter a number:");
    scanf( "%i", &no );
    ir = 1;
    hr = 1;
   
    switch(no){
      case 1:  ir = ir + 1;
   
      case 2:  ir = ir + 2;
   
      case 3:  ir = ir + 3;
      
      case 4:  ir = ir + 4;
    }

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

    if (no
== 1){
        hr = hr - 5;
    } else if ( no
== 2 ) {
        hr = hr - 3;
    } else if ( no
== 3 ) {
        hr = hr + 1;
    } else {
        printf( "The input no (%i) is too large\n", no ); exit( 1 );
    }

    printf( "hr = %i\n", hr );
  }
    
a) If the input "no" is 1, write the output the program produces:

______ir = 11____________________________________________________________


______hr = -4____________________________________________________________

 
b) If the input "no" is 4, write the output the program produces:

 

______ir = 5___________________________________________________________


______The input no (4) is too large__________________________________

 

18.  Write C code for a complete function named sumodd that has two input parameters named nums and count.  The input parameter nums is an array of integer numbers, count is of integer datatype and stores the number of elements in nums. You may assume that the value of count is greater than zero. The function sumodd returns an integer value. The function sumodd returns 1 if the sum of all the elements of nums is an odd number and 0 if the sum of elements in nums is even.  The function header is provided below.  Do not write a complete C program, just the code for the function sumodd .  Do not use the “printf” function.

Hint: you may use % to determine odd or even.

 

int sumodd( int nums[] , int count){

 

    /* declare any variables you use here */

 

    int i;

    int sum = 0;

 

 

 

    /* compute the sum of the integers in the array */

 

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

       sum += nums[i];

 

 

 

 

 

    /* if the sum is odd return the value 1 else return the value 0 .*/