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).
____________________________________________________________
____________________________________________________________
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.
___________________________________________________________
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.
__________________________________________________________
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);
}
_____________________________________________________________________
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++]);
}
__________________________________________________________________
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); _____________________________
for(i=0 ; i< 3 ; ++i) _____________________________
scanf("%i", Array[i]); _____________________________
for(i=0 ; i< 3 ; ++i) _____________________________
sum += Array[i]; _____________________________
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);
}
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________