You are on page 1of 10

APSC 160: SAMPLE MIDTERM EXAMINATION

SOLUTIONS

Don't even think about looking at these solutions unless you've put serious effort into coming up
with your own solution. Reading a solution that someone else put together is easy and, in itself,
will not prepare you for the midterm exam. You need to be able to come up with these solutions on
your own!
Note:
1) Many people lost marks on Q4A because they did not read the question. If you do not make
use of the printStars function, you will lose marks, even though your solution "works".
2) Many people lost marks on Q4B because they did not follow the hint provided. If your
solution does not make use of the function written in Q4A, you will lose marks, even
though your solution "works".
3) Many people lost marks on Q5B and Q5C because they did not make use of the symbolic
constants provided.
4) Many people lost marks on Q5C because they did not make appropriate use of if-else
statements and/or their solution incorporated redundant Boolean expressions.

Page 2 of 10
Section 1: Multiple Choice
For each of the multiple choice questions below, circle the letter corresponding to the one right
answer.
[2]

Q1A. Consider the following code segment:


if( num1 < 2 ||
printf( "A"
if( num2 > 9 )
printf( "B"
else
printf( "C"

num2 > 7 )
);
);
);

What is printed on the screen when this code segment runs assuming that num1 and num2
are variables of type int and that num1 has the value 4 and num2 has the value 8?
a)
b)
c)
d)
e)
[3]

B
C
A
AB
AC

Q1B. Consider the following code segment:


int count = 1;
int sum = 0;
while( count < 6 && sum < 7 )
{
sum += count;
count++;
}
printf( "sum = %d, count = %d\n", sum, count );

What is printed on the screen when this code segment runs?


a)
b)
c)
d)
e)

sum
sum
sum
sum

=
=
=
=

10,
15,
15,
10,

count
count
count
count

none of the above

=
=
=
=

5
6
5
4

1 mark was awarded if you answered


d) rather than a). In that case you got
the sum right but the count wrong.
There were no marks for choosing b),
c) or e).

Page 3 of 10
[2]

Q1C. What is the value of count after the following code segment has executed?
int count = 0;
int indexA;
int indexB;
for( indexA = 0; indexA < 4; indexA++ )
{
for( indexB = 3; indexB >= 0; indexB-- )
count++;
}

a)
b)
c)
d)
e)

[2]

none of the above

Q1D. Suppose you want to write a function that takes an array of integers and the size of that
array as parameters and that prints all the entries in the array onto the screen. What is the
function prototype?
a)
b)
c)
d)
e)

[3]

3
4
9
12

char printArray( int[], int );


void printArray( int[ int ] );
int[] printArray( void );
void printArray( int[], int );

none of the above

Q1E. Suppose you are asked to write a program to display some simple statistics for a set of
positive integers supplied by the user. The user may provide as many positive integers as
he/she likes and then indicates that he/she has finished entering the positive integers by
entering the value -1. The program then prints on the screen: (1) the average of all the
positive integers entered by the user and (2) the largest of all the positive integers entered
by the user.
Such a program would...
a)
b)
c)
d)
e)

...require an array but not a loop.


...not require an array but would require one loop.
...not require an array but would require at least two loops.
...require an array and one loop.
...require an array and at least two loops. 1 mark was awarded if you answered
c) rather than b), for realizing that an
array was not needed. There were no
marks for choosing a), d) or e).

Page 4 of 10
Section 2: Short Answer
[2]

Q2A. What is the binary equivalent of the decimal number 35?


Show all intermediate steps in your calculation.
3510 = 1000112

[2]

35
17
8
4
2
1
0

1
1
0
0
0
1

Q2B. What is the decimal equivalent of the hexadecimal number AF?


Show all intermediate steps in your calculation.
AF16 = 10 ( 16 ) + 15 ( 1 ) = 160 + 15 = 17510

Section 3: Explain in Plain English


[2]

Q3A. Explain in plain English (in no more than one or two sentences) the purpose of the
following code segment, assuming that a, b and c are variables of type int. For example,
you might answer "Prints the average of the values a, b and c." This is not what the code
does but it gives you an example of the kind of answer that's expected.
if( a < c && b < c )
printf( "%d", c );
else if( a < b )
printf( "%d", b );
else
printf( "%d", a );

Prints the largest of the values of a, b and c on the screen.


[4]

Q3B. Explain in plain English (in no more than one or two sentences) the purpose of the
following function, assuming that the parameter size represents the size of the array data
that's also passed as a parameter. Further assume that size is at least 2.
void procArray( int data[], int size )
{
int temp = data[ 0 ];
int index;
for( index = 0; index < size 1; index++ )
data[ index ] = data[ index + 1 ];
data[ size - 1 ] = temp;
}

