Department of Computer Science
Computer Science 101: Mid Term Exam II (60 minutes)
![]()
Name: NetID:
Lab Section: Date: November 4, 2010
![]()
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.
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 |
7 |
|
|
|
3 |
6 |
|
|
|
4 |
5 |
|
|
|
5 |
8 |
|
|
|
6 |
5 |
|
|
|
7 |
10 |
|
|
|
8 |
6 |
|
|
|
9 |
6 |
|
|
|
10 |
6 |
|
|
|
11 |
5 |
|
|
|
12 |
6 |
|
|
|
13 |
6 |
|
|
|
14 |
10 |
|
|
|
15 |
8 |
|
|
|
16 |
10 |
|
|
|
17 |
9 |
|
|
|
18 |
6 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
1. Write a single Unix command to list all the files in your current directory which begin with the letters pc .
________ls pc*
__or___ls ./pc*____________
2. Write
a single Unix command to copy the file named config located in the directory
~cs101ta/lab8 ,
into your current directory, keeping the name of the file the same.
_________cp ~cs101/lab8/config ./_or_._or__config_or_./config________
3. Assume that a file lab4.c is located in your current directory. Write a single Unix command that renames the file lab4.c as lab4.c.bak .
_____mv lab4.c lab4.c.bak__________________________________
4. The following C program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void main(void)
{
int a = 12;
int b = 10;
int c = 5;
if (c > b)
printf("too large!
");
if (c < a)
printf("too
low! ");
else
printf("greater
than a! ");
}
________too low!_____________________________________________________
5. Write the correct results of evaluating the following C language expressions:
a) 5 /
2 = _______2______________
b) 113 %
2 = _______1______________
c) 3 %
9 = _______3______________
d) 13 / 5 + 7 * 4
= _______30______________
6. 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("%i",B);
7.
Consider the following
C program:
#include <stdio.h>
void main(void)
{
int
x;
scanf("%i",
&x);
switch
(x)
{
case
0:
printf("Too small!\n");
break;
case
1:
case 2:
printf("Just right!\n");
break;
default:
printf("Too large!\n");
}
}
Fill in the blanks below to
re-implement the program using if statements. You may assume
that the user enters only non-negative integers. 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 == 0___________)
printf("Too small!\n");
else
if (___(x == 1) || (x == 2)_____)
printf("Just right!\n");
else
_____ printf("Too
large!\n");_____
}
8. The function find_min returns the minimum value of three variables x, y and z. Choose the answer choice that has the four correct expressions to fill in the four blanks below so that find_min works correctly.
int find_min(int x, int y, int z)
{
int min = x;
if (x <= y)
if(x <= z)
min = _(I)__ ;
else
min
= _(II)__ ;
else
if(y <= z)
min = _(III)_ ;
else
min = _(IV)__ ;
return min;
}
a) I) z , II) x , III) z , IV)
y
b) I) x , II) z , III) y , IV) z
c) I) z , II) x , III) y , IV) z
d) I) z , II) x , III) z , IV) y
9.
The following C
program compiles and runs without errors. Write the output produced by this
program.
#include
<stdio.h>
void
main(void)
{
int a = 0;
int b = 1;
int c = 2;
int d = 3;
if ( (b <= 2) && ((c <
1) || (d >= 4)) )
printf("A ");
else if ( (c != 5) && ((d - a)
< 1) )
printf("B ");
else
printf("C ");
if ( !(d > 2));
printf("D ");
if (a++)
printf("E ");
else
printf("F ");
printf("\n");
}
__________C D F______________________________
10. The following C program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void main(void)
{
int x,y,z;
x = -1;
if (x > 0)
printf("positive! ");
y = 0;
if (y = 1)
printf("equal! ");
else
printf("not equal! ");
z = 3;
switch(z)
{
case 1: printf("one ");
case 2: printf("two ");
case 3: printf("three ");
default: printf("not valid!");
}
printf("\n");
}
________equal!
three not valid!____________________________________
11. Circle the correct C language code for a function that has one input parameter, an array of data type int and the function returns the second element of this array. There is only one correct answer.
a)
foo(int a[])
{
return a[2];
}
b)
int foo(int a[])
{
return a[1];
}
c)
int foo(int a[])
{
return a[2];
}
d)
int foo(int [100])
{
return a[1];
}
12. The following C program compiles and runs
without errors. Write the output produced by the
program.
#include <stdio.h>
int add(int x, int
y)
{
int z = 10;
return x
+ y + z;
}
void
main(void)
{
int x = 100,
y = 10, z = 1;
printf("%i
%i %i\n", add(5,5), x+y, z);
}
______20___________ ________110__________ ________1____________
13. The following C program compiles and runs
without errors. Write the output produced by the
program.
#include <stdio.h>
int fun(int x)
{
if(x += 1)
return x;
else
return 100;
}
void main(void)
{
printf("%i ", fun(-1));
printf("%i ", fun(0));
printf("%i ", fun(1));
}
_______100___________ ______1_____________ ______2_____________
14. The following C program compiles and runs without errors. Write the output produced by the program.
#include
<stdio.h>
void main(void)
{
int Array[3], Xarray[] = {1, 2, 3, 4};
int i;
for(i=0 ; i<3 ; i++)
{
Array[i] = i;
printf("%i ", Array[i]);
}
for(i=0 ; i<3 ; i++)
printf("%i ", Xarray[++i]);
}
______0 1 2 2 4____________________
15. The following C program compiles and runs without errors. Write the output produced by the
program.
#include <stdio.h>
#include <string.h>
void main(void)
{
int i;
char course[12] = {'C','S','1','0','1'};
char college[] = "Engr";
for (i=5;i<12;i++)
course[i]='A';
course[i-6]='\0';
printf("%s\n", course);
if(strcmp(college,
"Engr") == 0)
printf("%s
\n", "yes");
else
printf("%s
\n", "no");
}
________CS101A_____________________________________________________
________yes_____________________________________________________
16. Fill in the blanks to complete the following program so that it reads characters one at a time and then prints "Good" when 'g' is input, and stops when 's' is input:
#include <stdio.h>
void main(void)
{
char c;
do
{
scanf(___”%c”_____________, _____&c_____________);
if (______c == ‘g’________________________________)
printf("Good\n");
}
while (________c != ‘s’_______________________);
}
17. Complete the code for the program by filling in the blanks below. This program finds and locates the maximum element in a two-dimensional array. The outputs should include the value of the maximum element, and the location of this element in the array (row and column). As an example, if
Array[3][4]={{1,2,3,4},{2,3,4,5},{6,4,3,2}}
then the output should be:
max = 6, at row 2, column 0
However, your code should work correctly for any integer values assigned to the array named Array.
#include <stdio.h>
void main(void)
{
int
Array[3][4]={{1,2,3,4},{2,3,4,5},{6,4,3,2}};
int i, j;
int maximum, row, col;
maximum = Array[0][0];
for (i=0;i<3;i++)
for (j=0;j<4;j++)
if(maximum < Array[i][j])
{
_____maximum
= Array[i][j];___________________
_____row
= i;_______________________
_____row
= j;______________________
}
printf("max =
%i, at row %i, column %i\n",maximum, row, col);
}
18. The following C program compiles and runs without errors. Write the output produced by the program.
#include <stdio.h>
void
main(void)
{
int i,j;
for(i
= 1; i < 3; i = i + 1)
{
for(j = i; j <= 3; j = j + 2)
{
printf("(%i,%i) ", i,j);
}
}
}
(_____1_____ , ____1______) (_____1_____ , ___3_______) (_____2_____ , ___2______)