Department of Computer Science
Computer Science 101: Midterm Exam II (60 minutes)
![]()
Name: NetID:
Lab Section: Date: April 7, 2011
![]()
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 13 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 |
|
10 |
|
|
|
|
2 |
4 |
|
|
|
3 |
7 |
|
|
|
4 |
6 |
|
|
|
5 |
8 |
|
|
|
6 |
8 |
|
|
|
7 |
8 |
|
|
|
8 |
6 |
|
|
|
9 |
6 |
|
|
|
10 |
5 |
|
|
|
11 |
8 |
|
|
|
12 |
5 |
|
|
|
13 |
9 |
|
|
|
14 |
5 |
|
|
|
15 |
9 |
|
|
|
16 |
5 |
|
|
|
17 |
8 |
|
|
|
18 |
8 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
subdirectory: files in it
------------- ------------
source intro.c test.c test.m
mp1 find_h.m main.m
mp2 1cs101.c Xcs121.c cs.c input.dat
a) Write a single UNIX command to create another subdirectory
with the name backup.
________________________________________________________________________
b) Write a single UNIX
command to make backup your current directory.
_______________________________________________________________________
c) Assume that you
are in your backup directory. Write a single UNIX command to copy
all the files in source subdirectory to backup directory. (Hint: You
may use the wildcard "*").
______________________________________________________________________
d) Assume that you are in your home directory. Write a single UNIX
command to move all files in the mp2 directory that have an extension
(suffix) of .c into the source directory.
_______________________________________________________________________
Unix Prompt>
_______________________________________________
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 _________________
#include
<stdio.h>
int changeVariables(int x, int y)
{
y=x;
return
y;
}
void
main(void)
{
int x = 4;
int y = 1;
y=changeVariables(y,x);
printf("y = %i \n", y);
}
y = _________________________
5.
The following C program compiles and
runs without errors. Write the output of this program.
#include <stdio.h>
int func1(int
z)
{
z = 2*z;
return z;
}
int func2(int
x)
{
x = x+4;
return x;
}
void
main(void)
{
int x = 2;
int y = 3;
int r = 5;
y = func1(func2(r));
r = func2(x);
printf(" x = %i , y = %i \n",
x, y);
}
x = _________________________ y =
_________________________
6.
The following C program compiles and
runs without errors. Write the output of this program.
#include <stdio.h>
char build1(char
z[])
{
z[0] = 'L';
}
char build2(char
m[])
{
m[7] = 'n';
}
void
main(void)
{
char x[64] = " ed";
char y[64]="Zeppeli
";
build1(x);
build2(y);
printf ("%s
%s\n", x,y);
}
_________________________________________________________
7.
The following C program compiles and
runs without errors. Write the output of this program.
#include
<stdio.h>
int fun(int x)
{
if(x%3)
return x;
else
return x%3;
}
void main(void)
{
printf("%i ",fun(4));
printf("%i ",fun(3));
}
___________________________ ______________________________
8. Circle
each of the following loops that are infinite. There may be more than one
correct answer.
a)
int a=5, b=6, x=0;
while (b>a)
{
a = x+rand()%2;
b = rand()%3;
}
b)
int a, b=6;
for(a=5; ; --b)
{
if(b < a)
break;
++b;
}
c)
int a=5, b=6;
do
{
if(b < a)
break;
++b;
}while(a > b);
9. Write
the final value of the variable named
a
after
running this code fragment.
int i, k, a=1;
for (i=1; i<=6; i=i+1)
for (k=i; k<3; k=k+1)
a=a*2;
____________________________
10. What
is the smallest integer, replacing the symbol X
below, in the “for” loop so that the “while” and “for” loops (Program 1 and
Program 2 shown below) produce the same final value for the variable named output
?
/* Program 1 using a while loop */ /* Program 2 using a for loop */
int i = 12, output=1;
int k, output = -1;
while( i >= 5)
for( k=12; k <= X ; k++)
{ {
output++; output=output+2;
i=i-2;
}
}
____________________________
11.
The following program compiles and
runs without errors. Write the output of this program.
#include
<stdio.h>
void main(void)
{
int A[] = {1 , 3 , 5, 7};
int i;
for(i=0
; i<3 ; ++i)
printf(" %i ", A[i++]);
}
_________________________________________________________
12.
The
following program compiles and runs without errors. Circle the
output this program
produces. There is only
one correct answer.
#include <stdio.h>
#define
A 7
void main(void)
{
int
i, c;
int a[A] = { 1, 2, 3, 4, 5, 6, 7};
for(i=0; i < A/2; i++)
{
c = a[i];
a[i] = a[ A- (i+1)];
a[A-(i+1)] = c;
}
for(i=0; i <A; i++)
printf(" %i ", a[i]);
}
a) 1 2
3 4 5
6 7
b) 7 6
5 4 3
2 1
c) 1 2
3 4 1
2 3
d) 5 6
7 4 5
6 7
e) 7 7 7 7 7 7 7
13.
The following program compiles and
runs without errors. Write the output of this program.
#include <stdio.h>
#include <string.h>
void main(void)
{
char band[32] = "GreenDay";
char member[32] = {'B','I','L','\0','I','E','\0'};
printf("%s
\n", member);
printf("%c
\n", member[0]);
strcpy(member,
band);
printf("%s
\n", band);
}
_______________________________________
_______________________________________
_______________________________________
14.
The following program compiles and runs
without errors. Circle the count of the number of times the letter J is printed. There is only
one correct answer.
#include<stdio.h>
void main(void)
{
int i,j;
for(i=3;
i>0; i--)
{
j=0;
while(j<=i)
{
printf("J");
j++;
}
}
}
a) 4
b) 5
c) 6
d) 7
e) 8
f)
9
15.
The following C program compiles and
runs without errors. Write the output of this program.
#include <stdio.h>
void main(void)
{
int x, a = 3, b = 2, c = 1 ;
if(a > 1)
c = c + b
* a;
printf("%i ", c);
if(( b <
4) && (c <= (a + 2)))
printf("X
");
x = 0;
if(x == -1)
printf("T
");
else if(x != 0)
printf("U
");
else
printf("Y
");
if(x == -1)
printf("Z
");
if(x
== 0)
printf("W
");
}
_________________________________________________________________________
16.
Circle the correct way to print out
the value of the integer variable named B by calling the printf function. There is
only one correct answer.
a) printf(B);
b) printf("%i",&B);
c) printf("%i",B);
d) printf("B");
17.
The following C program compiles and
runs without errors. Write the output of this program.
#include <stdio.h>
void main(void)
{
int i = 8;
if((i/3 == 0) || (i/3
== 2))
{
switch(i/2)
{
case 3: printf("L
");
case 2: printf("M
");
case 1: printf("N
");
default: printf("P
" );
}
if(i == 2)
printf("Q
" );
else
printf("R
" );
}
else
{
switch(i/2)
{
case 3: printf("U
");
case 2: printf("V
");
case 1: printf("W
");
default: printf("X
" );
}
if(i == 2)
printf("Q
" );
else
printf("R
" );
}
}
_______________________________________________________________
18.
The following C program compiles and
runs without errors. Write the output of this program.
#include <stdio.h>
void
main(void)
{
int no =
5, v = - 4;
int a =
3;
if((a >= 5) && (a <=
2))
printf("A
");
if(a != 8)
printf("B
");
if((a <= 1) || (a >= 5))
printf("C
");
else
printf("D
");
switch ( no-1 )
{
case 1: v = v * 2;
case 2: v = v - 1;
break;
case 3: v = v + 10;
default: printf("%i " , no);
}
printf("%i \n", v);
}
_______________________________________________________________