Shifts all the values in the array to the left by one slot. The element originally in the first
position is moved to the last position.

Page 5 of 10
Section 4: Working with programmer defined functions
[8]

Q4A. In this question, you should assume that the following function has already been written and
is available for you to use:
/*
* Prints a row of stars on the screen with a space after
* every star. Does not print '\n' at the end of the row.
* Param: numStars - the number of stars to print
*/
void printStars( int numStars );

Write a function printRect that takes the width and height of a rectangle as its only
parameters and that prints a rectangle of stars of the specified size on the screen. You can
assume that the values passed as parameters are greater than 0. So, for example, if the
width is 3 and height is 2, your function must print the following rectangle (note that there's
a space after each star):
* * *
* * *

Note: in the interests of time, it is not necessary to include any comment statements to
document your code.
void printRect( int width, int height )
{
int row;
for( row = 0; row < height; row++ )
{
printStars( width );
printf( "\n" );
}
}

Page 6 of 10
[10] Q4B. Write a function that takes the size of a picture as its only parameter and that prints a picture
on the screen as illustrated below. You can assume that the value passed as a parameter is
greater than zero. Note that there's a space after every star.
size is 1

size is 2

size is 3

*
*
*
*

*
*
*
*
*
*
*
*
*

* * *
* * *
*
*

*
*
*
*
*
*
*
*
*

*
*
*
*
*
*
*
*
*

*
*
*
*
*
*

*
*
*
*
*
*

* * * *
* * * *
* * * *
*
*
*

Hint: how does the solution to Q4A play a role in the solution to this question? Assume
that the function written in Q4A is available for use in this question.
Note: in the interests of time, it is not necessary to include any comment statements to
document your code.
void printPic( int size )
{
int row;
for( row = size; row > 0; row-- )
printRect( row * size, size );
}

Page 7 of 10
Section 5: Writing a complete program using functions
Now we're going to complete a program that:

repeatedly prompts the user for his/her age until an age between 1 and 125 inclusive
(i.e., including 1 and 125) is entered. If the user enters an age that is not valid (i.e.,
not in the specified range), your program must print an error message before
prompting for another value.

prints a message on the screen to indicate the cost of a ticket (to 2 decimal places)
for a tour bus as indicated in the table below:
age of tourist
child - under 18
adult 18 to 64 inclusive
senior 65 or older

cost of ticket
$13.45
$21.95
$18.95

Important notes:
In the interests of time, it is not necessary to include any comment statements in the
code that you write.
Carefully read all the code that has been provided for you on pages 8 through 10
before you write any code of your own.

[4]

Q5A. Fill in the code in the two empty boxes provided in main. You should carefully read the
documentation for the functions provided on pages 9 and 10 before doing so.

[8]

Q5B. Write the implementation of the function getAge on page 9. Read the supplied
documentation carefully.

[8]

Q5C. Write the implementation of the function getFare on page 10. Read the supplied
documentation carefully.

/* Write your code on the following pages */

Page 8 of 10
#include <stdio.h>
#include <stdlib.h>
#define
#define
#define
#define
#define
#define
#define

MIN_AGE 1
MAX_AGE 125
MIN_ADULT_AGE 18
MIN_SENIOR_AGE 65
CHILD_FARE 13.45
ADULT_FARE 21.95
SENIOR_FARE 18.95

/* function prototypes */
int getAge( void );
double getFare( int );
int main( void )
{
double fare;
int age;
age = getAge();
fare = getFare( age );
printf( "Cost of ticket: %.2f\n", fare );
system( "PAUSE" );
return 0;
}

Page 9 of 10
/*
* Prompts the user for his/her age until an age between MIN_AGE
* and MAX_AGE has been entered. If the user enters an age that
* is not in the specified range, prints an error message before
* prompting for another value.
* Return: age entered by the user in range MIN_AGE to MAX_AGE,
* inclusive.
*/
int getAge( void )
{
int age;
printf( "Enter your age (%d to %d):\n", MIN_AGE, MAX_AGE );
scanf( "%d", &age );
while( age < MIN_AGE || age > MAX_AGE )
{
printf( "Error: age not in range.\n\n" );
printf( "Enter your age (%d to %d):\n", MIN_AGE, MAX_AGE );
scanf( "%d", &age );
}
return age;
}

Page 10 of 10
/*
* Computes the cost of a tour ticket based on the age of the
* tourist.
* Param: age the age of the tourist
* Return: corresponding cost of tour ticket
*/
double getFare( int age )
{
double fare;
if( age < MIN_ADULT_AGE )
fare = CHILD_FARE;
else if( age < MIN_SENIOR_AGE )
fare = ADULT_FARE;
else
fare = SENIOR_FARE;
return fare;
}

You might also like