You are on page 1of 7

CS/APR 2007/CSC425

CONFIDENTIAL

UNIVERSITI TEKNOLOGI MARA


FINAL EXAMINATION

COURSE

INTRODUCTION TO COMPUTER PROGRAMMING

COURSE CODE

CSC425

EXAMINATION

APRIL 2007

TIME

3 HOURS

INSTRUCTIONS TO CANDIDATES
1.

This question paper consists of two (2) parts :

2.

Answer ALL questions from all two (2) parts in the Answer Booklet. Start each answer on a
new page.

3.

Do not bring any material into the examination room unless permission is given by the
invigilator.

4.

Please check to make sure that this examination pack consists of:
i)
ii)

PART A (9 Questions)
PART B (2 Questions)

the Question Paper


an Answer Booklet - provided by the Faculty

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 7 printed pages
Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2007/CSC425

PART A (70 MARKS)

QUESTION 1
Given the following declarations
double x = 5.5, y = -2.5;
int m = 20, n = 6;
string s = "Hello there!", t = "World";
What are the values of the following expressions?
a)
b )
c)
d)
e)
f)

x + n * y - (x + n) * y
m / n + m % n + 5 * x - n / 5
static_cast<int>(x + 3.0) + sqrt(sqrt(pow((m - n*2), 2 ) ) )
2 - (2 - (2 - (2 - n ) ) )
s . s u b s t r ( 4 , 2)
s . l e n g t h () + t . l e n g t h ()
(12 marks)

QUESTION 2
If the variables a, b, and c (type d o u b l e ) are 504, 3 0 2 . 5 5 8 , - 1 2 . 3 1 , respectively. Write
the C++ statements that will display the following line (for clarity, a "_" denotes a blank
space).

504

302.56

-12.3
(3 marks)

QUESTION 3
Assuming x (type double) is 12.335 and n (type i n t ) is 100. Show the output lines for the
following statements. For clarity, use the underscore symbol ("_") to denote a blank space.
cout < <
<<
<<
<<
cout < <
cout < <
<<
<<

showpoint << fixed


setprecision (2)
"\n x is " << setw (6) X
" n is " setw(4) << n ;
"\n\n n is " setw(2) n;
showpoint << fixed
setprecision (1)
"\n x is " setw (5) x;
(4 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2007/CSC425

QUESTION 4

Write a C++ assignment statement to compute each of the following:


a) The volume of sphere using the formula:

(2 marks)
b) Extract the last three digits of the integer stocknumber and assign to p r i c e with a
decimal point before the last two digits, (e.g. if stocknumber is 1758316, the last three
digits are 316, therefore, p r i c e is assigned the value of 3 .16)
(3 marks)
c) Calculate the area of a triangle using Hero's formula: area = ^(s

- a)(s - b)(s - c)
(2 marks)

QUESTION 5
Write appropriate C++ statement(s) for each of the following:
a) A certain city classifies a pollution index less than 35 as "pleasant", 35 through 60 as
"unpleasant", and above 60 as "hazardous". Display the appropriate classification of a
number representing a pollution index.
(4 marks)
b) For each integer numint with value 10, 11, 12,13, 14, and 15, display the symbols A,
B,C, D, E, and F, respectively.
(4 marks)
c)

Display a list of points (x, y) on the graph of y = x4 - 3x + 1 for x ranging from -2 to 2 in


steps of 0.2.
(4 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CS/APR 2007/CSC425

CONFIDENTIAL

QUESTION 6
Referring to the following i f statement, assume that num is an i n t type:
if (fabs(num) <= 4)
i f (num > 0)
cout << 2 * num + 1 endl;
else
cout 2 * num << endl
'

else
cout << num << " is out of range.\n";
What is the output produced from the above statement if:
a) num i s - 9
b) num is 0
c) num is 3
(6 marks)

QUESTION 7
Referring to the following program segment, answer the questions that follow.
cout << "\nEnter an integer: ";
cin >> n;
product = n;
count = 0;
while (count < 4)
cout "\nproduct is " << product;
product *= n;
++count;

a) What output values are displayed by the above program segment for each of the
following data value entered?
(i)
5
(ii) 6
(iii) 7
(6 marks)
b) In general, for a data value of any number n, what does this loop display?
(3 marks)
c) Rewrite the above program segment by replacing the w h i l e loop with the f o r loop that
will produce the same output.
(3 marks)
Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2007/CSC425

QUESTION 8
Assuming that the array nums [ ] is initially as shown below.
nums[]

35

50

17
[2;

109

88
[4]

Show the contents of the array elements after each of the following program segments is
executed.

a)
temp = nums[0];
nums[0] = nums[2];
nums[2] = temp;

nums

[o;

[1]

[2]

[4]

(3 marks)
b)
temp = nums[0];
for (i = 1; i < 3; ++ i)
nums [i] = nums[i + l];
nums [4] = temp;

nums

[2;

[4]

(5 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CS/APR 2007/CSC425

CONFIDENTIAL

QUESTION 9
The array monthName [ ] holds the following three-character strings.
monthName[]

Jan

Feb

Mar

Apr

[0]

[1]

[2]

[3]

May

Jun

Jul

Aug

Sep

Oct

Nov

Dec

[4]

[5]

[6]

[7]

[8]

[9]

[10]

[11]

a) If integer n is 3, what is displayed by the following statement?


cout << monthName[++n] << " and " << monthName[n + 3 ] ;

(3 marks)
b) What value is assigned to string variable winterMonths by the following statement?
winterMonths

= monthName[11] + " ,
+ monthName[1];

" + monthName[0]

+ ",

"

(3 marks)

Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

CONFIDENTIAL

CS/APR 2007/CSC425

PARTB (30 MARKS)

QUESTION 1
Write a program that calculates and displays values for y when
y = xz(x - z)
Your program should calculate y for values of x ranging between l and 5 and values of z
ranging between 2 and 6. The x variable should control the outer loop and be incremented
in steps of 1 and z should be incremented in steps of l . Your program should also display
the message " F u n c t i o n Undefined" when the x and y values are equal. The output of
your program should look like the following. (Note: you are required to use programmerdefined functions in your program)

1.0
1.0
1.0
2.0
2.0
2.0

2.0
3.0
4.0
2.0
3.0
4.0

-2.000
-1.500
-1.333
Function Undefined
-6.000
-4.000
(15 marks)

QUESTION 2
The expansion of a steel bridge as it is heated to a final Celsius temperature, TF from an
initial Celsius temperature, To can be approximated using the formula
Increase in length = a * L* (TF -To)
where a is a coefficient of expansion (which for steel is 11.7 e-6) and L is the length of a
bridge at temperature To. Using this formula, write a C++ program that displays a table of
expansion lengths for a steel bridge that is 7365 meters long at 0 degrees Celsius, as the
temperature increases to 40 degrees in 5 degree increments. (Note: you are required to
use programmer-defined functions in your program)
(15 marks)

END OF QUESTION PAPER


Hak Cipta Universiti Teknologi MARA

CONFIDENTIAL

You might also like