You are on page 1of 11

COMPILATION OF PAST YEARS EXAM QUESTIONS

TEST 1

Academic Session 2021/2022

QUESTION 3

(a) Given the following program in Figure 5.

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]

(b) Given the following program segment in Figure 6.

1 int row, col;


2
3 for(row = 1; row <= 3; row++)
4 {
5 for(col = 1; col <= 5; col++)
6 printf ("#");
7 printf ("\n");
8 }

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

Academic Session 2017/2018

Question 2

(a) Rephrase the if…else segment in Figure 1 to a switch statement.

if (day = = 1)

printf (“\n Today is Monday \n”);

else if (day = = 2)

printf (“\n Today is Tuesday \n”);

else

printf (“\n Invalid\n”);

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)

(ii) Trace the output of this program segment.

(4 Marks/ Markah)

(iii) State the type of the controlled loop in Figure 3

(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. }

(i) Construct a function prototype at line 4 and 5.

(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 pointer ab : 0x7ffcc3ad291c


Content of pointer ab : 29
The value of m assigned to 34 now.

Address of pointer ab : 0x7ff3ad291c


Content of pointer ab : 34
The pointer variable ab is assigned the value 7 now

Address of m : 0x7ffcc3ad291c
Value of m : 7

Figure 7

(i) Declare the variable ab to be a pointer to an object of type int.

(1 Marks/ Markah)

(ii) Assign the address of variable m to pointer variable ab.

(1 Marks/ Markah)

(iii) Print the address stored in ab.

(1 Marks/ Markah)

(iv) Print the value stored in ab.


(1 Marks/ Markah)

(v) Assign the pointer variable ab to value 7.


(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

Title : Me Before You


Author : Mayes Jojo
Publisher : Penguin
Book_ID : 9780718157838

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)

(ii) Declare Non-Fiction and Fiction book as type Book.


[Istiharkan buku bukan fiksyen dan buku fiksyen sebagai buku.]

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)

(iv) Display the input data of fiction book structure variable.


[Paparkan data input buku fiksyen struktuur buku.]

(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

Academic Session 2018/2019

Question 2

9
CONFIDENTIAL

10
CONFIDENTIAL

11

You might also like