AMAT 2120 - Midterm Test

Answers and solutions

      Test in pdf

1. Check all correct answers.

(a) Suppose Test1 and Test2 are two subdirectories of the same parent directory. There is file myprog.c in the directory Test1. Your current working directory is Test2and you want to save a copy of the existing file under the name prog2.c. Which command will do it?
      Correct answer:     cp ../Test1/myprog.c prog2.c
(The answer mv ../Test1/myprog.c ./prog2.c was less wrong than other incorrect answers (1 mark out of 5). With this command, the user would obtain the desired file in current directory, but the file myprog.c in directory Test1 would be erased.)

(b) If variables int n and double x are declared in the program, then the assignment x=n is

           (Compare carefully with Q.(b) in Quiz)

(c) What is, technically, the word main at the beginning of every C program?
      Correct answer: A reserved name of function recognized by the operating system, which initiates execution of this function.

(d) Which sentence best describes the following tiny program?

   int main()
   {
     while(5){ return(0);}
   }  

(e) A variable i is of type int. The value of the expression i+(!i) is
      Correct answer: i or 1 depending on the value of i. (If i!=0, then (!i) is 0 and the result is i. If i==0, then i+(!i)=0+1=1.)
(The answer "always 1" was considered the least evil among wrong answers and was given 1 mark out of 5. It is a psychological trap to which even experienced programmers are often caught: if you think only of the possibilities i==0 and i==1, then that answer is correct. But i can be any integer!)

2. Programming question: see Lab 8.

3. Trace question. Here is the required table (Only values that are assigned new values or checked in conditions at current step are shown).

 Line |   i  |   j   |   k   |  cond. |  Output
 -----------------------------------------------
 1    |   ?  |   1   |   ?   |        |    
 2    |   1  |       |       | (1<5) T|    
 4    |   1  |       |       | (1<3) T|    
 6    |      |       |20+1=21|        |    
 7->16|      |       |       |        | 21 
 17->2|   2  |       |       | (2<5) T|    
 4    |   2  |       |       | (2<3) T|    
 6    |      |       |20+0=20|        |    
 7->16|      |       |       |        | 21 
 17->2|   3  |       |       | (3<5) T|    
 4    |   3  |       |       | (3<3) F|    
 8->10|      |   1   |       | (1==0)F|    
 12->13      |       |       |        | -- 
 14   |      | (!1)=0|       |        |    
 16   |      |       |  20   |        | 20
 17->2|   4  |       |       | (4<5) T|  
 4    |   4  |       |       | (4<3) F|  
 8->10|      |   0   |       | (0==0)T|  
 11   |      |       |   0   |        |  
 14   |      | (!0)=1|       |        |  
 16   |      |       |   0   |        |  0
 17->2|   5  |       |       | (5<5) F|   
 2->18|   5  |       |       |        |  5
The resulting output is 2120 -- 2005. Here is the program; you can insert intermediate printf's to check the table.

4. By taking sample nonnegative value(s) of x and tracing the code guess the purpose of the following function:

10  void f(int x)
11  {
12    while (x)
13    {
14      printf("%d", x%10);
15      x=x/10;
16    }
17  }
Tracing table: (My sample value is 735)
 Line  |  x  | cond  |  Output
 -----------------------------
 10    | 735 |       |  	
 12    | 735 |  T    |  	
 14    | 735 |       |   5	
 15    | 73  |       |  	
 16->12| 73  |  T    |  	
 14    | 73  |       |   3	
 15    | 7   |       |  	
 16->12| 7   |  T    |  	
 14    | 7   |       |   7	
 15    | 0   |       |  	
 16->12| 0   |  F    |  	
 17    |       return    
Conclusion: The function prints x backwards.

Here is a program that includes this function. You can check how everything works in practice.

6. Given program with errors;     corrected program.