Department of Computer Science
Computer Science 101: Mid Term Exam II (60 minutes)
![]()
Name: NetID:
Lab Section: Date:
![]()
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.
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, books, notes or
other references during this examination.
Section |
Possible Score |
Score |
Grader |
|
7 |
|
|
|
|
2 |
6 |
|
|
|
3 |
6 |
|
|
|
4 |
6 |
|
|
|
5 |
4 |
|
|
|
6 |
8 |
|
|
|
7 |
10 |
|
|
|
8 |
8 |
|
|
|
9 |
6 |
|
|
|
10 |
8 |
|
|
|
11 |
10 |
|
|
|
12 |
4 |
|
|
|
13 |
4 |
|
|
|
14 |
8 |
|
|
|
15 |
4 |
|
|
|
16 |
6 |
|
|
|
17 |
8 |
|
|
|
18 |
12 |
|
|
|
Total |
125 |
|
|
A
Unix commands:
a) cp
b) mv
c) cd
d) ls
e) more
f)
rm
g) mkdir
Descriptions:
1.
display the contents of a directory _________________
2.
make a new directory _________________
3.
copy a file or files _________________
4.
rename or move a file or directory _________________
5.
delete a file or files _________________
6.
display the contents of a file _________________
7.
change the current directory _________________
2.
Write a single
UNIX command that compiles a C program stored in a file named lab99.c , found in your current directory, into an executable file named lab99. That is, to run this program you will type lab99 and NOT a.out at the UNIX
prompt.
____________________________________________________________________________

__________________________________________________________________
4.
A “block” in C
programming language is defined by using which of the following pairs of
symbols? Circle only one choice.
a) ( )
b) { }
c) [ ]
d) /* */
e) ""
char sport[] = "baseball";
printf(" %i \n", strlen(sport));
________________________________________________________________
#include <stdio.h>
#include <string.h>
void main(void)
{
char password[32] ={'k','i','t','t','y','\0','c','a','t','\0'};
char animal[32] = "dog";
printf("%s \n", password);
printf("%c \n",password[1]);
strcpy(password, "dog");
printf("%s \n", animal);
if (strcmp( password, animal)
> 0)
printf("yes
\n");
else
printf("no
\n");
}
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
#include <stdio.h>
void main(void)
{
int i, j, total;
int scores[5][5]={10,
9, 9, 10, 8,
10, 5, 6, 6, 2,
8, 8, 8, 5, 4,
10, 8 , 5, 4, 5,
6, 2, 6, 7, 8};
double average[5];
/* not shown here is
code that may change values in scores array*/
/* fill in the blanks below with the correct C code */
for(i=0; i < 5 ; _____________________)
{
total = 0;
for(j=0; j < 5 ; ____________________ )
{
______________________________________;
}
average[______] =
__________________________________;
}
for(i=0; i< 5; ++i)
printf("average[%i] =
%lf \n", i, average[i]);
}
8.
The following C program compiles and runs without
errors. Write the output of this program.
#include
<stdio.h>
void
main(void)
{
int a, b = 3, c = 2,
d = 4;
a= (c++) + b;
b++;
d /= 2;
printf(" a = %i , b = %i , c =
%i , d = %i\n", a, b, c, d);
}
a = _____________ , b = _____________ , c = _____________ , d = ______________
9.
The following C program compiles and runs without
errors. Write the output of this program.
#include <stdio.h>
void main(void)
{
int i, j;
for(i = 1; i <=
2; i++)
{
for(j = 1; j < 3; j++)
{
printf("%i ", i * j);
}
}
}
_________________________________________________________________
a) for(i = 0; i < 6; i++)
{
printf("%i", i);
}
b) i = 0;
while (1)
{
printf("%i",++i);
if (i==5)
{
break;
}
}
c) i = 1;
do
{
if (i != 0)
{
printf("%i", i);
}
} while (i < 6);
d) for(i = 0; i < 6; i++)
{
if (i == 0)
{
for(j = 1; j < 6; j++)
{
printf("%i", j);
}
}
}
if (choice == 1)
printf("A\n");
else
if (choice == 2)
printf("B\n");
else
printf("C\n");
12. The following C
program compiles and runs without errors. Write the output of this program.
#include <stdio.h>
void main(void)
{
int x = -1;
int y = -2;
if(((x + y)
>=3) || ((x + y) <= -3))
if(x >
y)
printf("X");
else
printf("Y");
else
printf("Z");
}
___________________________________________________
13. Circle the (one) correct choice of a data type. You are
trying to declare and initialize the variable named pi with the value 3.1415926535 , with the highest precision possible.
________ pi = 3.1415926535;
a) long
b) double
c) float
d) char
14. The following code tries to ask the user to
enter their ID number and displays it on the screen, but the program has two
errors. Write down the line numbers of the lines that contain errors and write
the corrected versions. (Note: the line numbers are not part of the code)
1:
#include <stdio.h>
2: void
main (void)
3: {
4: int idnum;
5: printf
("Please enter your student ID number:");
6: scanf
("%f", &idnum);
7: printf ("Your
ID number is %i \n", &idnum);
8: }
Line#_________ Corrected Line:
__________________________________________________
Line#_________ Corrected Line:
__________________________________________________
a) int plus_ten(int x[ ]);
b) int plus_ten(int
y);
c) int plus_ten(
int);
d) void plus_ten( int);
16. The following C
program compiles and runs without errors. Write the output of this program.
#include <stdio.h>
int fun(int x)
{
x = 7;
return x;
}
void main(void)
{
int x = 5;
int y = 3;
y = fun(x);
printf(" x = %i , y = %i \n", x,
y);
}
x = ______________________ , y = ______________________
17. The following C
program compiles and runs without errors. Write the output of this program.
#include <stdio.h>
int didsumpin(int d, double e[])
{
e[0] = e[0] + 2.0; e[1] = e[1] + 3.0; return 3*d;
}
void main(void)
{ int a = 9; int c; double b[2] = {4.0,3.0}; c = didsumpin(a+1,b);
printf("a = %i b[0] = %lf b[1] = %lf c = %i\n",a ,b[0] ,b[1] ,c);
}
a = _____________ b[0] = _____________ b[1] = _____________ c = _____________
Fill in the two blanks and complete
the C code for the function named ave.
__________
ave( int numbers[], _________ count)
{
double total = 0.0;
int i;
/* your code goes here */
}