CSC128
CSC128
CSC128
CS/APR2009/CSC128
INSTRUCTIONS TO CANDIDATES 1. 2. This paper consists of two (2) parts: PART A (10 Questions) PART B (6 Questions) Answer ALL questions from all two (2) parts. i) ii) 3. Answer PART A in the Objective Answer Sheet. Answer PART B in the Answer Booklet. Start each answer on a new page
Do not bring any material into the examination room unless permission is given by the invigilator. Please check to make sure that this examination pack consists of: i) the Question Paper ii) an Answer Booklet - provided by the Faculty iii) an Objective Answer Sheet - provided by the Faculty
4.
CONFIDENTIAL
CS/APR2009/CSC128
1. Which of the following statements is FALSE? A. B. C. D. Constants cannot be changed once they are initialized. A variable is initialized when it is first given a value. The value of a variable can be replaced with another by a process called assignment. An assignment operator in C++ is the same as mathematical equality.
2.
The compile stage is when A. B. C. D. the object code is linked with code for functions in other files the C++ program is translated into machine language code the program is executed one instruction at a time the program is placed in memory
3.
By using the variable declarations below, which of the following Boolean expressions is FALSE? int y = 2 , x = - 4 , z =
A. y > x && ! ( y >= z )
300, w = 2 ;
B. C.
D.
4. Which of the following is TRUE about a d o . . w h i l e statement? A. B. C. D. A counter variable must be used to control the number of execution. A sentinel value must be used to stop the loop execution. The loop condition is validated after the execution of a loop body. The loop condition is validated before the execution of a loop body.
CONFIDENTIAL
CONFIDENTIAL
CS/APR2009/CSC128
!= 0;
= x / 2)
Which of the following program segments DO NOT produce the same result as the above program segment? A. int x = 150; int i = 0; while (x != 0) { x = x / 2; i++; } int x = 150; int i = 0; do { x = x / 2; i++; } while (x != 0) int x = 150; for (int i = 0; i != 0; i++) x = x / 2; int x = 150; for (int i = 0; x != 0; i++) x = x / 2;
B.
C.
D.
CONFIDENTIAL
CONFIDENTIAL
CS/APR2009/CSC128
6.
int sum = 0; int num = 10; if ( num > 0) sum = sum + 10; else if ( num > 5) sum num + 15; sum = sum + 10; cout sum;
A. B. C. D.
10 20 25 35
7.
The statement which can be used to declare a character based array called s t r of six elements is . A. B. C. D. char str [6]; string str; char str[5]; string str[6];
CONFIDENTIAL
CONFIDENTIAL
CS/APR2009/CSC128
8.
Which of the following function definitions DO NOT contain any errors? i . void printNum(int x) cout i i . x;
i i i .
i v .
A. B. C. D. 9.
ii i, iii i, iii, iv
Which of the following is FALSE about function? A. B. C. D. The formal parameters listed in a function header, must include each parameter's data type and name. The names of the formal parameters in the function header must be the same to the names of the actual arguments in the function call. The sequence of parameters in the function call and parameters in the function header must be the same. The function call of a void function should be a statement by itself, while the return function must be written in other statement.
10.
A. B. C.
D.
inFiie.open ();
CONFIDENTIAL
CONFIDENTIAL
CS/APR2009/CSC128
i.
saleCommissionl,
(2 marks)
ii.
To prompt the user to input 3 values and store them into b a s i c S a l a r y , saleCommissionl, saleCommission2.
(2 marks)
i i i .
(2 marks)
b) Evaluate the expressions below :
i . i i . i i i . i v .
cout 4 % 3 * 2 - 7 / 4 endl; cout 35 * (2 + sqrt(4)) / 5 endl; cout cout 30 / 60 endl; "60 / 30" endl;
(4 marks)
CONFIDENTIAL
CONFIDENTIAL
CS/APR 2009/CSC128
void main() { int numl, num2, number, a, b; cin numl >> num2; if (numl > num2) { a = numl; b = num2; } else { a = num2; b = numl; } for (int i = 3; i <= 5; i++) { cin number; if (number > a) { b = a; a = number; } else if (number > b) b = number; } cout "Result 1 " a endl
"Result 2 "
endl;
a)
ii)
-56
(6 marks) b) What is an appropriate message to replace R e s u l t l and R e s u l t 2? (2 marks) c) Replace the variables a and b with more suitable names. (2 marks)
CONFIDENTIAL
CONFIDENTIAL
CS/APR2009/CSC128
QUESTION 3 Find and correct the LOGIC ERRORS in the following program segments: a) This program segment should accept an allowance and count the number of staff who received allowance in the range less than RM200, between RM200 to RM300 and greater than RM300.
cin allowance; if (allowance > RM200) totStaff++; else if (allowance > RM300) totStaff++; else totStaff++; cout totStaff; (4.5 marks) b) This program segment should accept ten exam scores and count the number of students who passed. The passing mark is 50.
int statusPass = 0, a = io, examScore; whil e (a >= 1) { cin examScore r if (examScore < 50) statusPass = statusPass + examScore; a = a + 1; } (4.5 marks)
CONFIDENTIAL
CONFIDENTIAL
CS/APR2009/CSC128
c)
This program segment should accept the number of customers then calculates the total of three item price bought by each customer.
int noOfCust, mark, sum = 0; cin noOfCust; for (int i = 1; i >= noOfCust; i++) { int j = 0; item = 3; while ( j <= noOfCust) { cin price; sum = sum + price; item = item + 1; } cout "The total price is : " }
price;
(6 marks)
36
[0]
51
[1]
18
[2]
110
[3]
89
[4]
Show the contents of the array elements after each of the following program segments is executed. Assume 4 a) and 4 b) is executed independently. a) temp = NUMBER[0]; NUMBER[0] = NUMBER[2]; NUMBER[2] = temp;
NUMBER[ ]
[0]
[1]
[2]
[3]
CONFIDENTIAL
10
CS/APR2009/CSC128
b) temp = NUMBER[0];
(i = 1; i < 3; i++) NUMBER[i] = NUMBER[i + l ] ; NUMBER[4] = t e m p ; for
NUMBER[]
to;
[i:
[2;
[3]
:4] (5 marks)
QUESTION 5 a) Write these functions definitions: Function convertToGrams () receives a measurement in pound and ounce as parameters, convert them to the equivalent gram and return the result of the conversion. (1 pound = 16 ounces, 1 pound = 453.592 grams) (4 marks)
II.
Function f i n d i Q L e v e l () receives an IQ score as a parameter. Determine the IQ level based on IQ scores from the following table and return the IQ level. IQ Scores <20 20 - 140 141 -210 >210 IQ Level Slow Normal Intelligent Genius (6 marks)
b)
Write one calling function statement for each of the following function prototypes. int FunctionA(int, int, int); void FunctionB(char[ ]); void FunctionC(int, floats, char); int FunctionD (float, floats, int&); char FunctionE () ; (5 marks) CONFIDENTIAL
i v . V.
CONFIDENTIAL
11
CS/APR2009/CSC128
QUESTION 6 The UPIN Cafe has three types of menu. The price for each menu is stated in the following table. Type of Menu Kids meal Snack Combo Price RM 10.50 RM 15.50 RM 18.00
Each customer must pay additional 5% for the government tax. Write a complete C++ program that includes the following statements. Read a type of menu and number of order. Calculate the government tax and total price of each customer order. Read amount received by the customer. Calculate the balance to be returned to the customer. Print a receipt for the customer using the following format.
WELCOME TO UPIN Cafe ************************************************** xxxxxxx Type of Menu XXX Number of o r d e r ( s ) Total(RM) xxxx.xx Cash (RM) xxxx.xx B a l a n c e (RM) xxxx.xx Thank y o u . P l e a s e come a g a i n . **************************************************
These processes will be repeated when customer received the order. Calculate total customer and total sale collection for that day. (20 marks)
CONFIDENTIAL