Professional Documents
Culture Documents
TEST 1
QUESTION 3
1 #include <stdio.h>
2 int main()
3 { int i;
4 for(i = 1; i <= 5; i--)
5 { if(i%2==0)
6 { printf ("i = %d\n",i);
7 }
8 }
9 return 0;
10 }
Figure 5
(i) In the above program, the for loop will be executed for an infinite number of times.
Explain why this condition happens. [1 Mark]
(ii) Rewrite the for loop so that the infinite loop could be avoided. [1 Mark]
(iii) Based on your answer in 3(a)(ii), determine the output of the program. [2 Marks]
Figure 6
(i) Determine how many times does the call to printf statement (in line 6) are executed. [1
Mark]
(ii) Determine how many times does the call to printf statement (in line 7) are executed. [1
Mark]
(iii) Convert the for loop into a while loop to produce the same output. [4 Marks]
CONFIDENTIAL
FINAL EXAM
Question 2
if (day = = 1)
else if (day = = 2)
else
Figure 1
(4 Marks/ Markah)
(b) Answer the following questions based on the program segment shown in Figure 3.
i=0;
while(i<2)
{
j=3;
while(j>0)
{
printf("%d ",j);
printf("\n");
--j;
}
++i;
}
Figure 3
2
CONFIDENTIAL
(i) Rephrase the following nested while loops into nested for loops
(5 Marks/ Markah)
(4 Marks/ Markah)
(1 Marks/ Markah)
3
CONFIDENTIAL
Question 3
(a) Based on the incomplete C program shown in Figure 4, answer the following questions.
1. #include <stdio.h>
2
3. //Function prototype
4. ________________________
5. ________________________
6 int screen1();
7. float const pi = 3.244;
8
9. int main(){
10. int a;
11.
12. //Calling function screen1
13. _________________________
14. if (a == 1)
15. printf(“%f”,function1(5));
16. else
17. function2(6,10);
18. return 0;
19. }
20.
21. int screen1(){
22. int choice;
23. printf(“Enter your choice a for area of circle , b for
24. area of triangle”);
24. scanf(“%c”, &choice);
25. return (choice);
26. }
27.
28. ______ function1(_______________){
29.
30. return (3.144*radius*radius);
31. }
32.
33. void function2(_______________){
34. printf(“The area of triangle is %f\n”,0.5*h*w);
35. }
(2 Marks/ Markah)
4
CONFIDENTIAL
(ii) Complete a function call at line 13 that invoke the function screen1.
(1 Marks/ Markah)
(iii) Construct a function definition for function1 and function2 at line 28 and line 33.
(4 Marks/ Markah)
(iv) Examine the local and global variables in the C program shown in Figure 4, give two
(2) local variables and one (1) global variables.
(2 Marks/ Markah)
(v) Examine the program in Figure 4, if the screen1 function returns the value of 2,
what will be the output of the program?
(1 Marks/ Markah)
5
CONFIDENTIAL
Question 4
(a) Figure 7 shows the output that demonstrate how to handle the pointers in the C program.
Write a C program segments that perform each of the following tasks.
Address of m : 0x7ffc3ad291c
Value of m : 29
Now ab is assigned with the address of m.
Address of m : 0x7ffcc3ad291c
Value of m : 7
Figure 7
(1 Marks/ Markah)
(1 Marks/ Markah)
(1 Marks/ Markah)
6
CONFIDENTIAL
(b) Based on the information given in Figure 9, write the following C program segments.
Object
Book
Characteristic
Title
Author
Publisher
Book_ID
Types of Book
Non-Fiction
Fiction
Non-Fiction
Title : The Girl with Seven Names
Author : Hyeonseo Lee
Publisher : William Collins
Book_ID : 9780007554850
Fiction
Figure 9
(i) Define Book structure with its structure member’s title, author and
publisher as array which has 50, 50 and 100 elements.
(2 Marks/ Markah)
7
CONFIDENTIAL
(2 Marks/ Markah
(iii) Initialize the non-fiction book’s title structure variable from keyboard.
[Memasukkan data buku bukan fiksyen daripada keyboard.]
(2 Marks/ Markah)
(2 Marks/ Markah)
(b) Supposed you have the following program in Figure 6, analyze and trace the output.
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char cStudentName[21];
6. char cMyName [16];
7. char cYourName[16];
8.
9. strcpy (cMyName,"David Morgan");
10. printf("%s\n",cMyName);
11. int iLen;
12. iLen=strlen(cMyName);
13. strcpy (cYourName, "Marsha Farr");
14. printf("%d\n",iLen);
15. iLen=strlen(cYourName);
16. printf("%d\n",iLen);
17. strcpy (cYourName,cMyName);
18. printf("%d\n",strcmp(cYourName,cMyName));
19. printf("%s\n",cYourName);
20.
21. return 0;
22. }
Figure 6
(5 Marks/ Markah)
8
CONFIDENTIAL
FINAL EXAM
Question 2
9
CONFIDENTIAL
10
CONFIDENTIAL
11