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 8, 2010
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 12 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.
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 |
|
Tony |
|
|
2 |
6 |
|
Tony |
|
3 |
6 |
|
Tony |
|
4 |
6 |
|
Grader1 |
|
5 |
5 |
|
Grader1 |
|
6 |
6 |
|
Piyush |
|
7 |
6 |
|
Piyush |
|
8 |
6 |
|
Grader2 |
|
9 |
6 |
|
Grader2 |
|
10 |
6 |
|
Lu |
|
11 |
8 |
|
Lu |
|
12 |
9 |
|
Grader3 |
|
13 |
8 |
|
Grader3 |
|
14 |
6 |
|
Reza |
|
15 |
8 |
|
Reza |
|
16 |
9 |
|
Andrew |
|
17 |
8 |
|
Andrew |
|
18 |
10 |
|
Rajesh |
|
Total |
125 |
|
Rajesh |
|
|
|
Exam Scoreà |
|
A
Use the figure below for the next three questions. Assume that your home directory is pictured as in the figure below. Thus, your home directory contains files named data2.txt and test.c . Your home directory also contains the directories named lab8 and fun, the directory lab8 contains the file named data.txt , the directory fun contains files named lab8.c and data3.txt and a directory named lab9.

1. Assume that your current directory is lab8. Write a single Unix command to copy all the files located in the directory named fun into the lab8 directory.
______cp ../fun/* ._____or___cp ~/fun/* ~/lab8____________________
2. Assume that your current directory is lab8. Write a single Unix command to move only the file named data2.txt located in your home directory into the lab8 directory.
_______mv ../data2.txt .___or____mv ~/data2.txt .____________________
3. Assume that your current directory is your home directory. Write a single Unix command to move the directory named lab9 so that it becomes an immediate subdirectory of your home directory.
____mv fun/lab9 ._______________________________________________
4. The following program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void main(void)
{
int value = 12;
int max = 10;
int min = 5;
if (value > max )
printf("too large! ");
if (value < min)
printf("too low! ");
else
printf("greater than min! ");
}
______too large! greater than min! _______________________________
5. Write the correct results of evaluating the following C language expressions:
a) 9 / 2 = _____4________________
b) 9 % 2 = _____1________________
c) 2 / 9 = _____0________________
d) 2 % 9 = _____2________________
e) 13 / 5 + 7 * 4 = _____30________________
6. Fill in the blanks to correctly print the values asked for in the comments.
#include <stdio.h>
void main(void)
{
int x = 8;
float y = 10.0;
char z = 'R';
/* print the value of x on the screen */
printf("%__i_________ \n", ____x___________ );
/* print the value of y on the screen */
printf("%___f________ \n", _____y__________);
/* print the value of z on the screen */
printf("%____c_______ \n", ______z_________);
}
7. Circle the correct choice of commands you would type at the UNIX prompt to compile and run the C program located in the file myExam.c . The program uses the math library. Circle only one answer choice.
a) gcc myExam.c –include <math.h>
a.out
b) gcc myExam.c –lm –o exam
./exam.out
c) gcc myExam.c –o math
.\math
d) gcc myExam.c -lm
./a.out
8. The following C program compiles and runs without errors. Circle the correct output produced by this program. Circle only one answer choice.
#include <stdio.h>
void main(void)
{
int x = 4;
if (x%3 == 1)
{
switch(x/2)
{
case 3: printf("A ");
break;
case 2: printf("B ");
break;
case 1: printf("C ");
break;
default: x++;
}
}
if (x%2 == 0)
{
switch(x/2)
{
case 3: printf("X ");
break;
case 2: printf("Y ");
break;
case 1: printf("Z ");
break;
default: x++;
}
if (x < 5)
printf("P " );
else
printf("Q " );
}
}
a) B Y P
b) B
c) C Z P
d) C Y Q
9. 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. You may assume that the variables, a, b, c and d are declared as integer data type variables.
if (a < b)
if (c >= d)
printf("A");
else
if (a <= c && a != d)
if (a == c)
printf("B");
else
printf("C");
else
if (a < d)
printf("D");
else
printf("E");
else
printf("F");
a = 2, b = 5, c = 3, d = 3
____________A_________________________________________________
a
= 3, b = 4, c = 2, d = 5
____________D_________________________________________________
a = 1, b = 1, c = 1, d = 1
____________F_________________________________________________
10. The following program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void main(void)
{
int x,y,z;
x = -1;
if (x > 0)
printf("positive! ");
y = 0;
if (y = 1)
printf("equal! ");
else
printf("not equal! ");
z = 3;
switch(z)
{
case 1: printf("one ");
case 2: printf("two ");
case 3: printf("three ");
default: printf("not valid!");
}
printf("\n");
}
______equal! three not valid!_______________________________________________________
11. The following program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void main(void)
{
int i,j,k;
for(i = 1; i <= 2; i++)
{
for(j = 1; j < 3; j++)
{
for(k = 1; k < 3; k++)
printf("%i ", i*j*k);
}
}
}
____1____ ____2____ ____2____ ____4____ ____2____ ____4____ ____4____ ____8____
12. Which of the following code fragments assign the value 1 to every element of the array named Array? Circle each correct answer. There may be more than one correct answer.
a)
int Array[10][10] = {1};
b)
int Array[10][10];
int i,j;
for(i=0; i<10 ;i++)
for(j=i; j<10 ;j++)
Array[j][i] = 1;
c)
int Array[10] = {1};
int i;
for (i=1; i < 10; i++)
Array[i] = Array[i - 1];
13. The following program compiles and runs without errors. Write the output of this program.
#include <stdio.h>
void main(void)
{
int Array[4], Xarray[] = {1, 2, 3, 4};
int i, j;
for(i=3 ; i > -1 ; --i)
{
j=i;
Array[i] = ++j;
}
for(i=0 ; i < 4 ; ++i)
{
printf("%i ", Array[i]);
printf("%i ", Xarray[++i]);
}
}
_____1______ _____2______ ____3_______ _____4______
14. 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 fruit1[16] = {'P', 'E', 'A', 'R', '\0'};
char fruit2[16] = {'A', 'P', 'P', '\0', 'E', '\0'};
char fruit3[16] = {'O', 'R', 'A', '\0', 'G', 'E', '\0'};
printf("%s \n", fruit1);
printf("%s \n", fruit2);
printf("%s \n", fruit3);
int i;
for(i=0;i<4;i++)
{
fruit2[i]= fruit1[i];
fruit3[i]= fruit1[i];
}
printf("%s \n", fruit1);
printf("%s \n", fruit2);
printf("%s \n", fruit3);
}
__________PEAR___________________________________________________
__________APP___________________________________________________
__________ORA___________________________________________________
__________PEAR___________________________________________________
__________PEARE___________________________________________________
__________PEARGE___________________________________________________
15. Complete the following C program by filling in the blanks below. When your program runs it should produce the output table exactly as shown below.
3 2 1
6 5 4
#include <stdio.h>
void main(void)
{
int mat[2][3] = {1, 2, 3, 4, 5, 6};
int row, col;
/* This code prints the table as shown above */
for(__row=0_______;__row < 2_______;__++row_______)
for(_col = 2________;__col >= 0_______;__--col_______)
{
printf("_%i ______", mat[row][col]);
if( col == __0_____)
printf("\n");
}
}
16. The following C program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
int coolest(int i)
{
int x = 10;
return x + i;
}
void main(void)
{
int i = 39 , x = 1;
int z = coolest(i);
printf("%i %i %i \n", i , x , z);
}
______39__________ ________1________ ______49__________
17. The following C program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
int fun(int x)
{
if(x%2)
{
if(x += 1)
return x;
else
return x/2;
}
return x-1;
}
void main(void)
{
printf("%i ",fun(3));
printf("%i ",fun(4));
}
______4__________ _______3_________
18. The following C program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void fun(int x[])
{
x[0] = x[0] + 5;
}
void main(void)
{
int y[2] = {3,8};
fun(y);
printf(" y[0] = %i , y[1] = %i \n", y[0], y[1]);
}
__y[0] = 8____ __y[1] = 8____