Department of Computer Science
Computer Science 101: Mid Term Exam II (60 minutes)
![]()
Name: NetID:
Lab Section: Date: November 6th, 2008
![]()
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 |
|
8 |
|
|
|
|
2 |
8 |
|
|
|
3 |
10 |
|
|
|
4 |
6 |
|
|
|
5 |
8 |
|
|
|
6 |
6 |
|
|
|
7 |
6 |
|
|
|
8 |
6 |
|
|
|
9 |
6 |
|
|
|
10 |
19 |
|
|
|
11 |
10 |
|
|
|
12 |
8 |
|
|
|
13 |
5 |
|
|
|
14 |
5 |
|
|
|
15 |
14 |
|
|
|
Total |
125 |
|
|
|
|
|
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 a file named cat.txt, and also contains the directories
named lab9 and workspace; the
directory lab9
contains the files named lab9.c and a.out; the
directory workspace contains a directory named lab10.

1.
Assume that you are in
the lab10 directory. Write a single
Unix command to change the directory to your home directory.
_________cd______________________________________________________
2.
Assume that you are in
your home directory. Write a single
Unix command to rename the file named cat.txt
to the file named dog.txt
.
________mv cat.txt dog.txt___________________________________________________
3.
Assume that you are in
your home directory. Write a single
Unix command to copy all the contents of the directory
named lab9 into the lab10
directory.
_____cp lab9/* workspace/lab10______________________________________________
4.
The following C
program compiles and runs without errors. Write the output produced by this
program.
#include <stdio.h>
void main(void)
{
/* printf("This is a comment."); */
printf("That was a comment.\n");
}
_______ That was a comment.________________________________________
5.
The following C
program compiles and runs without errors. Write the output produced by this
program.
#include <stdio.h>
void main(void)
{
int x = 5;
int y = 4;
int z = 0;
if ((x < 1) || ( y > 3))
printf("H ");
if ((x > 4) && (y <
3))
printf("I ");
else
printf("J ");
if ( !(x < 2))
printf("K
");
if (z)
printf("L
");
else
printf("M
");
printf("\n");
}
______H J K M__________________________________________________
6.
The following C
program compiles and runs without errors. Write the output produced by this
program.
#include <stdio.h>
void main(void)
{
int i = 2;
if (i/3 == 0)
{
switch(i/2)
{
case 3: printf("A ");
case 2:
printf("B ");
case
1: printf("C ");
default:
printf("D " );
}
if (i ==
2)
printf("E " );
else
printf("F
" );
}
else
{
switch(i/2)
{
case 3: printf("U ");
case 2:
printf("V ");
case 1: printf("W
");
default: printf("X " );
}
if (i ==
2)
printf("Y " );
else
printf("Z
" );
}
}
_________C D E______________________________________________________
7. The following program compiles and runs without errors:
#include <stdio.h>
void main(void)
{
int
c = 7;
char n = 'c';
int
i = 1;
printf("\n i %i
n %c c %i n \n",i,n,c);
}
Which of the following is
the output obtained after running this program?
Circle the correct answer.
(There is only one correct
answer below.)
a)
Hello, world!
b)
1 1 c c 7 7 c
c) i %i n
%c c %i n 1 c 7
d) \n i %i n %c c %i
n \n 1 c 7

e)
i 1 n c c
7 n
8.
Which of the following
is a correct way of reading an integer value from the keyboard and storing the
value in a variable named c (assuming c
has been previously declared as having type integer)? Circle the correct answer. (There is only one correct answer below.)
a) scanf("%i",c);
b) scanf("&i",c);
c) scanf("&i",%c);
d) scanf("%i",&c);
e) scanf(c,"%i");
9.
Assume that a C
program contains the following line, where x, y and z are previously
declared integer variables:
x * z = y;
Circle the correct answer.
(There is only one correct
answer below.)
a)
The above is
a correct C statement that assigns the value of y to the result of multiplying x and z.
b)
The above is incorrect,
since a compound expression cannot be specified in the left-hand side of an
assignment in C.
c)
The above is a correct
C statement that assigns to y the result of multiplying x and z.
d)
The above is a correct
C statement that multiplies x with the result of assigning z to y.
10. Complete the following C program by filling in the blanks
below. When your program runs it should produce the output exactly as shown
below.
![]()
![]()
#include
<stdio.h>
void main(void)
{
int mat[ 3 ] [ _2__ ] = { ___2, 3, 4, 5, 6, 7_______ };
int i, j;
/* This code just
prints the matrix */
for(i = __0_____; ___i
< 3________ ; __++i_____)
for(j = ___0____;
___j < 2________ ; __++j_____)
{
printf("_%i
______", ___mat[i][j]____________);
if( j == ____1___________)
printf("\n");
}
}
11. 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 temp[32];
char vegetable1[32] =
{'P', 'E', 'A', 'S', '\0'};
char vegetable2[32] = {'C', 'A', 'R', '\0',
'O', 'T', 'S', '\0'};
printf("%s \n", vegetable1);
printf("%s
\n", vegetable2);
vegetable2[3] = 'R';
strcpy(temp, vegetable2);
strcpy(vegetable2, vegetable1);
strcpy(vegetable1, temp);
printf("%s \n", vegetable1);
printf("%s \n", vegetable2);
if (strcmp(
vegetable1, vegetable2) == 0)
printf("yes
\n");
else
printf("no
\n");
}
______PEAS_________________________________
______CAR_________________________________
______CARROTS_________________________________
______PEAS_________________________________
______no_________________________________
12.
Circle each C loop below that terminates, i.e., does not repeat infinitely.
Assume that n is a variable of type int.

a)
for (n = 1; n < 40; n *= 2)
if
(n == 37)
break;
b)
for (n = 100; ; n = n + 1)
if
(n % 500 == 0)
break;
c)
n = 5;
while (n != 0)
{
n = n - 2;
if (n < -4)
n = 5;
}
d)
n = 1;
do
{
printf("%i ", n);
n = 5;
} while (n >= 4);
13. The following C program compiles and runs without errors.
Write the output that it produces.
#include <stdio.h>
void main(void)
{
int j, A[5];
A[0]=0; A[1]=1;
for (j=2; j<5; j++)
A[j]
= A[j-1]+A[j-2];
for (j=4; j>=0; j--)
printf("%i
", A[j]);
}
_______3 2 1 1 0_______________________________________________________________
14. Circle each answer
choice whose code fragments produce the output
1 4 9 (if included in a complete C program which compiles and runs
without errors). Assume that n and k
are variables of type int.



a)
for (n = 1; n <= 3; n++)
printf("%i ", n*n);
b)
k = 0;
for (n = 0; n <= 2; n++)
{
k = k+2*n+1;
printf("%i ", k);
}
c)
for (n = 1, k = 0; n <= 3; n = n + 2)
{
k = k + n;
printf("%i
", k);
}
d)
for (n = 1; n <= 3; n++)
{
for (k = 0; ; k++)
if (k*n == n*n*n)
break;
printf("%i ", k);
}

e)
k = 0;
for (n = 2; n <= 4; ++k, ++n)
printf("%i ", k*n+1);
15. Complete the following C program by filling in the blanks. This program should prompt the user to enter a positive integer and next read that integer into the variable named h. Then the program should print out a triangle of height h. For example, if h equals 6 then your program should print a triangle in the following shape:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Of course your code should work correctly for any height not only for the one shown above.
#include <stdio.h>
void main(void)
{
int h, row, col;
printf("Enter the height
of the triangle: ");
scanf(" __%i_______", ____&h_______);
for(row=1; row <= __h______ ;
++row)
{
for(col= __1____ ; ____col <= row____________ ; ++col)
printf("%i ", _____col______________);
printf( __"\n"________ );
}
}