You are on page 1of 7

Massey University - Albany Campus

Institute of Information and Mathematical Sciences


Computer Science
159.101 - Programming Fundamentals
2001
Mid-Semester Test - Semester 1

Family name .........................................................................


First name

.........................................................................

ID

.........................................................................

Mark.................

/60

Read ALL questions CAREFULLY before answering.


You will lose marks if (i) your answers do not follow the layout as described in lectures
or (ii) you use features of the C language not covered in lectures.
Each question is worth 10 marks
ANSWER ALL QUESTIONS.
TIME ALLOWED 90 minutes.
Contribution to Final Mark 15%

Question 1.
a)

Which is bigger: 10MBytes or 1GigaByte?


[1]
---------------------------------------------------------------------------------------------------------------------

b)

A program should be 'robust'.What does this mean?


[1]
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

c)

Give an example of an error that the compiler can detect.


[1]
---------------------------------------------------------------------------------------------------------------------

d)

Write example declarations for a character, a real number, an integer and a string:
[2]
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

e)

is the header file that contains the description of the printf function. Which two header
files contain the description for strcpy and getch?
[2]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

f)

Which format specifier do you use to print out a character variable?


[1]
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

g)

How do you tell printf that the maximum number of characters in a string that it outputs is 7?[1]
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

h)

Which format specifier do you use to print an integer in a field width of 4 with leading zeros? [1]
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

stdio.h

Question 2.
The following code contains 5 errors. Mark each one of them with a circle, and describe why it is
an error.
[10]
#include <stdio.h>
int I;
int main {
for (i=0;i<10,i+1) {
printf("%04d\n',i);
}
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Question 3.
For the following program, state precisely what the output will be (make any spaces visible) :
#include <stdio.h>
int i,j;
int main() {
i = 1;
while (i<13) {
j = i%7;
printf("i=%-2d j=%d\n",i,j);
i = i + 3;
}
}

[10]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Question 4.
Write a C function zero that uses 5 printf calls to produce the following pattern on output.
000
0
0
0

0
0
0
000

[5]
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Write a main program that uses the above function to produce 3 patterns, each seperated with a
blank line (but no blank line at the end). You need to include a function prototype at the beginning
of your program, but you do not need to write out the zero() function again.
[5]
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Question 5.
Write a program to calculate the total cost of a shopping bill.
Bread costs $1.50 a loaf, but if you buy 5 or more the cost is only $1.25 a loaf.
Milk costs $1.80 for a 1 litre carton, or $2.50 for a 2 litre carton.
Fish costs $12.00 a kilo.
Ask the user for how much of each they require (which is a whole number), and then output the
total cost. In the case of milk, make sure that the user pays the least amount.
[10]
e.g.
How many loaves of bread? 10
How many litres of milk? 5
How many kilos of fish? 3
The total cost is: $55.30
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Question 6.
An 'ascending word' is one in which each letter in the word is followed by a letter that comes after
it in the alphabet. (A word with a repeated letter is not ascending).
ant

is an ascending word because n is after a in the alphabet, and t is after n.

but
dog

is not an ascending word because although o is after d in the alphabet, g is before o.

Write a program that asks the user to type in a word (in lowercase), and responds with whether
the word is ascending or not.
[10]
e.g.
Type in a word: almost
almost is an ascending word

or
Type in a word: nearly
nearly is not an ascending word

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------This page is to be used for rough working. Ensure that programs written in the answer sections
are correctly indented

You might also like