Ccsb163 Midterm Test - Aug 2006 - Questions
Ccsb163 Midterm Test - Aug 2006 - Questions
Ccsb163 Midterm Test - Aug 2006 - Questions
B. SC & B.ENG
MID TERM TEST
SEMESTER I 2006/2007
PRINCIPLES OF C PROGRAMMING
(CCSB163/CSEB113)
INSTRUCTIONS TO CANDIDATES.
1.
2.
3.
DO NOT OPEN THIS QUESTION PAPER UNTIL YOU ARE INSTRUCTED TO DO SO.
THIS QUESTION PAPER CONSISTS OF 6 PRINTED PAGES INCLUDING THIS PAGE.
Operators are tokens that result in some kind of computation or action when
applied to variable in an expression.
2.
3.
4.
5.
The C operator that has the lowest precedence level among the following
operators is:
(a)
*
(b)
+
(c)
%
(d)
=
2.
3.
4.
5.
Semester 1 2006/2007
Principles of C Programming
SECTION C (DESIGN)
Question 1
Design a program using the pesudocoding technique (pseudocode) that will find the sum
of all the integers between two integers that are given by the user. An example of your
output will probably look like the following, if the user enter 5 and 7:
Enter the first integer: 5
Enter the second integer: 7
Total sum of all the integers between 5 and 7 is 18.
The calculation for the answer was obviously 5+6+7 = 18. You may assume the first
number will always be smaller than second.
[10 marks]
Question 2
A new Gallery Smart Shop owned by Robin sells items below:
Item
Pensonic Cordless Steam Iron
Toaster Ebba
Table Fan National
Water Boiler 3.99 L
Vacuum Cleaner National
Code
1
2
3
4
5
Price (RM)
30.00
35.00
40.00
90.00
120.00
Design a program using the pesudocoding technique (pseudocode) that ask user to enter
the code and quantity of the item. The program calculates and prints out the total price of
the item entered. The program should also displays an error message if the user enters
invalid item code.
[10 marks]
SECTION D (TRACING)
Question 1
What are the values stored in variables A, B, C and D when the following program
segment is executed? Assume that A, B, C and D are integer variables.
A = 14 5 % 3;
B = 7 * 3 + 9 / 4;
C = (6 + 3) * (7 % 4);
D = 2 < 0;
[8 marks]
Page 3 of 6
Semester 1 2006/2007
Principles of C Programming
Question 2
Determine the output of the code fragment.
int a = 5, b = 4, c = 1 ;
if ( a < b + 1 ) {
printf("Tea, Earl Grey, Hot!\n") ;
} else if ( a > b + c ) {
printf("Ahead warp factor 9. Engage!\n") ;
} else if ( a % b >= c ) {
printf("Warp core breach in 20 seconds!\n") ;
} else {
printf("I sense a lot of anxiety in this room.\n") ;
}
[5 marks]
Question 3
The following C program contains nine syntax errors. Rewrite program such that is free
of syntax. Highlight your corrections by using circle.
#include<stdio.H>
#define MESSAGE Exams today
Main()
{
int a,b,c;
int 2number,number_three = 10;
a=5; b=3, c=2;
printF("%d",a + b + c);
printf("%d\n ',Number_three * 5);
printf(MESSAGE)
}
[9 marks]
Question 4
Consider the following fragment of C-codes
if (Marital_status == S)
if (gender == M && age >= 18 || age <= 26)
printf ( You are a single adult male);
else
printf ( You are a single teen male);
What will be printed if Marital_status = S , gender = M and age = 15?
[6 marks]
Page 4 of 6
Semester 1 2006/2007
Principles of C Programming
Question 5
Consider the following fragment of C-codes
#include<stdio.h>
main() {
int hours;
double fee;
printf(Enter no of hours: );
scanf(%d, &hours);
if (hours > 2) {
fee = 2+(hours-2)*2;
} else {
fee = hours * 1 ;
}
printf(Fee: RM%.2lf, fee);
}
Determine the output on the screen when user enter
(a) 1
(b) 2
(c) 3
[6 marks]
Question 6
Find the value of integer n if the values of a, b and c are given as follows:
int a = 5, b = 4, c = 3;
(a) n = (a c) * a c;
(b) n = a % c;
(c) n = c / a % ++a - -b;
[6 marks]
SECTION E (PROGRAMMING)
Question 1
Write a complete C program that prompts the user for two positive integers greater than
0. The input is prompted one at a time. If the user enters an invalid integer, the program
displays an error message and prompts again for the second time (there is not need to use
a loop/repetition). The program then displays the two integers as a real number, where
the first integer forms the integer portion of the real number, and the second integer forms
the fractional portion. For example, if the user enters 10 and 20, then the real number
being displayed is going to be 10.20. (Hint: You do not need to perform any arithmetic
calculation.)
[10 marks]
Page 5 of 6
Semester 1 2006/2007
Principles of C Programming
Question 2
Write a complete C program to translate the following flowchart into its equivalent
executable codes without changing any of its logic.
[10 marks]
Begin
Read Age
False
True
Age more than 59
Read Retired
True
Age > 20
False
Print Adult
True
Retired = Y
Age > 12
False
True
False
Print Teen
Print Retired
Senior
Print Working
Senior
Print Child
End
Page 6 of 6
Semester 1 2006/2007
Principles of C Programming