You are on page 1of 7

THE UNIVERSITY OF BRITISH COLUMBIA

APSSC 160: MIDTERM EXAMINATION


OCTOBER 27TH, 2004

Name: _______________________

Student #:

_________________

Signature: _____________________

Lab Section: ______

Instructor: _______________

Notes about this examination


1. You have 60 minutes to write this examination.
2. No notes, books, or any type of electronic equipment is allowed including cell phones and
calculators.
3. Good luck!

1.
2.

3.

4.

5.

Rules Governing Formal Examinations


Each candidate must be prepared to produce, upon request, a
Library/AMS card for identification.
Candidates are not permitted to ask questions of the
invigilators, except in cases of supposed errors or
ambiguities in examination questions.
No candidate shall be permitted to enter the examination
room after the expiration of one-half hour from the
scheduled starting time, or to leave during the first half hour
of the examination.
Candidates suspected of any of the following, or similar,
dishonest practices shall be immediately dismissed from the
examination and shall be liable to disciplinary action.
a. Having at the place of writing any books, papers or
memoranda, calculators, computers, audio or video
cassette players or other memory aid devices, other
than those authorized by the examiners.
b. Speaking or communicating with other candidates.
c. Purposely exposing written papers to the view of
other candidates. The plea of accident or
forgetfulness shall not be received.
Candidates must not destroy or mutilate any examination
material; must hand in all examination papers; and must not
take any examination material from the examination room
without permission of the invigilator.

Question

Max
7
10
17
10
6

Total

50

1
2
3
4

Mark

Name (please print):

Page 2 of 7

Section 1: Multiple Choice


One mark each. For each of the multiple choice questions below, circle the best answer.
Q1A. Which of the following mechanisms is used when a variable of type int is passed as a
parameter to a function in C?
a) pass by address
b) pass by formal parameters
c) pass by value
d) pass by actual parameters
Q1B. Which of the following is not a primitive data type in C?
a) double
b) boolean
c) char
d) float
Q1C. The word used to describe the set of statements in which a variable can be used is:
a) global
b) local
c) lifetime
d) scope
Q1D. Which one of the following can never be used as a unary operator?
a) +
b)
c) /
d) !
Q1E. A compiler is
a) a program translator
b) a high-level language
c) a binary code
d) an operating system
Q1F. Modularity is
a) repeating the same piece of code many times
b) hiding irrelevant details of an algorithm
c) another name for computer hardware
d) dividing a solution into smaller components
Q1G. A function prototype
a) is ignored by the compiler
b) is only shown in a module chart
c) shows how a function should be called
d) is not useful to a programmer

Name (please print):

Page 3 of 7

Section 2: Programming Questions


[10] Q2. Use a trace table to trace the following code segment and determine the output to the screen
when the segment is executed:
int data[] = { 4, 5, 5, 3, 2, 0, 3 };
char letters[] = { 'i', 'H', 's', 'C', 'l', 'o', 'e' };
int index;
for( index = 6; index >= 0; index-- )
{
nextValue = data[ index ];
nextCharacter = letters[ nextValue ];
printf( "%c", nextCharacter );
}

Note:
No marks will be awarded if you simply present the output from this program. You
must include a trace table to illustrate that you understand exactly how this code segment
works.
The format specifier "%c" is used to print a single character.

Name (please print):

Page 4 of 7

[17] Q3. Be sure to read the entire question before you write any code!
Write a complete program in C that:
1. prompts the user for the radius of the base of a cone and reads it from the keyboard
2. prompts the user for the height of a cone and reads it from the keyboard
3. computes the volume of the cone using the formula:
volume = ( 1 / 3 ) x (radius)2 x height
4. prints the volume of the cone on the screen
5. repeats steps 1-4 until the user enters 0 for the base radius of the cone
Your program must include a function that:
takes the base radius and height of a cone as its only parameters
returns the volume of the cone
Note:
if the user enters 0 for the base radius of the cone, they should not then be prompted to
enter the height of the cone!
do not assume that the base radius and the height are integers
in the interests of time, it is not necessary to provide any comment statements.

/* continue your answer to Q3 on the next page */

Name (please print):


/* continue your answer to Q3 on this page */

Page 5 of 7

Name (please print):

Page 6 of 7

[10] Q4. Identify the errors in the following program by circling them, and correct the errors by crossing
out code / inserting code where appropriate. Read the comment statements carefully! Assume
that they are correct and accurately describe what the program is intended to do. Note that the
code does not contain any syntax errors!
/* This program reads from a data file information about all the animals in
* an animal shelter, and determines which ones need special attention to
* expedite their adoption.
*
* Input: the first row of the data file contains a single integer indicating
*
the number of rows that follow. Each subsequent row contains a
*
single animal's ID #, age, and number of days at the shelter. All
*
values are integers
* Output: to the screen - a list of all animal IDs corresponding to animals
*
that are more than 6 years old, and have been at the shelter for
*
more than 60 days
*/
#include <stdio.h>
#include <stdlib.h>
#define FILENAME "animals.txt"
int main(void)
{
FILE *in;
int numRecords;
int count;
int id, age, numDays;
/* open file and check if successful */
in = fopen(FILENAME, "r");
if( in = NULL )
{
printf("Error opening data file\n");
}
/* read the number of animal records */
fscanf( in, "%i", &numRecords );
printf("Animals needing critical attention:\n");
/* process all records */
while( count < numRecords )
{
/* read one record */
fscanf( in, "%i %i %i", &id, &age, &numDays );
/* if the status of the animal is critical
* (the age is more than 6, and the numDays is more than 60),
* output that animal's ID# */
if( age > 6 || numDays > 60 )
{
printf("%i\n", id);
}
}
return 0;
}

Name (please print):

Page 7 of 7

[6] Q5. Consider the following two functions:


/* draws 10 stars */
void drawStars()
{
int count = 0;
for( count = 0; count < 10; count++ )
{
printf( "%c", '*' );
}
}
/* draws 5 bars */
void drawBars()
{
int count = 0;
for( count = 0; count < 5; count++ )
{
printf( "%c", '|' );
}
}

Note that these functions contain very similar code and that neither of them takes parameters.
Replace these two functions by a single function with parameters that can perform the task of
either of the two functions above, depending on the value of the parameters that are passed.

You might also like