Department of Computer Science
University of Illinois at Urbana-Champaign
Computer Science 101: Mid Term Exam II (60 minutes)
![]()
Name: NetID:
Lab Section: Date: November 1, 2007
![]()
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.
Section |
Possible Score |
Deduction |
Grader |
|
6 |
|
|
|
|
2 |
5 |
|
|
|
3 |
5 |
|
|
|
4 |
5 |
|
|
|
5 |
8 |
|
|
|
6 |
8 |
|
|
|
7 |
5 |
|
|
|
8 |
7 |
|
|
|
9 |
6 |
|
|
|
10 |
6 |
|
|
|
11 |
8 |
|
|
|
12 |
10 |
|
|
|
13 |
6 |
|
|
|
14 |
14 |
|
|
|
15 |
14 |
|
|
|
16 |
12 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
1. Answer the following questions by filling in the blanks.
a) When you use the Unix ls command to list the contents of a directory the output may not fit on one full terminal screen. Since the screen scrolls you may not see the first part of the output. Fill in the blank line to solve this problem:
ls -la __|____ more
b) Fill in the blank to send the output from the ls command to the file “screen.txt”.
ls -la __>____ screen.txt
2.
Assume that you are in your home directory which
contains the file lab9.c Write Unix commands
to create a subdirectory named lab9 and
next, without changing directories, copy the file named lab9.c located in your home directory into the lab9 directory (do not copy any other files).
_________mkdir lab9___________________________________________________
_________cp lab9.c lab9 ___________________________________________________
3. The following program is saved in a file named test.c,
#include <stdio.h>
#include <math.h>
void main(void)
{
double x;
printf("Enter a number:");
scanf("%lf", &x);
printf("sin(%lf) = %lf \n",x,
sin(x));
}
Write a single command to
compile the code in the file test.c, so that to run your program at the Unix prompt you
would type:
testing
instead of a.out.
______gcc test.c –lm -o testing_______________________________
4. Write a single Unix command to move the files named file1, file2, file3 located in your current directory into a subdirectory named dirname. You may assume that these three files are the only files located in your current directory and that dirname already exists.
______mv file1 file2 file3 dirname___OR ____________mv * dirname _____________
5. Which
of the following code fragments assign the value 10 to each 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; i<10 ;i++)
for(j=0; j<10 ;j++)
Array[j][i] = 10;
b)
int Array[10][10] = {10};
c)
int Array[10][10];
strcpy(Array,10);
d)
int Array[10][10];
int i,j;
for(i=0; i<10 ; i++)
{
Array[0][i] = 10;
for(j=1; j<10 ; j++)
Array[j][i] = Array[j-1][i];
}
6. The
following program compiles and runs without errors. Write the output of this
program.
#include <stdio.h>
void main(void)
{
printf("%i ", 3/4);
printf("%i ", 3%4);
printf("%i ",3/4*4);
printf("%i ",3+3*3);
}
_______0 3 0 12______________
7. The
following program compiles and runs without errors. Write the output of this
program:
#include <stdio.h>
void main(void)
{
int
Array[3], Xarray[] = {1 , 2 , 3};
int i;
for(i=0 ; i<3 ; ++i)
{
Array[i] = i;
printf("%i ", Array[i]);
}
for(i=0 ; i<3 ; ++i)
printf("%i ", Xarray[i++]);
}
_______0 1 2 1 3_____________
8. There is an error on three different lines of code in the
following program. Write the corrected lines in the blanks provided. Only three lines need be rewritten. There is
one syntax error, one run time error and one logical error.
The
program below should read 3 integers, compute and display the average of the 3
integers.
For example, if the 3 integers were 2, 3 and 5 then the program would print the
average as 3.33.
#include <stdio.h>
void main(void)
{
int i, sum=0; _____________________________
int
Array(3); _____[3];____________________
(only change shown)
for(i=0 ; i< 3 ; ++i) _____________________________
scanf("%i", Array[i]); ____&Array[i]__________
(only change shown)
for(i=0 ; i< 3 ; ++i) _____________________________
sum += Array[i]; ________sum/3.0__OR (float)sum/3_OR
variations__ (only change shown)
printf("%.2f \n",sum/3); _____________________________
}
9. The following C program compiles and runs without errors.
Write
the output of this program when it is executed.
#include <stdio.h>
void
main(void)
{
int i = 1, n = 3, sum1 = 0,
sum2 = 0,sum3 = 0;
while(i <= n)
{
sum1 = sum1 + i;
i =
i + 1;
}
printf("%i %i \n",i,sum1);
for(i = 1; i <= n; i = i+1)
sum2 = sum2 + i;
printf("%i %i \n",i,sum2);
i = 5;
do{
sum3 = sum3 + i;
i =
i + 1;
}while(i <= n);
printf("%i %i \n",i,sum3);
}
___________4 6__________________________________________________________
___________4 6__________________________________________________________
___________6 5__________________________________________________________
10. The
following C program compiles and runs without errors.
Write the output of this program when it is executed.
#include <stdio.h>
void main(void)
{
int x = 5, y = 5;
do
x = x + x;
while(x < 5);
while(y < 5)
y = y + y;
printf("%i %i \n", x , y);
}
_________10 5_______________________________________________
11.
Assume we have written a function named my_prod as defined
below:
int my_prod(int x, int y)
{
return x + y;
}
Circle each of the following that are valid prototypes of the function my_prod. There may
be more than one correct answer.
a) void my_prod(int x, int y)
b) int my_prod(int a, int b);
c) int my_prod(int x, int y) { return x + y };
d) int my_prod(int x, int y);
12. The following C program compiles and runs without errors.
Write
the output of this program when it is executed.
#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("A
\n");
else
printf("B
\n");
}
_______CS101______________________________________
_______CS1______________________________________
_______S_______________________________________
_______CS1______________________________________
______B________________________________________
13. The following C program compiles and runs without errors.
Write the output of this program
when it is executed.
#include <stdio.h>
void set_elements(int x, double
list[])
{
list[0]
= x;
x = 3*x;
list[1]
= x;
}
void
main(void)
{
double
price[2] = {7.0, 9.0};
int x = 2;
set_elements(2, price);
printf("x = %i \n", x);
printf("price[0]=%.2lf \n",
price[0]);
printf("price[1]=%.2lf \n",
price[1]);
}
_________x =2____________________________________________
_________price[0] = 2.00____________________________________________
_________price[1] = 6.00____________________________________________
14. Complete the code for the function named replace_max by filling in the blanks below. This function finds and replaces each occurrence of the maximum value of an array named list with a given number, x. The function has three input arguments: the actual array, the number of elements in the array, and the given value for replacement. The function does not return a value. You may assume that list is non-empty. To see how the function replace_max should work consider the following example:
#include <stdio.h>
void replace_max( int
list[], int count, int x);
void main(void)
{
int i ,
total , list[4] = {3 , -2 , 1 , 3};
replace_max(
list , 4 , -1 );
for(i=0; i<4
; i=i+1)
printf("
%i ", list[i]);
printf("\n");
}
displays the values:
-1 -2 1 -1
however
your code should enable replace_max
to work for any non-empty array of integers.
___void________ replace_max( int list[], int
count, int x)
{
int max, i , total = 0;
/* initialize max */
max =
___list[0]_________________________ ;
/* find maximum value of the
array and assign to max */
for(i = 0; i<count ; ++i)
if
( __list[i] > max_____ )
max = ____list[i]__________ ;
/* replace the elements of
array equal to max with x */
for(i = 0; i<count ; ++i)
if
( ____max == list[i]_____ )
_____list[i]_____ = _x____ ;
}
15. Convert the flowchart shown below to C code by writing a switch statement. You should NOT
use any if , if-else or nested ifs in your code. You may assume that choice is an integer
variable. Hint: you may need to use the break statement.

switch(choice)
{
case 1: N = 16;
break;
case 2: N =128;
break;
case 3: N = 1024;
break;
default: N = -1;
break; /* the last break is always optional */
}
16. Write a
complete function named
dist_to_origin. This function has two input parameters x and y,
both of datatype double. The function dist_to_origin
returns the value
.
For example, to compute the distance
to the orgin for the point (3.0 ,
4.0) you would call your function as follows:
#include <stdio.h>
#include <math.h>
double dist_to_origin( double x, double y);
void main(void)
{
printf("%lf \n", dist_to_origin(3.0,4.0));
}
Which would display the result:
5.000000
Of course your code should work correctly
for any point and not just (3.0,4.0). Write your code
for the function dist_to_origin below. Hint: You may want to use the sqrt function. Do
NOT write the code for main. Do NOT include comments in your code.
double dist_to_origin(
double x , double y)
{
return sqrt( x*x + y*y );
}