Department of Computer Science
Computer Science 101: Mid Term Exam II (60 minutes)
![]()
Name: NetID:
Lab Section: Date: April 3rd, 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 10 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 |
6 |
|
|
|
3 |
6 |
|
|
|
4 |
6 |
|
|
|
5 |
6 |
|
|
|
6 |
5 |
|
|
|
7 |
5 |
|
|
|
8 |
10 |
|
|
|
9 |
8 |
|
|
|
10 |
8 |
|
|
|
11 |
8 |
|
|
|
12 |
9 |
|
|
|
13 |
8 |
|
|
|
14 |
6 |
|
|
|
15 |
5 |
|
|
|
16 |
10 |
|
|
|
17 |
13 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
Write a single
Unix command to make "Dir2" your current
directory. This command should work regardless of what the current directory is
at the time you are typing it at the Unix prompt.
_________cd ~/Dir1/Dir2 or cd ~netid/Dir1/Dir2__________________________
________pwd______________________________________________________________
________ls
Sdir______________________________________________________________
(Circle only one choice.)
a)
It can be used to copy
a file without deleting the original version.
b)
It cannot be used to move files from one
directory into another.
c)
It can be used to
rename a file.
d)
It launches a program called "my
voicemail".
#include <stdio.h>
#define LARGE 20
#define SMALL 10
void main(void)
{
int value = 7;
if
(value > LARGE)
printf("large\n");
if
(value > SMALL)
printf("med\n");
else
printf("small\n");
}
__________small______________________________________________________
a)
5 / 3 = ________1_____________
b)
5 / 6 = ________0_____________
c)
5 % 3 = ________2_____________
d)
3 % 5 = ________3_____________
e)
6 + 3 / 2 = ________7_____________

int wow[ _3 or blank_ ]
[ _3 not
blank__ ] = { __{1, 9, 1}, { 2 , 2, 3}, {4, 5, 6}________
};
(inner curly braces not required)
#include <stdio.h>
void main(void)
{
int x;
scanf("%i", &x);
switch
(x/32)
{
case
0:
printf("Too
cold!\n");
break;
case
3:
printf("Too
hot!\n");
break;
case
2:
case 1:
printf("Just
right!\n");
break;
default:
printf("Weird
weather!\n");
break;
}
}
Fill in
the blanks below to re-implement the program using if
statements. After you enter your code below the program should be equivalent to
the program above.
#include <stdio.h>
void main(void)
{
int x;
scanf("%i", &x);
if
(_______x/32 == 0__________________)
printf("Too cold!\n");
else
if (_______x/32 == 3______________________________)
printf("Too hot!\n");
else if (____(x/32 == 2) || (x/32 == 1)__________)
printf("Just
right!\n");
else
printf("Weird
weather!\n");
}
if (a < b)
if (c < d)
printf("A");
else
if (a <= c && a != d)
if
(a == c)
printf("B");
else
printf("C");
else
if (c > d)
printf("D");
else
printf("E");
printf("F");
a.
a = 2, b = 5, c = 3, d = 6
____AF___________________________________________________
b.
a = 3, b = 4, c = 2, d
= 1
____DF___________________________________________________
c.
a = 1, b = 1, c = 1, d
= 1
____F___________________________________________________
d. a = 3,
b = 4, c = 5, d = 2
____CF___________________________________________________
a)
Infinite Terminate
for (i =
1; i == i + 1; i = i - 1)
{
printf(" loop \n");
}
b)
Infinite Terminate
for (i =
1; ; i = i + 1)
{
if (i >
10)
break;
}
c)
Infinite Terminate
i = 0;
while(i < 1)
{
printf(" loop \n");
}
i = i +
1;
d)
Infinite Terminate
do{
i = 0;
printf(" loop \n");
i = i +
1;
}while(i <
1);
#include <stdio.h>
void main(void)
{
char
c;
do
{
scanf(_____"%c"________, _______&c___________);
if
(__________c == 'g'_____________)
printf("Good\n");
} while (______c != ' '_________);
}
int funstring
(char a[], char b[])
{
int lena = strlen( __a___ );
int lenb = strlen( __b___ );
if
(lena < lenb)
{
strcpy( ___b_____ , ___a_____);
return __1____ ;
}
else
if (lena > lenb)
{
strcpy(
___a_____ , ____b____);
return __-1____ ;
}
else
return __0____ ;
}
#include <stdio.h>
void main (void)
{
int array[4] = {7, 2, 3, 4};
int i, j, temp;
for
(i = 0; i < 3; i++)
for (j = i + 1; j < 4; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
for
(i = 0; i < 4; i++)
printf ("%i ",
array[i]);
}
________2 3 4 7_________________________
#include <stdio.h>
void func(int y[], int x)
{
if
(x >= 2)
{
y[0]
= x / 2;
y[1]
= x;
}
x = 15;
}
void main(void)
{
int x[2] = {4, 10};
func(x, x[0]);
printf("x[0] = %i , x[1] = %i \n", x[0],
x[1]);
}
x[0] = ____________2__________ , x[1] = _______4_______________
float power;
double resistance;
a)
scanf("%f", &power);
b)
scanf("%lf", resistance);
c)
printf("Resistance is %d (ohms)\n", &resistance);
d)
printf("Resistance is %lf (ohms)\n", resistance);
e)
printf("power = %f \n" power);
#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("yes
\n");
else
printf("no
\n");
}
_______CS101________________________________
_______CS1________________________________
_______S________________________________
_______CS1________________________________
_______no________________________________
For example, once your function step has
been written it should produce the following results when it is called from main :
#include <stdio.h>
double step( double x);
void main(void)
{
printf("%.2lf \n", step(3.5));
printf("%.2lf
\n", step(-7.1));
}
which would display the result:
1.00
0.00
Of course your code should work correctly
for any values and not just for 3.5 and -7.1 as shown above .
Write your code for the function step below.
Do NOT write the code for main.
Do NOT include comments in your
code.
Do NOT use the printf
or scanf function in your code. This will result in a
score of zero.
double step( double x)
{
if (x >= 0.0)
return 1.0;
else
return 0.0;
}
or
double step( double x)
{
if (x >= 0.0)
return 1.0;
return 0.0;
}
or
double step( double x)
{
if (x >= 0.0)
return 1.0;
if (x < 0.0)
return 0.0;
}
or
double step( double x)
{
return x >=0 ;
}
or using the conditional
operator ? :
double step( double x)
{
return (x >=0) ? 1.0 : 0.0 ;
}