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 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 and to write your signature on the line below:
_______________________________________________________________________
(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 |
Score |
Grader |
|
1 |
6 |
|
|
|
2 |
6 |
|
|
|
3 |
5 |
|
|
|
4 |
6 |
|
|
|
5 |
5 |
|
|
|
6 |
10 |
|
|
|
7 |
9 |
|
|
|
8 |
9 |
|
|
|
9 |
6 |
|
|
|
10 |
8 |
|
|
|
11 |
8 |
|
|
|
12 |
9 |
|
|
|
13 |
6 |
|
|
|
14 |
8 |
|
|
|
15 |
12 |
|
|
|
16 |
6 |
|
|
|
17 |
6 |
|
|
|
Total |
125 |
|
|
A
1.
You are in your Unix
home
directory. You have the following subdirectories:
subdirectory: files in it
------------- ------------
dogs lassie
snooppy scooby buck
cats
other
bigbird
jerry
big <empty>
Write a SINGLE Unix
command to perform each of the following actions: (assume your pwd (current
directory) is your home directory)
a) Copy
the file lassie in the dogs directory to a file named
favoritedog
in the home directory.
__________cp dogs/lassie favoritedog_________________
b) Move
the file called bigbird
from the other directory to
the big
directory.
_______mv other/bigbird big__________________________
c) Rename the cats directory cartoon_cats .
________mv cats cartoon_cats________________________
2.
You wite a program in C and save your
code in a file named test.c
and compile
by typing:
gcc test.c
Your program compiles
without errors. You want to use Unix redirection to
read data from the file input.dat when you run your program. Which of the
commands correctly uses Unix redirection while executing your program ? (Circle all correct answers)
a) test.c | input.dat
b) a.out |
input.dat
c)
a.out < input.dat
d) a.out >
input.dat
e) test.c <
input.dat
f) test.c >
input.dat
3. Write a single Unix command that will display details on how to use
the more command.
___________man more__________________________________________
4. The following program is compiled and
executed without error.
Write the
output of the program:
#include <stdio.h>
void main(void)
{
int a = 3, b = 2;
int c, d;
float e = 2.0;
float f;
c =
(a+b)/2;
d =
(a-b)/e;
f =
(a+b)/e;
printf("c = %i \n", c);
printf("d = %i \n", d);
printf("f = %f \n", f);
}
______c = 2______________________________
______d = 0_____________________________
_______f = 2.5___________________________
5. The following program generates and prints a single random, integer
between 10 and 20 (inclusive).
Fill in the blank to complete the program.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
int r;
srand(time(NULL));
/*
generate the random number here */
/* Hint:
Use the rand() function. */
r =
_________10+ (rand() %11)____________________;
printf("%i \n", r);
}
6.
Every room in
Enter
4-digit code: 2302
Floor
number: 2
Area number:
3
Room number:
2
Floor and area numbers range from 1 to 4, while room numbers are from 1 to 30. You may assume that the user always enters a valid 4-digit code. Fill in the following blanks to complete the program:
#include <stdio.h>
void main(void)
{
/* Variables
to store the code, floor, area and room
numbers */
int code;
int floor, area, room;
/* Ask the user for the code and then read the code */
printf( _”Enter code: “________ );
scanf( ___”%i”, &code_______________________ );
/* Calculate the floor, area and room numbers */
/* Hint: You
may need to use the modulus operator */
floor = __code/1000;_________________________
area = ___(code/100)%10;____________________
room = ____code%100;_____________________
/* Print the
results */
printf(“Floor number: %i\n”,floor);
printf(“Area number: %i\n”,area);
printf(“Room number: %i\n”,room);
}