CCSB163 Final - Mar 2007.pdf - MetaLab - Universiti Tenaga Nasional
CCSB163 Final - Mar 2007.pdf - MetaLab - Universiti Tenaga Nasional
CCSB163 Final - Mar 2007.pdf - MetaLab - Universiti Tenaga Nasional
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
UNIVERSITI TENAGA NASIONAL<br />
College of Information Technology<br />
BACHELOR OF INFORMATION TECHNOLOGY (HONS.)<br />
BACHELOR OF COMPUTER SCIENCE (HONS.)<br />
BACHELOR OF MECHANICAL ENGINEERING (HONS.)<br />
FINAL EXAMINATION<br />
SEMESTER II 2006/2007<br />
PRINCIPLES OF PROGRAMMING<br />
(<strong>CCSB163</strong>/CSEB113)<br />
April 2007<br />
Time allowed: 3 hours + 10 minutes for reading<br />
INSTRUCTIONS TO CANDIDATES<br />
1. The total marks for this exam is 100 marks.<br />
2. There are THREE (3) SECTIONS to this paper: Section A, Section B, and Section C.<br />
3. Answer ALL questions in the answer booklet provided.<br />
DO NOT OPEN THIS QUESTION PAPER UNTIL YOU ARE INSTRUCTED TO DO SO<br />
THIS QUESTION PAPER CONSISTS OF 11 PRINTED PAGES INCLUDING THIS PAGE
SECTION A: MULTIPLE-CHOICE (20 QUESTIONS, 30 MARKS)<br />
Instruction: Please select the BEST answer from the given choices.<br />
1. What the following program would display<br />
(The symbol '#' stands for one blank character.)<br />
int main(void)<br />
{<br />
double a, b;<br />
a = 11.56;<br />
b = 101.117;<br />
printf("Is it%6.1f%9.4f", a, b);<br />
printf("\n");<br />
return (0);<br />
}<br />
(a)<br />
(b)<br />
(c)<br />
(d)<br />
Is#it##11.6#101.1170\n<br />
Is#it##11.6#101.1170<br />
Is#it##11.5#101.1170<br />
Is#it##11.6#101.117\n<br />
2. Every statement in C must end with a<br />
(a) period (.)<br />
(b) backslash (/)<br />
(c) colon (:)<br />
(d) semicolon (;)<br />
3. Which of the following is not a VALID escape sequence<br />
(a) \~<br />
(b) \\<br />
(c) \n<br />
(d) \”<br />
4. Which of the following is an INVALID identifier (variable name)<br />
(a) _<strong>Mar</strong>ks<br />
(b) MaRK<br />
(c) 5MARKS<br />
(d) MARKS5<br />
Semester 2 2006/2007<br />
Page 2 of 11<br />
Principles of Programming
5. Using the following function definition, the parameter list is represented by<br />
X Y( Z ) {<br />
D<br />
}<br />
(a)<br />
(b)<br />
(c)<br />
(d)<br />
X<br />
Y<br />
Z<br />
D<br />
6. Which of the types listed below can be the type of the result value returned by<br />
a user-defined function<br />
(a) int<br />
(b) double<br />
(c) char<br />
(d) all of the above<br />
7. What will be the value of variable i after the C statements below have been<br />
executed<br />
i = 2;<br />
j = 10;<br />
if ((3 * i) < j)<br />
i = i + 2;<br />
i = i + 3;<br />
(a) 5<br />
(b) 6<br />
(c) 7<br />
(d) 10<br />
Semester 2 2006/2007<br />
Page 3 of 11<br />
Principles of Programming
8. What is the output from the following C program fragment if the value of<br />
variable input is 0<br />
switch (input) {<br />
case 0:<br />
case 1:<br />
printf("Cat ");<br />
case 2:<br />
printf("Dog");<br />
break;<br />
case 3:<br />
printf("Cow");<br />
case 4:<br />
printf("Coyote");<br />
}<br />
printf("\n");<br />
(a)<br />
(b)<br />
(c)<br />
(d)<br />
Cow<br />
CatDog<br />
Dog<br />
Cat<br />
9. What is wrong with the following while loop<br />
(a)<br />
While should be written as while.<br />
(b) Braces are required around sum = sum +30;.<br />
(c)<br />
While (sum
11. What is the final value of x after performing the following operations<br />
int x = 31;<br />
double y = 9;<br />
double z = 14;<br />
y = x / z;<br />
x = 5.5 * y;<br />
(a) 12.18<br />
(b) 11<br />
(c) 12<br />
(d) 11.5<br />
12. Which of the following is not specified by the for loop below<br />
for ( c = 1; c
15. Which statement is true regarding the statement below:<br />
++frequency[ responses[ answer ] ];<br />
(a)<br />
(b)<br />
(c)<br />
(d)<br />
This statement increases the appropriate frequency counter depending<br />
on the value of responses[ answer ].<br />
This statement increases the appropriate answer counter depending on<br />
the value of frequency[ responses ].<br />
This statement increases the appropriate responses counter depending<br />
on the value of frequency[ answer ].<br />
This statement produces a syntax error because subscripts cannot be<br />
nested.<br />
16. Which statement is FALSE<br />
(a) In declaring an array, the size can be a constant or variable.<br />
(b) Index used to access an array can be a constant or variable.<br />
(c) The index of the first element of an array is always 0.<br />
(d) The name of an array is actually the address of the first element of the<br />
array.<br />
17. What value is returned by the following call to strlen<br />
strlen("computer")<br />
(a) 7<br />
(b) 8<br />
(c) 9<br />
(d) 10<br />
Semester 2 2006/2007<br />
Page 6 of 11<br />
Principles of Programming
18. In the fragment below, what is the minimum size of result required for<br />
successful and valid concatenation of "double" and "trouble"<br />
strcpy(result, "double");<br />
strcat(result, "trouble");<br />
(a) 11<br />
(b) 12<br />
(c) 14<br />
(d) 15<br />
19. Which of the following correctly copies the contents of string2 into<br />
string1 Assume that string2 is equal to "Good" and string1 is<br />
equal to "Morning"<br />
(a) strcat( string1, string2 );<br />
(b) strcpy( string2, string1, 7 );<br />
(c) strncpy( string1, string2, 6 );<br />
(d) strcpy( string1, string2 );<br />
20. Which statement is FALSE<br />
(a) Function strcpy copies its first argument into its second argument.<br />
(b) Function strncpy does not necessarily copy the terminating null<br />
character of its second argument.<br />
(c) A common error is not appending a terminating null character to the<br />
first argument of a strncpy when the third argument is less than or<br />
equal to the length of the string in the second argument.<br />
(d) The first character of the second argument of strcat replaces the<br />
null character that terminates the string in the first argument.<br />
Semester 2 2006/2007<br />
Page 7 of 11<br />
Principles of Programming
SECTION B: SHORT ANSWERS (5 QUESTIONS, 40 MARKS)<br />
Instruction: Please answer ALL the questions. DO NOT show any working<br />
unless stated by the question.<br />
s<br />
Question 1<br />
(a)<br />
Rewrite the following code using code fragment using switch statement:<br />
if (ch == 'E' || ch =='e')<br />
countE++;<br />
else if (ch == 'A' || ch == 'a')<br />
countA++;<br />
else if (ch == 'I' || ch == 'I')<br />
countI++;<br />
else<br />
printf("Error –Not A,E, or I\n");<br />
[5 marks]<br />
(b)<br />
If originally x=0, y=0, and z=0, what is the value of x, y, and z after<br />
executing the following code<br />
if (x)<br />
if (y)<br />
if(z)<br />
z=3;<br />
else<br />
z=2;<br />
[3 marks]<br />
Question 2<br />
(a) Declare a void function named my_func with ONE (1) parameter of type float<br />
[2 marks]<br />
(b)<br />
Briefly describe the difference between a GLOBAL variable and a LOCAL<br />
variable.<br />
[2 marks]<br />
Semester 2 2006/2007<br />
Page 8 of 11<br />
Principles of Programming
(c)<br />
(d)<br />
Briefly describe the difference between local variable with and without<br />
STATIC storage.<br />
[2 marks]<br />
Trace and write the output for the program below:<br />
#include <br />
int m=12;<br />
int f(int a,int b,int c);<br />
void main(void)<br />
{<br />
int n=30;<br />
int x=1,y=2,z=3,i;<br />
printf("Before: m=%d, n=%d, x=%d\n",m,n,x);<br />
i=f(x,y,z);<br />
}<br />
printf("After: m=%d, n=%d, x=%d\n", m,n,x);<br />
int f(int a,int b,int c)<br />
{<br />
int n=100;<br />
printf("In function f: n=%d, m=%d, a=%d\n",n,m,a);<br />
m=999;<br />
if (a >=1)<br />
{<br />
a+=b+m+n;<br />
printf("modified: n=%d, m=%d, a=%d\n",n,m,a);<br />
}<br />
}<br />
return a;<br />
[6 marks]<br />
Semester 2 2006/2007<br />
Page 9 of 11<br />
Principles of Programming
Question 3<br />
(a) Change program fragment below from do–while loop to the equivalent while<br />
loop.<br />
x = 0;<br />
do{<br />
printf(“%d\n”, x);<br />
x++;<br />
}while(x < 100);<br />
[4 marks]<br />
(b) Write a C program fragment using for loop to calculate x y (x to the power of y)<br />
where x and y are variables.<br />
(e)<br />
[5 marks]<br />
Write a C program fragment using nested for loops to produce the diagram<br />
below<br />
Question 4<br />
BBBBBBBBB<br />
BBBBBBB<br />
BBBBB<br />
BBB<br />
B<br />
[6 marks]<br />
The function chr2val below is supposed to return the equivalent integer value of<br />
the character in input_digit if it is a valid digit character. Otherwise, it will<br />
return -1. Fill up the FIVE blanks accordingly<br />
________ chr2val (char input_digit)<br />
{<br />
}<br />
if (________ && ________)<br />
else<br />
return ________;<br />
return ________;<br />
[5 marks]<br />
Semester 2 2006/2007<br />
Page 10 of 11<br />
Principles of Programming
SECTION C: DESIGNING/ PROGRAMMING (2 QUESTIONS, 30 MARKS)<br />
Instruction: Please answer ALL the questions.<br />
Question 1<br />
A series of numbers are as follow:<br />
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89…<br />
Observe that all the numbers in this series EXCEPT the first and second are the sum<br />
of their previous two numbers.<br />
Design a program using pseudocode to display the nth number in this series where n is<br />
user input variable. Example, if user enters 6, the output is the 6th number of the<br />
series which is 8. If user enters any numbers less than 1, the program should display<br />
“Invalid input of n!”<br />
Example 1: Example 2:<br />
Enter n: 6<br />
The 6th number of the series is 8<br />
Enter n: 0<br />
Invalid input of n!<br />
[15 marks]<br />
Question 2<br />
Write a complete C program to solve the following problem.<br />
Get a list of POSITIVE INTEGER (including 0) from user and store in an ARRAY.<br />
Read in ONE integer at a time. User needs to enter ANY NEGATIVE INTEGER to<br />
indicate end of the list. Next, the program should determine if the list of integers<br />
entered is sorted in increasing order, decreasing order or is unordered. You may<br />
ASSUME that the maximum number of integers user would enter is 10.<br />
Example 1: Example 2: Example 3:<br />
Enter integer no.1 : 1<br />
Enter integer no.2 : 3<br />
Enter integer no.3 : 4<br />
Enter integer no.4 : -4<br />
Increasing order<br />
Enter integer no.1 : 9<br />
Enter integer no.2 : 5<br />
Enter integer no.3 : 2<br />
Enter integer no.4 : -1<br />
Decreasing order<br />
Enter integer no.1 : 6<br />
Enter integer no.2 : 3<br />
Enter integer no.3 : 5<br />
Enter integer no.4 : -2<br />
Unsorted<br />
[15 marks]<br />
---End of Questions---<br />
Semester 2 2006/2007<br />
Page 11 of 11<br />
Principles of Programming