CS101 Midterm Exam
August 5th, 2011
DO NOT START UNTIL INSTRUCTED TO DO SO. YOU WILL LOSE POINTS IF YOU START WORKING ON THE TEST BEFORE WE TELL YOU. THIS IS A 120 MINUTE EXAM.
|
Name: |
|
Discussion Section: |
|
TA: |
|
FORM A |
· Use only a #2 pencil; otherwise, your exam won't be graded.
· Bubble in the Form Letter above!!! Bubble in your name and your NetID!!!
· Write your TA's name (of the section you attend) in the instructor slot and your section number in the section slot. Your section number is in the table provided at the bottom of this page.
· The exam consists of 17 pages (including this page). Check that your copy is complete, and ask for a replacement if it is not. Make sure you look at all the pages.
· You will have 120 minutes to FILL IN THE SCANTRON for the exam. You may not use any books, notes, or other references for this exam.
· NO QUESTIONS WILL BE ANSWERED DURING THE EXAM.
· At the end of the exam, stop writing and put down your pencil when time is called. Hand in both the scantron and all the pages of the exam booklet.
· NO CALCULATORS OR ELECTRONIC DEVICES OF ANY KIND ARE ALLOWED. TURN OFF CELL PHONES AND PAGERS. IF A CELL PHONE RINGS, DO NOT ANSWER IT!!
|
Time |
Tuesday-Thursday |
Monday-Wednesday |
|
9:00 am |
101-Arjun Rao |
|
|
10:00 am |
|
102-Steve Henry |
Note: Always choose one answer, the most correct choice of the answers offered.
1. The following C program compiles and runs without errors. What is the output produced by this program?
#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);
}
a) y = 3
b) y = 5
c) y = 1
d) y = 4
2. What is the correct way to print out the value of the integer variable named B by calling the printf function?
a) printf(B);
b) printf("%i",&B);
c) printf("%i",b);
d) printf("%i",B);
3. 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 for any integer values assigned to x, y and z.
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
4. Which of the following code fragments will assign to the variable named grade the value 'P' if score is 4 or 5 and 'F' if the score is any other value? You may assume that score has been declared as a variable of datatype int and grade has been declared as a variable of datatype char .
a)
switch
(score)
{
case 4:
case 5:
grade
= 'P';
break;
default:
grade
= 'F';
}
b)
switch
(score)
{
case 4,5:
grade
= 'P';
break;
default:
grade
= 'F';
}
c)
switch
(score)
{
case 4:
case 5:
grade
= 'P';
default:
grade
= 'F';
break;
}
d)
switch
(score)
{
case 5:
case 6:
grade
= 'P';
break;
default:
grade
= 'F';
break;
}
5. Which of the following code fragments does not assign the value 2 to every element of the array named Array?
a)
int Array[10][10];
int i,j;
for (i = 0, j = 0; i < 10 && j < 10; i++, j++)
Array[i][j]=2;
b)
int Array[10];
int i=0;
Array[0] = 2;
for (i=1; i < 10; i++)
Array[i] = Array[i - 1];
c)
int Array[10];
int i = 0;
for (i=0; i < 10; i++)
{
Array[i] = 1;
Array[i]++;
}
d)
int Array[10][10];
int i,j;
for(i=0; i<10 ; i++)
{
Array[0][i] = 2;
for(j=1; j<10 ; j++)
Array[j][i] = Array[j-1][i];
}
6. Which is the only true statement about the Unix command "mv" listed below?
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".
7. Assume that in your home directory there is a directory named "Dir1" and inside "Dir1" there is a directory named "Dir2".
Which Unix command makes "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.
a) cd ~/Dir1
b) cd ~/Dir1..
c) cd ~/Dir1/Dir2
d) cd ~/Dir2/Dir1
8. What is the result of evaluating the following C language expression?
6 + 3 % 2
= _____________________
a) 4
b) 1
c) 5
d) 7
9. Which C statement below produces no error, neither a syntax error, nor a run time error nor a logical error? You may assume that the variable named resistance has been declared as follows:
double resistance;
a) scanf("%f",
&resistance);
b) scanf("%lf",
resistance);
c) printf("Resistance is
%lf (ohms)\n", resistance);
d) printf("Resistance is
%s (ohms)\n", resistance);
10. The following program compiles and runs without errors. What is the output of the program?
#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
11. The following C program compiles and runs without errors. What is the output from the program?
#include <stdio.h>
void variables(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void main(void)
{
int x = 5;
int y = 3;
variables(x,y);
printf("y = %i \n", y);
}
a) y = 3
b) y = 4
c) y = 5
d) y = 6
12. The following C program compiles and runs without errors. What is the output from the program?
#include <stdio.h>
int func1(int z)
{
z = 3*z;
return z;
}
int func2(int x)
{
x = x%1;
return x;
}
void main(void)
{
int x = 1;
int y = 7;
int r = 3;
x = func1(func2(r));
r = func2(y);
printf("x = %i , y = %i \n", x, y);
}
a) x = 2 , y = 18
b) x = 1 , y = 3
c) x = 3 , y = 7
d) x = 0 , y = 7
13. The following C program compiles and runs without errors. What is the output from the program?
#include <stdio.h>
#include <string.h>
char build1 (char * z)
{
z[3] = 'i';
}
char build2 (char m[])
{
strcpy(m,"Bandit");
}
void main(void)
{
char x[32] = "Suzuky";
char y[32] = "andit";
build1(&x[1]);
build2(y);
printf ("%s %s\n",
x,y);
}
a) Suzuiy Bandit
b) Suzuki Bandit
c) Suzuky anditBandit
d) Suzuky i
14. Given the pointer named fin declared below, which is the correct way to use fopen to open a file with the name “input.txt” for writing?
FILE * fin;
a) FILE = fopen("input.txt","w");
b) FILE = fopen("w","input.txt");
c) fin = fopen("input.txt","w");
d) FILE = fopen("w","input.txt");
15. When opening the file named "fileName" using fopen what action does fopen take when mode "a" is selected assuming that the file named "fileName" is successfully opened?
a) Opens “fileName” and deletes contents and writes in the empty file.
b) Opens “fileName” and writes at the end of the file.
c) Opens “fileName” for reading.
d) Opens “fileName” for reading and writing.
16. Which is the correct order of inputs to the qsort function? (num_of_elts means number of elements in the array, size_of_elts mean number of bytes for each element in the array)
a) qsort(arrayname, num_of_elts ,size_of_elts, compare_function);
b) qsort(arrayname, size_of_elts, num_of_elts, compare_function);
c) qsort(compare_function, arrayname, num_of_elts, size_of_elts);
d) qsort(compare_function, num_of_elts, size_of_elts, arrayname);
17. If the compare_function used in qsort returns a negative value then qsort swaps the array values.
a) True
b) False
18. Given the C program below, the count of how many distinct variables exist in the program equals _________________?
#include <stdio.h>
int r = 51;
int s = 4;
void myfunc(int r, int z)
{
printf("%i ", r/z);
}
void myfunc2(int s)
{
printf("%i ", r % s);
}
void main(void)
{
int r = 1;
int s;
if(r == 1)
{
s = 3;
myfunc(r, s);
}
}
a)
6
b)
5
c)
3
d) 7
19. Given the structured typedef named carData shown below, which is the correct way to declare a variable named car and assign the values of a 1999 Buick Century with a price of $5000.00? (note that Buick is the make and Century is the model)
typedef struct
{
float price;
char make[32];
char model[32];
int year;
} carData;
a)
carData car = {1999, "Buick Century",5000.00};
b)
carData car = {1999, "Century", "Buick",
5000.00};
c)
carData car = {5000.00, "Buick",
"Century",1999};
d) carData {1999, "Buick", "Century",5000.00};
20. Which of the following loops produces the same value for the variable x as the loop shown below?
int i, x = 0;
for(i=0; i<=100; ++i)
x += i;
printf("x = %i \n",x); /* prints x = 5050 */
|
a) int i = 0, x = 0;
while(i<=100) x += i;
printf("x = %i \n",x);
|
b) int i = 0, x = 0;
do x += i; while(i<=100); ++i;
printf("x = %i \n",x);
|
|
c) int i = 0, x = 0;
while(i<=100) x += i++;
printf("x = %i \n",x);
|
d) int i = 0, x = 0;
while(i<=100) x += ++i;
printf("x = %i \n",x); |
Use the following structures named Album and Track along with the variables named mycd and myPtr in the next 3 questions.
typedef struct {
char name[40]; /* track name or song name */
int length; /* in seconds */
} Track;
typedef struct {
int cdno; /* cd number */
char title[30];
char artist[20];
int year;
int num_tracks; /* number of tracks */
int quantity;
float price;
float sales[12]; /* cd sales ($US) over 12 months */
Track tracks[20]; /* info for each track */
} Album;
Album mycd;
Album *
myPtr = &mycd;
21. Which C statement below assigns the value 123 to the length subfield of the first track of mycd ?
a) (mycd.tracks)->length = 123;
b) (mycd -> tracks[1]).length = 123;
c) mycd[1].length = 123;
d) mycd.tracks[1].length = 123;
22. Which C statement below assigns the value 1001 to the cdno field of mycd ?
a) myPtr.cdno = 1001;
b) myPtr -> cdno = 1001;
c) (*myPtr)->cdno = 1001;
d) (&myPtr).cdno = 1001;
23. Which C statement below assigns the value “Tesla” to the name subfield of the last track of mycd ?
a) mycd.tracks[19].name = "Tesla";
b) mycd.tracks[20].name = "Tesla";
c) mycd->tracks->name = "Tesla";
d) strcpy(mycd.tracks[19].name, "Tesla");
24. The following C program compiles and runs without errors. What is the output produced by this program?
#include <stdio.h>
int x = 0;
void swap(int x, int * y)
{
int temp = x;
x = *y;
*y = temp;
}
void main(void)
{
int x = 1;
int y = 2;
swap(x, &y);
printf("x = %i y = %i \n", x, y);
}
a) x = 1 y = 1
b) x = 1 y = 2
c) x = 2 y = 1
d) x = 2 y = 2
Use the Student structure and array named students shown below to answer the question below.
typedef struct {
char firstname[40];
char lastname[40];
int age;
}Student;
Student students[50];
25. Which compare function shown below would you use in order to sort the array named students in ascending order by lastname? In the event of two last names being the same the compare function should look at the value in the firstname field and sort in ascending order by first name. This means, for example, that “Bob Jones” (firstname = “Bob” , lastname = “Jones”) should come before “Adam Smith” (firstname = “Adam” , lastname = “Smith”) in the sorted order since “Jones” comes before “Smith” alphabetically. But “Adam Smith” would be sorted before “Bob Smith” since “Adam” comes before “Bob” alphabetically.
a)
int compare( Student * L, Student * R)
{
return strcmp(L->lastname, R->firstname);
}
b)
int compare( Student * L, Student * R)
{
return strcmp(L->firstname, R->lastname);
}
c)
int compare( Student * L, Student * R)
{
if(strcmp(L->lastname, R->lastname) != 0)
return strcmp(L->lastname, R->lastname);
else
return strcmp(L->firstname, R->firstname);
}
d)
int compare( Student * L, Student * R)
{
return strcmp(L->lastname, R->lastname);
return strcmp(L->firstname, R->firstname);
}