You are on page 1of 5

Section 1: Short Answer

Q1A. Consider the following code segments:


if( num1 > 9 )
{
if( num2 < 12 )
printf( "A" );
}

if( *** )
printf( "A" );
printf( "B" );

printf( "B" );

What Boolean expression can be inserted in place of *** in the code segment on the right
so that these two code segments produce exactly the same output for every possible value of
num1 and num2?
( num1 > 9 && num2 < 12 )

Q1B. What value should be used in place of *** in the code segment below so that count has the
value 20 when the code segment has executed?
int count = 0;
int indexA = 0;
int indexB;
while( indexA < *** )
{
indexB = 0;
while( indexB < 5 )
{
count++;
indexB++;
}
indexA++;
}
4

Q1D. 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?
AC

Page 2 of 5
Q1E. 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?


sum = 10, count = 5

Section 2: Short Answer


Q2A. What is the binary equivalent of the decimal number 35?
Show all intermediate steps in your calculation.
2 35
17
8
4
2
1
0

remainder
1
1
0
0
0
1

So, 3510 = 1000112

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


Show all intermediate steps in your calculation.
AF16 =
=
=
=

A( 161 ) + F ( 160 )
10( 16 ) + 15( 1 )
160 + 15
17510

Page 3 of 5
Section 3: Explain in Plain English
Q3A. Consider the following code segment. Assume that b is greater than a.
int retVal = 0;
int next = a;
while( next <= b )
{
retVal += next;
next++;
}

Explain in plain English the value that will be stored in retVal when this code segment
has executed.
The value stored in retVal will be the sum of the integers from a
to b, including a and b.

Q3B. 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 a, b and c.

Page 4 of 5
Section 4: Flowcharts
Q4A.

Consider the following program specification:


Repeatedly:
prompt the user for a time and read it from the keyboard
compute the position p of the particle at that time t using the formula: p = 10sin(t)
print on the screen the position of the particle to 2 decimal places
until the user enters a time that is negative.
Draw a flowchart that represents a solution to this problem.

Page 5 of 5
Section 5: Writing a complete program
Q5A.

Write 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.
#include <stdio.h>
#define
#define
#define
#define
#define
#define
#define

MIN_AGE 1
MAX_AGE 125
ADULT 18
SENIOR 65
CHILD_FARE 13.45
ADULT_FARE 21.95
SENIOR_FARE 18.95

int main( void )


{
int age;
double fare;
printf( "Please enter your age: " );
scanf( "%d", &age );
while( age < MIN_AGE || age > MAX_AGE )
{
printf( "Error: age entered is not valid.\n" );
printf( "Please enter your age: " );
scanf( "%d", &age );
}
if( age < ADULT )
fare = CHILD_FARE;
else if( age < SENIOR )
fare = ADULT_FARE;
else
fare = SENIOR_FARE;
printf( "Cost of tour ticket: $%.2f\n", fare );
return 0;
}

You might also like